• Spring jdbcTemplate


      承接【Spring事务管理】,上一篇我们已经简单接触了关于Spring jdbc的知识,今天我们承接上一篇进行一下补充,上一篇直接将dataSource注入到了Dao层进行了实现,本篇我们通过简单进行一下补充,将另外两种实现为大家进行一下演示:1、在自己定义的DAO 实现类中注入一个DataSource 引用来完 成JdbcTemplate 的实例化。也就是它是从外部“注入” DataSource 到DAO 中,然后 自己实例化JdbcTemplate,然后将DataSource 设置到JdbcTemplate 对象中,源码下载;2、 在 Spring 的 IoC 容器中配置一个 JdbcTemplate 的 bean,将 DataSource 注入进来,然后再把JdbcTemplate 注入到自定义DAO 中,源码下载;3、Spring 提供了 org.springframework.jdbc.core.support.JdbcDaoSupport 类 , 这 个 类 中 定 义 了 JdbcTemplate 属性,也定义了DataSource 属性,当设置DataSource 属性的时候,会创 建jdbcTemplate 的实例,所以我们自己编写的DAO 只需要继承JdbcDaoSupport 类, 然后注入DataSource 即可,源码下载。提倡采用第三种方法。

      对于第一种实现,我们在上一篇博客中已经进行了探讨,在这里就不再赘述,本篇一起来看一下另外两种:

      1、JdbcTemplate 方式注入Dao

      a、修改我们的applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
        
        <context:property-placeholder location="classpath:db.properties"/>
        
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="${jdbc.dirverClass}" />
            <property name="url" value="${jdbc.url}" />
            <property name="username" value="${jdbc.user}" />
            <property name="password" value="${jdbc.password}" />
        </bean>
        
        <bean id="blankService" class="com.edu.hpugs.spring.BlankServiceImpl">
            <property name="blankDao" ref="blankDao"></property>
        </bean>
        
        <bean id="blankDao" class="com.edu.hpugs.spring.BlankDaoImpl">
            <property name="jdbcTemplate" ref="jdbcTemplate"></property>
        </bean>
        
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
        
    </beans>

      b、修改Dao:

    public class BlankDaoImpl implements IBlankDao {
        
        private JdbcTemplate jdbcTemplate;
        
        public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
            this.jdbcTemplate = jdbcTemplate;
        }
    
        public void outMonery(String userName, double monery) {
            // TODO Auto-generated method stub
            String sql = "UPDATE userAccount SET monery = monery - ? WHERE name = ?";
            jdbcTemplate.update(sql, monery, userName);
        }
    
        public void inMonery(String userName, double monery) {
            // TODO Auto-generated method stub
            String sql = "UPDATE userAccount SET monery = monery + ? WHERE name = ?";
            jdbcTemplate.update(sql, monery, userName);
        }
    
    }

      ok,完成

      2、下面看一下基于继承JdbcDaoSupport的实现:

      我们的无需修改applicationContext.xml文件,只需Dao类继承一下JdbcDaoSupport:

    public class BlankDaoImpl extends JdbcDaoSupport implements IBlankDao {
    
        public void outMonery(String userName, double monery) {
            // TODO Auto-generated method stub
            String sql = "UPDATE userAccount SET monery = monery - ? WHERE name = ?";
            this.getJdbcTemplate().update(sql, monery, userName);
        }
    
        public void inMonery(String userName, double monery) {
            // TODO Auto-generated method stub
            String sql = "UPDATE userAccount SET monery = monery + ? WHERE name = ?";
            this.getJdbcTemplate().update(sql, monery, userName);
        }
    
    }

      

      好了到这里关于补充内容就介绍完毕,如有不当之处,还望批评指正。谢谢

  • 相关阅读:
    loadrunner实现字符串的替换
    Windows Server 2008 R2 实现多用户同时登陆
    LoadRunner测试场景中添加负载生成器
    loadrunner统计字符串中指定字符出现的次数
    loadrunner怎么将变量保存到参数中
    loadrunner生成随机数
    loadrunner取出字符串的后面几位
    loadrunner参数化excel数据
    LoadRunner常用函数列表
    loadrunner解决在项目中的难点解决
  • 原文地址:https://www.cnblogs.com/AndroidJotting/p/7517668.html
Copyright © 2020-2023  润新知