JdbcTemplate与声明式事务
1、JdbcTemplate
1.1、概述
前面我们已经学习了 Spring 中的Core Container
核心部分和AOP
、Aspects
等面向切面编程部分,接下来就是Data Access/Integration
即数据访问和集成部分
Spring 既可以单独使用,也可以集成其他框架,如Hibernate
、MyBatis
等。除此之外,其中对于JDBC
也做了封装,即本章节的JdbcTemplate
,用它可以比较方便地对数据库进行增删改查等操作
总结一下:
JdbcTemplate
就是 Spring 框架对JDBC
技术进行的二次封装模板,能够简化对数据库的操作
1.2、准备工作
步骤预览
- 1)引入相关
jar
包 - 2)Spring 配置文件配置
Druid
连接池信息 - 3)配置
JdbcTemplate
对象,注入dataSource
- 4)创建 Service 和 Dao 类,在 Dao 类中注入
JdbcTemplate
对象
详细操作
- 1)引入相关
jar
包(或依赖)druid
mysql-connector-java
spring-jdbc
spring-orm
spring-tx
- 2)Spring 配置文件配置
Druid
连接池信息
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${mysql.driverClassName}"/>
<property name="url" value="${mysql.url}"/>
<property name="username" value="${mysql.username}"/>
<property name="password" value="${mysql.password}"/>
</bean>
沿用之前章节的Jdbc.properties
配置信息,但稍作修改
mysql.driverClassName=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql:///book_db
mysql.username=root
mysql.password=root
- 3)配置
JdbcTemplate
对象,注入dataSource
<!--配置JdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--属性注入dataSource-->
<property name="dataSource" ref="dataSource"></property>
</bean>
为何使用属性注入?
JdbcTemplate
虽然含有DataSource
的有参构造,但其调用了setDataSource()
方法
这个方法是在其父类中定义了的
- 4)创建 Service 和 Dao 类,在 Dao 类中注入
JdbcTemplate
对象
Dao 类
public interface BookDao {
}
@Repository
public class BookDaoImpl implements BookDao {
@Autowired
private JdbcTemplate jdbcTemplate;
}
Service 类
@Service
public class BookService {
@Autowired
private BookDao bookDao;
}
别忘了开启注解扫描
<!--开启注解扫描-->
<context:component-scan base-package="com.vectorx.spring5.s15_jdbctemplate"/>
配置文件整体结构
<?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.xsd">
<!--开启注解扫描-->
<context:component-scan base-package="com.vectorx.spring5.s15_jdbctemplate"/>
<!--配置dataSource-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${mysql.driverClassName}"/>
<property name="url" value="${mysql.url}"/>
<property name="username" value="${mysql.username}"/>
<property name="password" value="${mysql.password}"/>
</bean>
<!--配置JdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--属性注入dataSource-->
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
1.3、添加操作
步骤预览
- 1)创建数据库中
t_book
表对应的实体对象 - 2)编写 Service 和 Dao 代码,增加_添加图书_的功能逻辑
- 3)代码测试
详细操作
- 1)创建数据库中
t_book
表对应的实体对象
public class Book {