JdbcTemplate概述
他是spring框架提供的一个对象,是对原始繁琐的JDBC API对象的简单封装。spring框架为我们提供了很多的操作模板类。例如:操作关系型数据库的JdbcTemplate和HibernateTemplate,操作nosql数据库的RedisTemplate,操作消息队列的JmsTemplate等等。
JdbcTemplate开发步骤
- 导入spring-jdbc和spring-tx
- 创建数据库表和实体
- 创建JdbcTemplate对象
- 执行数据库操作
@Test
public void test1() throws PropertyVetoException {
//创建数据源对象
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/ssm?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8");
dataSource.setUser("root");
dataSource.setPassword("root");
JdbcTemplate jdbcTemplate = new JdbcTemplate();
//设置数据源对象,知道数据库在哪里
jdbcTemplate.setDataSource(dataSource);
int i = jdbcTemplate.update("insert into account values(10,?,?)", "武大郎", 300);
System.out.println(i);
List<Account> accountList = jdbcTemplate.query("select * from account", new BeanPropertyRowMapper<Account>(Account.class));
System.out.println(accountList);
}
Spring产生JdbcTemplate对象
我们可以将JdbcTemplate的创建权交给spring,将数据源DataSource的创建权也交给spring,在spring容器内部将数据源DataSource注入到JdbcTemplate模板对象中,配置如下:
<!--数据源DataSource-->
<bean id="dataSouce" class="com.mchange.v2.c3p0.ComPooledDataSource">
<property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssm"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
事务控制
编程式事务控制
PlatformTransactionManager接口是spring的事务管理器,它在里面提供了我们常用的操作事务的方法。
- TransactionStatus getTransaction(TransactionDefination defination) 获取事务的状态信息
- void commit(TransactionStatus status) 提交事务
- voidrollback(TransactionStatus status) 回滚事务
TransactionDefinition 是事务的定义信息对象,里面有如下方法:
int getIsolationLever() 获取事务的隔离级别
int getPropogationBehavior() 获取事务的传播行为
int getTimeout() 获得超时时间
boolean isReadOnly() 是否只读
TransactionStatus接口提供的是事务具体的运行状态,方法介绍如下:
- boolean hasSavepoint() 是否存储回滚点
- boolean isCompleted() 事务是否完成
- boolean isNewTransaction() 是否是新事务
- boolean isRollbackOnly() 事务是否回滚
基于XML的声明式事务控制
什么是声明式事务控制
spring的声明式事务顾名思义就是采用声明的方式来处理事务。这里所说的声明,就是指在配置文件中声明,用在Spring配置文件中声明式的处理事务来代替代码式的处理事务
声明式事务处理的作用
- 事务管理不侵入开发的组件。具体来说,业务逻辑对象就不会意识到正在事务管理中,事实上也应该如此,因为事务管理是属于系统层面的服务,而不是业务逻辑的一部分,如果想要改变事务管理策划的话,也只需要在定义文件中重新配置即可
- 在不需要事务管理的时候,只要在设定文件上修改一下,即可移除事务管理,无需改代码重新编译,这样维护起来及其方便
入门配置
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
">
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="accountDao" class="com.zyh.dao.impl.AccountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<!--目标对象 内部的方法就是切点-->
<bean id="accountService" class="com.zyh.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
<!--配置平台事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--通知 事务的增强-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*"/><!--*代表所有方法
name:切点方法的名称
isolation:事务的隔离级别
propogation:事务的传播行为
timeout:超时时间
read-only:是否只读
-->
</tx:attributes>
</tx:advice>
<!--配置事务的aop织入-->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.zyh.service.impl.*.*(..))"></aop:advisor>
</aop:config>
</beans>
基于注解的事务控制
在需要事务控制的类或方法上加@Transactional代替
<!--配置平台事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--事务的注解驱动 加上这个配置@Transactional 才会起作用-->
<tx:annotation-driven transaction-manager="transactionManager"/>