JdbcTemplate基本使用
JDBC已经能够满足大部分用户最基本的需求,但是在使用JDBC时,必须自己来管理数据库资源如:获取PreparedStatement,设置SQL语句参数,关闭连接等步骤。
JdbcTemplate是Spring对JDBC的封装,目的是使JDBC更加易于使用。JdbcTemplate是Spring的一部分。JdbcTemplate处理了资源的建立和释放。他帮助我们避免一些常见的错误,比如忘了总要关闭连接。他运行核心的JDBC工作流,如Statement的建立和执行,而我们只需要提供SQL语句和提取结果。 Spring源码地址:https://github.com/spring-projects/spring-framework 在JdbcTemplate中执行SQL语句的方法大致分为3类:
-
execute
:可以执行所有SQL语句,一般用于执行DDL语句。 -
update
:用于执行INSERT
、UPDATE
、DELETE
等DML语句。 -
queryXxx
:用于DQL数据查询语句。
JdbcTemplate使用步骤
-
准备DruidDataSource连接池
-
导入依赖的jar包
-
spring-beans-4.1.2.RELEASE.jar
-
spring-core-4.1.2.RELEASE.jar
-
spring-jdbc-4.1.2.RELEASE.jar
-
spring-tx-4.1.2.RELEASE.jar
-
com.springsource.org.apache.commons.logging-1.1.1.jar
-
-
创建
JdbcTemplate
对象,传入Druid
连接池 -
调用
execute
、update
、queryXxx
等方法
案例代码
public class Demo05 {
public static void main(String[] args) throws Exception {
// test01();
// test02();
// test03();
}
// JDBCTemplate添加数据
public static void test01() throws Exception {
JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSourceUtils.getDataSource());
String sql = "INSERT INTO product VALUES (NULL, ?, ?);";
jdbcTemplate.update(sql, "iPhone3GS", 3333);
jdbcTemplate.update(sql, "iPhone4", 5000);
jdbcTemplate.update(sql, "iPhone4S", 5001);
jdbcTemplate.update(sql, "iPhone5", 5555);
jdbcTemplate.update(sql, "iPhone5C", 3888);
jdbcTemplate.update(sql, "iPhone5S", 5666);
jdbcTemplate.update(sql, "iPhone6", 6666);
jdbcTemplate.update(sql, "iPhone6S", 7000);
jdbcTemplate.update(sql, "iPhone6SP", 7777);
jdbcTemplate.update(sql, "iPhoneX", 8888);
}
// JDBCTemplate修改数据
public static void test02() throws Exception {
JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSourceUtils.getDataSource());
String sql = "UPDATE product SET pname=?, price=? WHERE pid=?;";
int i = jdbcTemplate.update(sql, "XVIII", 18888, 10);
System.out.println("影响的行数: " + i);
}
// JDBCTemplate删除数据
public static void test03() throws Exception {
JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSourceUtils.getDataSource());
String sql = "DELETE FROM product WHERE pid=?;";
int i = jdbcTemplate.update(sql, 7);
System.out.println("影响的行数: " + i);
}
}
DataSourceUtils.getDataSource() 工具类在笔记二里面有
dbcTemplate的update
方法用于执行DML语句。同时还可以在SQL语句中使用?占位,在update方法的Object... args
可变参数中传入对应的参数。
JdbcTemplate查询-queryForObject返回String
API介绍
public <T> T queryForObject(String sql, Class<T> requiredType) 参数一:sql语句; 参数二:数据库表的的任意类型,String.class 或者 Long.class 引用数据类型,包装类 执行查询语句,返回一个指定类型的数据。
使用步骤
-
创建JdbcTemplate对象
-
编写查询的SQL语句
-
使用JdbcTemplate对象的queryForObject方法,并传入需要返回的数据的类型
-
输出结果
public static void test03() throws Exception {
String sql = "SELECT pname FROM product WHERE price=7777;";
JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSourceUtils.getDataSource());
String str = jdbcTemplate.queryForObject(sql, String.class);
System.out.println(str);
}
JdbcTemplate查询-queryForMap返回一个Map集合
API介绍
public Map<String, Object> queryForMap(String sql)
执行查询语句,将一条记录放到一个Map中。
public static void test04() throws Exception {
String sql = "SELECT * FROM product WHERE pid=?;";
JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSourceUtils.getDataSource());
Map<String, Object> map = jdbcTemplate.queryForMap(sql, 6);
System.out.println(map);
}
JdbcTemplate查询-queryForList返回一个List集合
API介绍
public List<Map<String, Object>> queryForList(String sql)
执行查询语句,返回一个List集合,List中存放的是Map类型的数据。
public static void test05() throws Exception {
String sql = "SELECT * FROM product WHERE pid<?;";
JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSourceUtils.getDataSource());
List<Map<String, Object>> list = jdbcTemplate.queryForList(sql, 8);
for (Map<String, Object> map : list) {
System.out.println(map);
}
}
JdbcTemplate查询-RowMapper返回自定义对象
Product 是一个Javabean对象来的,没有展示出来
API介绍
public <T> List<T> query(String sql, RowMapper<T> rowMapper)
执行查询语句,返回一个List集合,List中存放的是RowMapper指定类型的数据。
// query使用rowMap做映射返回一个对象
public static void test06() throws Exception {
JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSourceUtils.getDataSource());
// 查询数据的SQL语句
String sql = "SELECT * FROM product;";
List<Product> query = jdbcTemplate.query(sql, new RowMapper<Product>() {
@Override
public Product mapRow(ResultSet arg0, int arg1) throws SQLException {
Product p = new Product();
p.setPid(arg0.getInt("pid"));
p.setPname(arg0.getString("pname"));
p.setPrice(arg0.getDouble("price"));
return p;
}
});
for (Product product : query) {
System.out.println(product);
}
}
RowMapper的使用?
-
使用JdbcTemplate对象的query方法,并传入RowMapper匿名内部类
-
在匿名内部类中将结果集中的一行记录转成一个Product对象
JdbcTemplate查询-BeanPropertyRowMapper返回自定义对象
API介绍
public <T> List<T> query(String sql, RowMapper<T> rowMapper) 执行查询语句,返回一个List集合,List中存放的是RowMapper指定类型的数据。
public class BeanPropertyRowMapper<T> implements RowMapper<T> BeanPropertyRowMapper类实现了RowMapper接口
// query使用BeanPropertyRowMapper做映射返回对象
public static void test07() throws Exception {
JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSourceUtils.getDataSource());
// 查询数据的SQL语句
String sql = "SELECT * FROM product;";
List<Product> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Product.class));
for (Product product : list) {
System.out.println(product);
}
}
BeanPropertyRowMapper的使用?
List<Product> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Product.class));