🦒看一个实际需求
实际需求: 如果程序员就希望使用spring框架来做项目, spring框架如何处理对数据库的操作呢?
- 方案1: 使用前面做项目开发的
JdbcUtils
类 - 方案2: 其实spring提供了一个操作数据库(表)功能强大的类
JdbcTemplate
. 我们可以同ioc
容器来配置一个jdbcTemplate
对象, 使用它来完成对数据库表的各种操作.
🦒官方文档
官方文档: spring-framework-5.3.8\docs\javadoc-api\index.html
🦒基本介绍
1.通过Spring
可以配置数据源, 从而完成对数据表的操作
2.JdbcTemplate
是Spring
提供的访问数据库的技术. 可以将JDBC
的常用操作封装为模板方法. [JdbcTemplate
类图]
🦒使用实例
📕需求说明
我们使用spring
的方式来完成 JdbcTemplate
配置和使用
📕代码演示
找到前面我们搭建的spring项目, 在此项目上学习 JdbcTemplate
.
2.创建数据库 spring
和表 monster
1)管理员打开cmd窗口, 开启数据库服务. 别忘了, 这很重要
安装Mysql5.7
2)sql
代码
-- 创建数据库
CREATE DATABASE spring;
USE spring;
-- 创建表
CREATE TABLE monster (
id INT UNSIGNED PRIMARY KEY,
`name` VARCHAR(64) NOT NULL DEFAULT '',
skill VARCHAR(64) NOT NULL DEFAULT ''
)CHARSET=utf8;
INSERT INTO monster VALUES(100, '孙悟空', '金箍棒');
INSERT INTO monster VALUES(200, '红孩儿', '三昧真火');
INSERT INTO monster VALUES(300, '铁扇公主', '芭蕉扇');
3.创建配置文件 src/jdbc.properties
JdbcTemplate
会使用到DataSource
, 而DataSource
可以拿到连接去操作数据库
jdbc.user=root
jdbc.pwd=zzw
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring
4.创建配置文件 src/JdbcTemplate_ioc.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 https://www.springframework.org/schema/context/spring-context.xsd">
<!--引入外部的jdbc.properties文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置数据源对象-DataSource-->
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
<!--给数据源对象配置属性值-->
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.pwd}"/>
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
</bean>
</beans>
5.新建测试类com.zzw.spring.test.JdbcTemplateTest
public class JdbcTemplateTest {
@Test
public void testDataSourceByJdbcTemplate() {
//获取容器
ApplicationContext ioc =
new ClassPathXmlApplicationContext("JdbcTemplate_ioc.xml");
//在这里打个断点
System.out.println("ok");
}
}
public class JdbcTemplateTest {
@Test
public void testDataSourceByJdbcTemplate() {
//获取容器
ApplicationContext ioc =
new ClassPathXmlApplicationContext("JdbcTemplate_ioc.xml");
//因为ComboPooledDataSource类实现了DataSource接口, 所以我们可以用接口类型来获取 comboPooledDataSource对象
DataSource dataSource = ioc.getBean(DataSource.class);
Connection connection = dataSource.getConnection();
//获取到connection=com.mchange.v2.c3p0.impl.NewProxyConnection@2cd2a21f
System.out.println("获取到connection=" + connection);
connection.close();
System.out.println("ok");
}
}
6.配置JdbcTemplate_ioc.xml
, 将数据源分配给JdbcTemplate bean
<?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 https://www.springframework.org/schema/context/spring-context.xsd">
<!--引入外部的jdbc.properties文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置数据源对象-DataSource-->
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
<!--给数据源对象配置属性值-->
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.pwd}"/>
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
</bean>
<!--配置JdbcTemplate对象-->
<!--JdbcTemplate会使用到DataSource, 而DataSource可以拿到连接去操作数据库-->
<bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
<!--给JdbcTemplate对象配置dateSource-->
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
7.修改JdbcTemplateTest.java
, 通过JdbcTemplate
对象完成添加一个新的 monster
public class JdbcTemplateTest {
@Test
public void addDataByJdbcTemplate() {
//获取到容器
ApplicationContext ioc =
new ClassPathXmlApplicationContext("JdbcTemplate_ioc.xml");
//获取JdbcTemplate对象, 这里我们先 debug一下
System.out.println("ok");
}
}
public class JdbcTemplateTest {
@Test
public void addDataByJdbcTemplate() {
//获取到容器
ApplicationContext ioc =
new ClassPathXmlApplicationContext("JdbcTemplate_ioc.xml");
//获取JdbcTemplate对象 [按类型获取]
JdbcTemplate jdbcTemplate = ioc.getBean(JdbcTemplate.class);
//1.添加方式①
//String sql = "insert into monster values(400, '陆小千', '魔幻手机')";
//jdbcTemplate.execute(sql);
//2.添加方式② ?占位符, 可以防止sql注入
String sql = "insert into monster values(?, ?, ?)";
//affected表示 执行后表受影响的行数
int affected = jdbcTemplate.update(sql, 500, "金角大王", "紫金红葫芦");
System.out.println("add success afftected=" + affected);
}
}
8.修改JdbcTemplateTest.java
, 通过JdbcTemplate
对象完成更新一个 monster
的 skill
public class JdbcTemplateTest {
//测试通过JdbcTemplate对象完成修改数据
@Test
public void updateDataByJdbcTemplate() {
//获取到容器
ApplicationContext ioc =
new ClassPathXmlApplicationContext("JdbcTemplate_ioc.xml");
//获取JdbcTemplate对象
JdbcTemplate jdbcTemplate = ioc.getBean(JdbcTemplate.class);
//组织SQL
String sql = "update monster set skill = ? where id = ?";
//假使修改后的数据和修改前的数据一样, 也认为是修改成功了一条语句, affected返回 1, 因为它没有判断要修改的数据
int affected = jdbcTemplate.update(sql, "美人计", 300);
System.out.println("update ok affected=" + affected);
}
}
9.修改JdbcTemplateTest.java
, 通过JdbcTemplate
对象完成批量添加两个 monster
白蛇精和猪八戒
public class JdbcTemplateTest {
//测试通过JdbcTemplate对象完成批量添加数据
//这里有一个使用API的技巧
/**
* 说明
* 1.对于某些类, 有很多API, 使用的步骤
* 2.使用技巧: (1)先确定API名字 (2)根据API提供相应的参数 [组织参数]
* (3)把自己的调用思路清晰 (4)根据API, 可以推测类的用法和功能
*/
@Test
public void addBatchDataByJdbcTemplate() {
//获取到容器
ApplicationContext ioc =
new ClassPathXmlApplicationContext("JdbcTemplate_ioc.xml");
//获取JdbcTemplate对象
JdbcTemplate jdbcTemplate = ioc.getBean(JdbcTemplate.class);
//1.先确定, 猜测API名称 update -> batchUpdate [如果出现问题,再重新找]
//public int[] batchUpdate(String sql, List<Object[]> batchArgs){}
//2.准备参数 [构建实参]
String sql = "insert into monster values(?, ?, ?)";
List<Object[]> batchArgs = new ArrayList<>();
batchArgs.add(new Object[]{600, "鼠鼠", "偷吃粮食"});
batchArgs.add(new Object[]{700, "猫猫", "抓老鼠"});
//3.调用
//说明: 返回结果是一个数组, 每个元素对应上面的sql语句对表的影响记录数
int[] ints = jdbcTemplate.batchUpdate(sql, batchArgs);
//输出
for (int anInt : ints) {
System.out.println("anInt=" + anInt);
}
System.out.println("batch add ok~");
}
}
10.查询 id=100
的 monster
并封装到 Monster
实体对象
1)创建Monster实体类, 跳转
2)这里有一个知识点: 给字段起别名, 应对应实体类的属性
. 我们在前面的家居购项目中遇到过
public class JdbcTemplateTest {
/**
* 查询id=100的monster对象封装到Monter实体对象[在实际开发中非常有用]
*/
@Test
public void selectDataByJdbcTemplate() {
//获取到容器
ApplicationContext ioc =
new ClassPathXmlApplicationContext("JdbcTemplate_ioc.xml");
//获取JdbcTemplate对象
JdbcTemplate jdbcTemplate = ioc.getBean(JdbcTemplate.class);
//1.确定API query -> queryForObject
//public <T> T queryForObject(String sql, RowMapper<T> rowMapper, @Nullable Object... args)
//2.准备参数
String sql = "SELECT id AS monsterId, `name`, skill FROM monster WHERE id = ?";
//使用了RowMapper 接口来对返回的数据, 进行一个封装 底层使用的反射 -> setter方法
//细节: 你查询的记录的表的字段需要和 Monster对象的字段名保持一致
RowMapper<Monster> rowMapper = new BeanPropertyRowMapper<>();
//3.调用
Monster monster = jdbcTemplate.queryForObject(sql, rowMapper, 100);
//输出
System.out.println("monster=" + monster);
System.out.println("query ok~");
}
}
3)结果报错: java.lang.IllegalStateException: Mapped class was not specified
4)修正代码
RowMapper<Monster> rowMapper = new BeanPropertyRowMapper<>(Monster.class);
5)运行结果
11.查询 id>=200
的 monster
并封装到 Monster
实体对象
public class JdbcTemplateTest {
/**
* 查询id>=200的monster并封装到Monster实体对象
*/
@Test
public void selectMulDataByJdbcTemplate() {
//获取到容器
ApplicationContext ioc =
new ClassPathXmlApplicationContext("JdbcTemplate_ioc.xml");
//获取JdbcTemplate对象
JdbcTemplate jdbcTemplate = ioc.getBean(JdbcTemplate.class);
//1.确定API query
//public <T> List<T> query(String sql, RowMapper<T> rowMapper, Object... args)
//2.准备参数
String sql = "SELECT id AS monsterId, `name`, skill FROM monster WHERE id >= ?";//这个?填进入也可以
RowMapper<Monster> rowMapper = new BeanPropertyRowMapper<>(Monster.class);
//3.调用
List<Monster> monsterList = jdbcTemplate.query(sql, rowMapper, 200);
//输出
for (Monster monster : monsterList) {
System.out.println("monster=" + monster);
}
}
}
12.查询返回结果只有一行一列的值, 比如查询 id=100
的怪物名
public class JdbcTemplateTest {
/**
* 查询返回结果只有一行一列的值
*/
@Test
public void selectScalarByJdbcTemplate() {
//获取到容器
ApplicationContext ioc =
new ClassPathXmlApplicationContext("JdbcTemplate_ioc.xml");
//获取JdbcTemplate对象
JdbcTemplate jdbcTemplate = ioc.getBean(JdbcTemplate.class);
//1.确定API query -> queryForObject
//public <T> T queryForObject(String sql, Class<T> requiredType, @Nullable Object... args)
//2.准备参数
String sql = "SELECT `name` FROM monster WHERE id = ?";
//Class<T> requiredType 表示你返回的单行单列的数据类型
//3.调用
String name = jdbcTemplate.queryForObject(sql, String.class, 100);
//输出
System.out.println("name=" + name);
System.out.println("ok~");
}
}
13.使用 Map
传入具名参数完成操作, 比如添加 螃蟹精 name
就是具名参数形式, 需要使用 NamedParameterJdbcTemplate
类
配置JdbcTemplate_ioc.xml
, 使用到NamedParameterJdbcTemplate
<?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 https://www.springframework.org/schema/context/spring-context.xsd">
<!--配置NamedParameterJdbcTemplate对象-->
<bean class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate" id="namedParameterJdbcTemplate">
<!--通过构造器, 设置数据源-->
<constructor-arg name="dataSource" ref="dataSource"/>
</bean>
</beans>
public class JdbcTemplateTest {
/**
* 使用Map传入具名参数完成操作, 比如添加
*/
@Test
public void testDataByNamedParameterJdbcTemplate() {
//获取到容器
ApplicationContext ioc =
new ClassPathXmlApplicationContext("JdbcTemplate_ioc.xml");
//得到NamedParameterJdbcTemplate bean
NamedParameterJdbcTemplate namedParameterJdbcTemplate = ioc.getBean(NamedParameterJdbcTemplate.class);
//1.确定API update
//public int update(String sql, Map<String, ?> paramMap)
//2.准备参数 [:my_id, :name, :skill] 要求按照规定的名字来设置参数
String sql = "insert into monster values(:id, :name, :skill)";
Map<String, Object> paramMap = new HashMap<>();
//给paramMap填写数据
paramMap.put("id", 800);
paramMap.put("name", "二郎神");
paramMap.put("skill", "哮天犬");
//3.调用
int affected = namedParameterJdbcTemplate.update(sql, paramMap);
//输出
System.out.println("add ok affected=" + affected);
}
}
14.使用 sqlparametersource
来封装具名参数, 还是添加一个 Monster
狐狸精
public class JdbcTemplateTest {
/**
* 使用sqlparamtersource 来封装具名参数, 还是添加一个Monster
*/
@Test
public void operDataBySqlparametersource() {
//获取到容器
ApplicationContext ioc =
new ClassPathXmlApplicationContext("JdbcTemplate_ioc.xml");
//得到NamedParameterJdbcTemplate bean
NamedParameterJdbcTemplate namedParameterJdbcTemplate = ioc.getBean(NamedParameterJdbcTemplate.class);
//1.确定API query
//public int update(String sql, SqlParameterSource paramSource)
//public BeanPropertySqlParameterSource(Object object)
//2.准备参数
String sql = "insert into monster values(:id, :name, :skill)";
Monster monster = new Monster(900, "妲己", "魅惑");
SqlParameterSource sqlParameterSource = new BeanPropertySqlParameterSource(monster);
//3.调用
int affected = namedParameterJdbcTemplate.update(sql, sqlParameterSource);
//输出
System.out.println("add ok affected=" + affected);
}
}
结果报错 No value supplied for the SQL parameter 'id': Invalid property 'id' of bean class [com.zzw.spring.bean.Monster]: Bean property 'id' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
修改代码
String sql = "insert into monster values(:monterId, :name, :skill)";
15.Dao 对象中使用 Jdbctemplate
完成对数据的操作
1)创建com.zzw.spring.jdbctemplate.dao.MonsterDao.java
@Repository //将MonsterDao注入到spring容器
public class MonsterDao {
//注入一个属性
@Resource
private JdbcTemplate jdbcTemplate;
//完成保存任务
public void save(Monster monster) {
//组织SQL
String sql = "insert into monster values(?, ?, ?)";
int affteced =
jdbcTemplate.update(sql, monster.getMonsterId(), monster.getName(), monster.getSkill());
System.out.println("affected=" + affteced);
}
}
2)修改src/JdbcTemplate_ioc.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 https://www.springframework.org/schema/context/spring-context.xsd">
<!--配置要扫描的包-->
<context:component-scan base-package="com.zzw.spring.jdbctemplate.dao"/>
<!--引入外部的jdbc.properties文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置数据源对象-DataSource-->
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
<!--给数据源对象配置属性值-->
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.pwd}"/>
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
</bean>
<!--配置JdbcTemplate对象-->
<!--JdbcTemplate会使用到DataSource, 而DataSource可以拿到连接去操作数据库-->
<bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
<!--给JdbcTemplate对象配置dateSource-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置NamedParameterJdbcTemplate对象-->
<!--<bean class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate" id="namedParameterJdbcTemplate">
<!–通过构造器, 设置数据源–>
<constructor-arg name="dataSource" ref="dataSource"/>
</bean>-->
</beans>
3)修改JdbcTemplateTest.java
, 增加测试方法
public class JdbcTemplateTest {
//测试MonsterDao是否生效
@Test
public void monsterDaoSave() {
//获取到容器
ApplicationContext ioc =
new ClassPathXmlApplicationContext("JdbcTemplate_ioc.xml");
MonsterDao monsterDao = ioc.getBean(MonsterDao.class);
monsterDao.save(new Monster(1000, "女娲", "女娲补天"));
System.out.println("save ok~");
}
}