Spring JDBC-NamedParameterJdbcTemplate模板类

举报
小工匠 发表于 2021/09/10 23:16:33 2021/09/10
【摘要】 概述示例 BeanPropertySqlParameterSource 使用示例MapSqlParameterSource使用示例 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);
    }

}

  
 
  • 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
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57

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

:paranName
  
 
  • 1

比如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>

  
 
  • 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

注意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;
    }

}

  
 
  • 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

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

  
 
  • 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

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

这里写图片描述


MapSqlParameterSource使用示例

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

public void addArtisanWithMapSqlParameterSource(Artisan artisan) {

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

        // 使用模板类方法
        namedParameterJdbcTemplate.update(insertArtisanSql,
                mapSqlParameterSource);
    }
  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

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

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

由于这个原因,使用方法调用链模式设计的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);
    }
  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

数据库结果

这里写图片描述


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);
  
 
  • 1
  • 2
  • 3

使用传统的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;
    }
  
 
  • 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

单元测试

@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());
        }
    }
  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

输出:

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

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

示例源码

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

文章来源: artisan.blog.csdn.net,作者:小小工匠,版权归原作者所有,如需转载,请联系作者。

原文链接:artisan.blog.csdn.net/article/details/78141709

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。