Spring JdbcTemplate 简介,java 高级开发面试总结
Spring 对数据库的操作在 jdbc 上面做了深层次的封装,使用 spring 的注入功能,可以把 DataSource 注册到 JdbcTemplate 中。
JdbcTemplate 位于

中。其全限定命名为 org.springframework.jdbc.core.JdbcTemplate。要使用 JdbcTemplate 还需要一个

这个包含了事务和异常控制。
四、JdbcTemplate 提供的方法
1、execute 方法
可以用于执行任何 SQL 语句,一般用于执行 DDL 语句。
2、update 方法和 batchUpdate 方法
update 方法用于执行新增、修改、删除等语句;batchUpdate 方法用于执行批量处理相关语句。
3、query 方法及 queryForXXX 方法
用于执行查询相关语句。
4、call 方法
用于执行存储过程、函数相关语句。
五、案例分析
1、在 src 下新建一个属性配置文件

jdbc.user=root
jdbc.password=123456
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///test
我们通常将数据库的配置信息单独放到一个文件中,这样也为了方便后期维护。
2、配置 Spring 配置文件 applicationContext.xml
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
第一行代码:用来读取 db.properties 文件中的数据。
第二行代码:用来配置一个数据源,这里数据实现类来此 C3P0 中的一个属性类,其中属性的值就是来自于 db.properties。
第九行代码:配置一个 JdbcTemplate 实例,并注入一个 dataSource 数据源。
3、update()方法
(1)通过 update 插入数据
//启动 IoC 容器
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
//获取 IoC 容器中 JdbcTemplate 实例
JdbcTemplate jdbcTemplate=(JdbcTemplate) ctx.getBean("jdbcTemplate");
String sql="insert into user (name,deptid) values (?,?)";
int count= jdbcTemplate.update(sql, new Object[]{"caoyc",3});
System.out.println(count);
这里 update 方法,第二参可以为可变参数。在数据库中可以看到,数据以被正确插入

(2)通过 update 修改数据
String sql="update user set name=?,deptid=? where id=?";
jdbcTemplate.update(sql,new Object[]{"zhh",5,51});
(3)通过 update 删除数据
String sql="delete from user where id=?";
jdbcTemplate.update(sql,51);
4、batchUpdate()批量插入、更新和删除方法
批量插入
String sql="insert into user (name,deptid) values (?,?)";
List<Object[]> batchArgs=new ArrayList<Object[]>();
batchArgs.add(new Object[]{"caoyc",6});
batchArgs.add(new Object[]{"zhh",8});
batchArgs.add(new Object[]{"cjx",8});
jdbcTemplate.batchUpdate(sql, batchArgs);
batchUpdate 方法第二参数是一个元素为 Object[]数组类型的 List 集合。
5、从数据库中读取数据到实体对象
(1)先定义一个 User 实体类
package com.proc;
public class User {
private Integer id;
private String name;
private Integer deptid;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getDeptid() {
return deptid;
}
public void setDeptid(Integer deptid) {
this.deptid = deptid;
}
public String toString() {
return "User [id=" + id + ", name=" + name + ", deptid=" + deptid + "]";
}
}
(2)读取单个对象
String sql="select id,name,deptid from user where id=?";
RowMapper<User> rowMapper=new BeanPropertyRowMapper<User>(User.class);
User user= jdbcTemplate.queryForObject(sql, rowMapper,52);
System.out.println(user);
输出结果
User [id=52, name=caoyc, deptid=6]
注意:
使用 BeanPropertyMapper 要求 sql 数据查询出来的列和实体属性需要一一对应。如果数据中列名和属性名不一致,在 sql 语句中需要用 as 重新取一个别名。
使用 JdbcTemplate 对象不能获取关联对象
(3)读取多个对象
String sql="select id,name,deptid from user";
RowMapper<User> rowMapper=new BeanPropertyRowMapper<User>(User.class);
List<User> users= jdbcTemplate.query(sql, rowMapper);
for (User user : users) {
System.out.println(user);
评论