Spring4---使用NamedParameterJdbcTemplate

在这里插入图片描述

在这里插入图片描述

  • 示例:使用具名参数
  1. 在Spring配置文件中配置一个NamedParameterJdbcTemplate实例,该类没有无参构造器,需要一个DataSource参数
<!--配置c3p0链接数据库对象-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="user" value="${user}"/>
    <property name="password" value="${password}"/>
    <property name="driverClass" value="${driver}"/>
    <property name="jdbcUrl" value="${url}"/>
</bean>
    
<!--配置NamedParameterJdbcTemplate-->
<bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
    <constructor-arg type="javax.sql.DataSource" ref="dataSource"/>
</bean>
  • 编写测试类
package mao.shu.spring.jdbc.template;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterBatchUpdateUtils;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;

import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.*;

public class DepartmentDaoTest {
    private ApplicationContext app;
    private NamedParameterJdbcTemplate npjt;


    @Before
    public void before() {
        this.app = new ClassPathXmlApplicationContext("mao/shu/spring/jdbc/template/template.xml");
        this.npjt = this.app.getBean("namedParameterJdbcTemplate", NamedParameterJdbcTemplate.class);
    }

    /**
     * 测试使用map集合作为参数,添加一条参数
     */
    @Test
    public void testNamed() {
        String sql = "INSERT INTO department(deptno,dname,loc)VALUES(:deptno,:dname,:loc)";
        Map<String, Object> map = new HashMap<>();
        map.put("deptno", 4);
        map.put("dname", "测试部门");
        map.put("loc", "火星");
        this.npjt.update(sql, map);
    }


    /**
     * 使用 SqlParameterSource作为参数
     * 测试使用持续化类作为对象,添加一条数据
     */
    @Test
    public void testNamedObject() {
        String sql = "INSERT INTO department(deptno,dname,loc)VALUES(:deptno,:dname,:loc)";
        Department dept = new Department();
        dept.setDeptno(6);
        dept.setDname("测试部3");
        dept.setLoc("月球");
        SqlParameterSource sqlParameterSource = new BeanPropertySqlParameterSource(dept);
        this.npjt.update(sql, sqlParameterSource);
    }

    /*测试添加多条语句*/
    @Test
    public void testNamedObjects() {
        String sql = "INSERT INTO department(deptno,dname,loc)VALUES(:deptno,:dname,:loc)";
        SqlParameterSource[] args = new SqlParameterSource[10];
        for (int i = 0; i < args.length; i++) {
            Department dept = new Department();
            dept.setDeptno(6+i);
            dept.setDname("测试部"+i);
            dept.setLoc("月球"+i);
            SqlParameterSource sqlParameterSource = new BeanPropertySqlParameterSource(dept);
            args[i] = sqlParameterSource;
        }

        this.npjt.batchUpdate(sql, args);
    }
}
  • 使用具名参数的好处

  • 如果sql语句中有多个参数,则使用具名参数不需要考虑参数的顺序问题,便于日后维护.

  • 缺点是:较为麻烦.

  • 改进:使用SqlParamterSource接口的实现类BeanPropertySqlParameterSouce 最为参数,可以直接保存一个VO对象,符合于程序的设计使用

猜你喜欢

转载自blog.csdn.net/qq_43386754/article/details/88174135
今日推荐