使用Spring框架来管理模板类

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);
            }
        }

转载于:https://www.cnblogs.com/wyhluckdog/p/10133878.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值