spring之-jdbcTemplate和NamedParameterJdbcTemplate的小差异

jdbcTemplate和NamedParameterJdbcTemplate都是spring框架提供的访问数据库的方法。 
jdbcTemplate是一个通用类,几乎可以在所有的场合使用; 
NamedParameterJdbcTemplate 提供了命名参数绑定的功能。

两个在spring的xml配置文件中,引入的方式不一样

  <bean id="jdbcTemplate" class ="org.springframework.jdbc.core.JdbcTemplate" 
        p:dataSource-ref="dataSource" />
 
 
  • 1
  • 2
   <bean id="namedParameterJdbcTemplate" class ="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate"  >

<constructor-arg ref="dataSource" />

其中,jdbcTemplate 是直接引入了数据源,而namedParameterJdbcTemplate,是在构造函数中,引入了数据源。

用namedParameterJdbcTemplate实现命名参数绑定的源代码

package com.spring.jdbc;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Service;

/**  
 * 利用spring的NamedParameterJdbcTemplate进行参数绑定
 *  
 * @author 范芳铭
 */ 
@Service("namedParameterService")
public class EasyNamedParameterJdbc {
    //@Autowired private JdbcTemplate jdbcTemplate;
    @Autowired private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    public void updateLastLogonTime(FfmUser user){
        final String sql = "update ffm_user u set u.last_logon_time = :lastLogonTime " +
                " where username =:username ";
        SqlParameterSource ss = new BeanPropertySqlParameterSource(user);
        int result = namedParameterJdbcTemplate.update(sql, ss);
        System.out.println("数据更新完成:" + result);

    }

    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("b_jdbc.xml");
        EasyNamedParameterJdbc service = (EasyNamedParameterJdbc)ctx.getBean("namedParameterService");
        FfmUser user = new FfmUser();
        user.setUsername("ffm");
        user.setLastLogonTime(System.currentTimeMillis());

        service.updateLastLogonTime(user);

    }
}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

配套bean

package com.spring.jdbc;
/**  
 * NamedParameterJdbcTemplate进行参数绑定的bean
 *  
 * @author 范芳铭
 */ 
public class FfmUser {
    private String username;
    private long lastLogonTime;
    private int charge;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }

    public long getLastLogonTime() {
        return lastLogonTime;
    }
    public void setLastLogonTime(long lastLogonTime) {
        this.lastLogonTime = lastLogonTime;
    }
    public int getCharge() {
        return charge;
    }
    public void setCharge(int charge) {
        this.charge = charge;
    }

}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

b_jdbc.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:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/task
        http://www.springframework.org/schema/task/spring-task.xsd">

   <context:component-scan base-package="com.spring.jdbc"/>    

   <context:property-placeholder location="classpath:dbconfig.properties" />
   <bean id = "dataSource"  class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method = "close"
        p:driverClassName="${jdbc.o2o.driverClassName}"
        p:url="${jdbc.o2o.url}"
        p:username="${jdbc.o2o.username}"
        p:password="${jdbc.o2o.password}"
   />

   <bean id="jdbcTemplate" class ="org.springframework.jdbc.core.JdbcTemplate" 
        p:dataSource-ref="dataSource" />
   <bean id="namedParameterJdbcTemplate" class ="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate"  >
        <constructor-arg ref="dataSource" />
    </bean>     

</beans>


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值