Spring JDBC-NamedParameterJdbcTemplate模板类

概述

除了标准的JdbcTemplate外,Spring还提供了两个易用的JDBC模板类

  • SimpleJdbcTemplate 封装了JdbcTemplate,将常用的API开放出来 . 这里暂不讨论
  • NamedParameterJdbcTemplate 提供命名参数绑定的功能。

在低版本的Spring 中, 用户只能使用“?”占位符声明参数,并使用索引号绑定参数,必须要保证参数的索引号和SQL语句中的占位符“?”的位置正确匹配。

NamedParameterJdbcTemplate模板了支持命名参数变量的SQL,位于org.springframework.jdbc.namedparam包中,该包中还定义了一个用于承载命名参数的SqlParameterSource接口

  • BeanPropertySqlParameterSource:该实现类是将一个JavaBean对像封装成一个参数源,以便通过JavaBean属性名和SQL语句中的命名参数匹配的方式绑定参数

  • MapSqlparameterSource:该实现类内部通过一个Map存储参数,可以通过addValue(String paramName , Object value) 或 addValue(Map value)添加参数,并通过参数键名和SQL语句的命名参数的方式绑定参数。


示例

BeanPropertySqlParameterSource 使用示例

package com.xgj.dao.namedParameterJdbcTemplate.dao;

import org.springframework.beans.factory.annotation.Autowired;
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.Repository;

import com.xgj.dao.namedParameterJdbcTemplate.domain.Artisan;

/**
 * 
 * 
 * @ClassName: ArtisanNJDaoImpl
 * 
 * @Description: @Repository标注的DAO层,受Spring管理
 * 
 * @author: Mr.Yang
 * 
 * @date: 2017年9月30日 上午12:42:26
 */

@Repository
public class ArtisanNJDaoImpl implements ArtisanNJDao {

    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    private final static String insertArtisanSql = "insert into artisan(artisan_name) values(:artisanName)";

    /**
     * 
     * 
     * @Title: setNamedParameterJdbcTemplate
     * 
     * @Description: 自动注入namedParameterJdbcTemplate
     * 
     * @param namedParameterJdbcTemplate
     * 
     * @return: void
     */
    @Autowired
    public void setNamedParameterJdbcTemplate(
            NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
        this.namedParameterJdbcTemplate = namedParameterJdbcTemplate;
    }

    @Override
    public void addArtisan(Artisan artisan) {

        // 定义命名参数
        SqlParameterSource sps = new BeanPropertySqlParameterSource(artisan);
        // 使用模板类方法
        namedParameterJdbcTemplate.update(insertArtisanSql, sps);
    }

}

在SQL语句中声明命名参数的格式为

:paranName

比如values(:artisanName) ,多个参数使用逗号分隔。

在这个示例中,使用BeanPropertySqlParameterSource提供数据源,它接收一个JavaBean作为构造函数的入参,调用namedParameterJdbcTemplate.update(insertArtisanSql, sps)执行插入数据的而操作。

配置文件

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

    <!-- 扫描类包,将标注Spring注解的类自动转化Bean,同时完成Bean的注入 -->
    <context:component-scan base-package="com.xgj.dao.namedParameterJdbcTemplate" />


    <!-- 使用context命名空间 配置数据库的properties文件 -->
    <context:property-placeholder location="classpath:spring/jdbc.properties" />

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close" 
        p:driverClassName="${jdbc.driverClassName}"
        p:url="${jdbc.url}" 
        p:username="${jdbc.username}" 
        p:password="${jdbc.password}" />


    <!-- 定义 namedParameterJdbcTemplate-->
    <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
        <constructor-arg ref="dataSource"/>
    </bean>

</beans>

注意NamedParameterJdbcTemplate的配置,使用构造函数。


Domain

package com.xgj.dao.namedParameterJdbcTemplate.domain;

import java.io.Serializable;

public class Artisan implements Serializable {

    private static final long serialVersionUID = 1L;

    private String artisanId;
    private String artisanName;

    public String getArtisanId() {
        return artisanId;
    }

    public void setArtisanId(String artisanId) {
        this.artisanId = artisanId;
    }

    public String getArtisanName() {
        return artisanName;
    }

    public void setArtisanName(String artisanName) {
        this.artisanName = artisanName;
    }

}

Artisan拥有两个属性,我们这里没有插入ID,暂且忽略。 其中 artisanName 这个属性和 SQL语句中的命名参数匹配,参数即按照这个匹配关系进行绑定。


单元测试

package com.xgj.dao.namedParameterJdbcTemplate.dao;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.xgj.dao.namedParameterJdbcTemplate.domain.Artisan;

public class ArtisanNJDaoImplTest {

    ClassPathXmlApplicationContext ctx = null;
    ArtisanNJDaoImpl artisanNJDaoImpl = null;

    @Before
    public void initContext() {
        // 启动Spring 容器
        ctx = new ClassPathXmlApplicationContext(
                "classpath:com/xgj/dao/namedParameterJdbcTemplate/conf_namedParameterJdbcTemplate.xml");
        artisanNJDaoImpl = ctx.getBean("artisanNJDaoImpl",
                ArtisanNJDaoImpl.class);
        System.out.println("initContext successfully");
    }

    @Test
    public void queryTeacherById() {
        Artisan artisan = new Artisan();
        artisan.setArtisanName("ArtisanNJ");
        artisanNJDaoImpl.addArtisan(artisan);
    }

    @After
    public void closeContext() {
        if (ctx != null) {
            ctx.close();
        }
        System.out.println("close context successfully");
    }
}

结果:
查看下数据库是否插入成功 (只是演示,忽略ID…)

这里写图片描述


MapSqlParameterSource使用示例

如果有数据表记录没有对应的领域对象,则用户可以直接使用MapSqlparameterSource达到绑定参数的目的。

public void addArtisanWithMapSqlParameterSource(Artisan artisan) {

        // 使用MapSqlParameterSource绑定参数
        MapSqlParameterSource mapSqlParameterSource = new MapSqlParameterSource()
                .addValue("artisanName", artisan.getArtisanName());

        // 使用模板类方法
        namedParameterJdbcTemplate.update(insertArtisanSql,
                mapSqlParameterSource);
    }

由于MapSqlParameterSource中的大多数方法都能返回对象本身,所以可以将几个参数的调用串成一个链,假设Artisan还有个artisanSex属性,如下

    MapSqlParameterSource mapSqlParameterSource = new MapSqlParameterSource()
                .addValue("artisanName", artisan.getArtisanName())
                .addValue("artisanSex",artisan.getArtisanSex);

由于这个原因,使用方法调用链模式设计的API很容易使用。


单元测试

@Test
    public void queryTeacherById() {
        // Artisan artisan = new Artisan();
        // artisan.setArtisanName("ArtisanNJ");
        // artisanNJDaoImpl.addArtisan(artisan);

        Artisan artisan = new Artisan();
        artisan.setArtisanName("ArtisanMS");
        artisanNJDaoImpl.addArtisanWithMapSqlParameterSource(artisan);
    }

数据库结果

这里写图片描述


NamedParameterJdbcTemplate 支持 in 的操作

PrepareStatement的缺陷

如果我们想查找artisan_id在 1 ,3 , 5 中的数据, PrepareStatement对in的操作只能动态拼接

String in_data = "1,3,5";
pst = conn.prepareStatement("select artisan_name from artisan where artisan_id in (?)");
pst.setString(1,in_data);

使用传统的prepareStatement是动态设定参数的,也就是生成 select artisan_name from artisan where artisan_id in (?) ,一个 ? 代表一个参数,pst.setString(1,”1,3,5”) 就相当于 select artisan_name from artisan where artisan_id in (“1,3,5”) ,这个SQL是查找artisan_id为 “1,3,5”的记录,而不是在 1,3,5中记录。


NamedParameterJdbcTemplate的操作示例

NamedParameterJdbcTemplate可以很好地解决上述问题呢。

....

private final static String selectArtisanByIds = "select artisan_name from artisan where artisan_id in (:artisanId)";

....

public List<Artisan> getArtisanByIds(List<String> artisanIds) {

        final List<Artisan> artisanList = new ArrayList<Artisan>();

        // 使用MapSqlParameterSource绑定参数
        MapSqlParameterSource mapSqlParameterSource = new MapSqlParameterSource();

        mapSqlParameterSource.addValue("artisanId", artisanIds);

        namedParameterJdbcTemplate.query(selectArtisanByIds,
                mapSqlParameterSource, new RowCallbackHandler() {

                    @Override
                    public void processRow(ResultSet rs) throws SQLException {
                        Artisan artisan = new Artisan();
                        artisan.setArtisanName(rs.getString("artisan_name"));
                        // 加入集合
                        artisanList.add(artisan);
                    }
                });

        return artisanList;
    }

单元测试

@Test
    public void queryTeacherById() {
        List<String> artisanIds = new ArrayList<String>();
        artisanIds.add("1");
        artisanIds.add("3");
        artisanIds.add("5");
        List<Artisan> artisans = artisanNJDaoImpl.getArtisanByIds(artisanIds);
        for (Artisan artisan : artisans) {
            System.out.println("artisanName:" + artisan.getArtisanName());
        }
    }

输出:

2017-09-30 02:01:27,648  INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5f0ab5d: startup date [Sat Sep 30 02:01:27 BOT 2017]; root of context hierarchy
2017-09-30 02:01:27,777  INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/dao/namedParameterJdbcTemplate/conf_namedParameterJdbcTemplate.xml]
initContext successfully
artisanName:Xiao2
artisanName:Xiao0
artisanName:Xiao4
2017-09-30 02:01:30,387  INFO [main] (AbstractApplicationContext.java:984) - Closing org.springframework.context.support.ClassPathXmlApplicationContext@5f0ab5d: startup date [Sat Sep 30 02:01:27 BOT 2017]; root of context hierarchy
close context successfully

示例源码

代码已托管到Github—> https://github.com/yangshangwei/SpringMaster

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小工匠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值