zoukankan      html  css  js  c++  java
  • 使用Spring框架来管理模板类

    1. 刚才编写的代码使用的是new的方式,应该把这些类交给Spring框架来管理。
    2. 修改的步骤如下
        applicationContext.xml中<beans>标签的开头配置为:
          * spring-framework-3.2.0.RELEASEdocsspring-framework-referencehtmlxsd-config.html,在网页中找到"40.2.10 the jdbc schma"标题,然后将下面标签的内容复制到我们的配置文件中。
        配置内容为:
     
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:jdbc="http://www.springframework.org/schema/jdbc" xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd"> 
    </beans>
      * 步骤一:Spring管理内置的连接池
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
                <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql:///spring_day03"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </bean>
    
        * 步骤二:Spring管理模板类
            <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
                <property name="dataSource" ref="dataSource"/>
            </bean>
    
        * 步骤三:编写测试程序
            @RunWith(SpringJUnit4ClassRunner.class)
            @ContextConfiguration("classpath:applicationContext.xml")
            public class Demo2 {
    
                @Resource(name="jdbcTemplate")
                private JdbcTemplate jdbcTemplate;
    
                @Test
                public void run2(){
                    jdbcTemplate.update("insert into t_account values (null,?,?)", "测试2",10000);
                }
            }
  • 相关阅读:
    robots.txt
    procdump和mimikatz工具配合破解windows账户口令
    通过vbs脚本控制方向盘按键
    批处理删除文件或文件夹代码
    彩色线条雨特效html代码
    secureCRT
    chrome 更新flash插件
    python命令行下安装redis客户端
    FastJson使用
    Redis 学习(二)
  • 原文地址:https://www.cnblogs.com/wyhluckdog/p/10133878.html
Copyright © 2011-2022 走看看