spring对JDBC的支持之JdbcTemplate简介

mac2024-06-03  33

目录

前言

1、方法概述

1.1、用 sql 语句和参数更新数据库

1.2、批量更新数据库

1.3、查询单行

1.4、查询多行

1.5、单值查询

2、代码演练

2.1、bean文件配置

2.2、mysql数据表结构

2.3、表属性类

2.4、jdbc代码实现

3、总结


前言

      为了使 JDBC 更加易于使用, Spring 在 JDBC API 上定义了一个抽象层, 以此建立一个 JDBC 存取框架. 作为 Spring JDBC 框架的核心, JDBC 模板的设计目的是为不同类型的 JDBC 操作提供模板方法. 每个模板方法都能控制整个过程, 并允许覆盖过程中的特定任务. 通过这种方式, 可以在尽可能保留灵活性的情况下, 将数据库存取的工作量降到最低.

1、方法概述

1.1、用 sql 语句和参数更新数据库

1.2、批量更新数据库

1.3、查询单行

便利的BeanPropertyRowMapper 实现

1.4、查询多行

1.5、单值查询

 

2、代码演练

2.1、bean文件配置

application_context.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" 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-4.0.xsd"> <!-- 外部属性文件 --> <context:property-placeholder location="classpath:db.properties"/> <bean id="dataSource" class ="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="${jdbc.user}"/> <property name="password" value="${jdbc.password}"/> <property name="driverClass" value="${jdbc.driverClass}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="initialPoolSize" value="${jdbc.initialPoolSize}"/> <property name="maxPoolSize" value="${jdbc.maxPoolSize}"/> <!-- <property name="user" value="root"></property> <property name="password" value="111111"></property> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/test_db?serverTimezone=UTC"></property> <property name="initialPoolSize" value="5"></property> <property name="maxPoolSize" value ="110"></property> --> </bean> <bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> </beans>

 外部依赖文件db.properties:

jdbc.user=root jdbc.password=111111 jdbc.driverClass=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/test_db?serverTimezone=UTC jdbc.maxPoolSize=120 jdbc.initialPoolSize=10

2.2、mysql数据表结构

2.3、表属性类

  Person.java:

package spring_jdbc_sample; import java.sql.Date; public class Person { private int id; private String name; private Date birthday; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } @Override public String toString() { return "TableElePerson [id=" + id + ", name=" + name + ", birthday=" + birthday + "]"; } } class PersonPropertis { private String name; private Date birthday; public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } @Override public String toString() { return "PersonPropertis [name=" + name + ", birthday=" + birthday + "]"; } }

2.4、jdbc代码实现

 TestMain.java:

package spring_jdbc_sample; import java.sql.Date; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import javax.sql.DataSource; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; public class TestMain { public static void main(String[] args) throws SQLException { // TODO Auto-generated method stub ApplicationContext ctx = new ClassPathXmlApplicationContext("application_context.xml"); DataSource ds = (DataSource) ctx.getBean("dataSource"); JdbcTemplate jt =(JdbcTemplate) ctx.getBean("jt"); String sql_cmd = null; //增、删、改均通过update方法执行 sql_cmd="insert into student(name) values(?)"; jt.update(sql_cmd,"wangjian"); sql_cmd="delete from student where name=?"; jt.update(sql_cmd,"wangjian"); sql_cmd="update student set birthday=? where name = 'wangquan'"; jt.update(sql_cmd,"2019-11-2"); /*批量执行 增、删、改 操作 * batchArgs:为object[]的List类型,修改单条记录多个参数需要一个object[]的数组 */ // sql_cmd="insert into student values(?,?,?)"; // List<Object[]> batchArgs= new ArrayList<Object[]>(); // batchArgs.add(new Object[]{10,"aa","2019-11-2"}); // batchArgs.add(new Object[]{11,"bb","2019-11-2"}); // batchArgs.add(new Object[]{12,"cc","2019-11-2"}); // jt.batchUpdate(sql_cmd, batchArgs); //单记录查询 /* *queryForObject(String sql, Class<String> requiredType, @Nullable Object... args):用于查询单个的字段 *queryForObject(String sql, RowMapper<PersonPropertis> rowMapper, @Nullable Object... args):查询多字段 */ sql_cmd="select name from student where id = ?"; String name = jt.queryForObject(sql_cmd, String.class, 1); System.out.println(name); sql_cmd="select birthday from student where id = ?"; Date birthday = jt.queryForObject(sql_cmd, Date.class, 1); System.out.println(birthday); //多字段解析 sql_cmd="select name,birthday from student where id = ?"; RowMapper<PersonPropertis> rowMapper = new BeanPropertyRowMapper<>(PersonPropertis.class); PersonPropertis person_part_info =jt.queryForObject(sql_cmd, rowMapper, 1); System.out.println(person_part_info); //批量查询 sql_cmd="select name,birthday from student where id > ?"; List<PersonPropertis> persons =jt.query(sql_cmd, rowMapper,1); System.out.println(persons); } }

3、总结

       每次使用都创建一个 JdbcTemplate 的新实例, 这种做法效率很低下.

      JdbcTemplate 类被设计成为线程安全的, 所以可以再 IOC 容器中声明它的单个实例, 并将这个实例注入到所有的 DAO 实例中.

      JdbcTemplate 也利用了 Java 1.5 的特定(自动装箱, 泛型, 可变长度等)来简化开发

      Spring JDBC 框架还提供了一个 JdbcDaoSupport 类来简化 DAO 实现. 该类声明了 jdbcTemplate 属性, 它可以从 IOC 容器中注入, 或者自动从数据源中创建.

最新回复(0)