1. 刚才编写的代码使用的是new的方式,应该把这些类交给Spring框架来管理。
2. 修改的步骤如下
    applicationContext.xml中<beans>标签的开头配置为:
      * spring-framework-3.2.0.RELEASE\docs\spring-framework-reference\html\xsd-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);
            }
        }
posted on 2018-12-17 20:59  wyhluckydog  阅读(529)  评论(0编辑  收藏  举报