JdbcTemplate
JdbcTemplate是spring对JDBC操作的进一步封装,下面就通过简单的CRUD来了解JdbcTemplate如何操作,连接池直接用的spring内置的连接池DriverManagerDataSource
首先配置JdbcTemplate和数据源
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/spring?serverTimezone=GMT%2B8"/>
<property name="username" value="root"/>
<property name="password" value="1001101"/>
</bean>
获得JdbcTemplate对象
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
JdbcTemplate jdbcTemplate = applicationContext.getBean("jdbcTemplate",JdbcTemplate.class);
CRUD操作
保存:
jdbcTemplate.update("insert into account (name,money) values (?,?)","999",2345);
查询所有:
List<Account> accounts = jdbcTemplate.query("select *from account where money > ?", new BeanPropertyRowMapper<>(Account.class),1000);
for (Account account : accounts) {
System.out.println(account);
}
查询一个:
List<Account> accounts = jdbcTemplate.query("select *from account where id = ?", new BeanPropertyRowMapper<>(Account.class),6);
System.out.println(accounts.get(0));
更新:
jdbcTemplate.update("update account set name = ? ,money = ? where id = ?","bbb","6789",5);
删除:
jdbcTemplate.update("delete from account where id = ?",7);
事务控制
事务是什么
事务是确保数据安全性的基础,事务确保操作要么都成功,要么都失败,不会存在一半的操作成功了另一半的操作失败了,例如银行转账,A转1000给B,A转的过程中,系统出现了错误,A账户的钱已经扣,B却没有收到钱,这时转账失败就需要事务的回滚,回到最初没有转账时的状态,确保资金的正常,事务遵从ACID原则,即原子性、一致性、隔离性和持久性
spring中的事务
spring提供了对事务的支持,在spring中主要有两种方式使用事务
- 编程式事务控制
- 声明式事务控制
spring为了方便处理事务,提供了事务管理器,对事务的控制归根到底是通过事务管理器进行控制,在spring中所有的事务控制必须要有事务管理器。这里只介绍声明式事务控制
spring中基于XML的声明式事务控制
配置步骤
在XML中配置事务,需要导入事务的约束
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
- 配置事务管理器
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/spring?serverTimezone=GMT%2B8"/>
<property name="username" value="root"/>
<property name="password" value="1001101"/>
</bean>
- 配置事务的通知
- 配置事务的属性 在tx:advice标签的内部
事务的属性:
isolation:指定事务的隔离级别
propagation:指定事务的传播行为,默认值是REQUIRED ,表示一定会有事务,查询方法可选择SUPPORTS
read-only:指定是否只读,默认值为false,表示读写
timeout:指定事务的超时时间,默认值为-1,表示永不超时,如果指定数值,则以秒为单位
rollback-for:指定一个异常,当产生该异常时,事务回滚,产生其他异常时,事务不回滚,没有默认值,表示任何异常都回滚
no-rollback-for:指定一个异常,当产生该异常时,事务不回滚,产生其他异常时,事务回滚,没有默认值,表示任何异常都回滚
tx:advice标签的属性
id:给事务通知起一个唯一标识
transaction-manager:给事务通知提供一个事务管理器的引用
<!--配置事务的通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="transfer" propagation="REQUIRED" read-only="false"/>
</tx:attributes>
</tx:advice>
3.配置通用切入点表达式和建立事务通知和切入点表达式的对应关系
<!--配置AOP-->
<aop:config>
<!--通用切入点表达式-->
<aop:pointcut id="pCut" expression="execution(* com..lb.service.impl.*.*(..))"/>
<!--建立切入点和通知的对应关系-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pCut"/>
</aop:config>
spring中基于注解的声明式事务控制
配置步骤
- 配置事务管理器,这步与上面的相同,不多赘述
- 开启spring对注解事务的支持
<!--开启注解事务的支持-->
<tx:annotation-driven transaction-manager="transactionManager"/>
- 在需要事务支持的地方使用@Transactional注解
有人可能会问为什么没有事务属性的配置,这是因为在@Transactional注解中,已经为这些属性赋上了默认值,如果有其他需求,在针对的修改即可
spring纯注解的事务控制
步骤与上面的相同,只是加上了一个配置类来代替XML配置
@Configuration
@ComponentScan("com.lb")
@EnableTransactionManagement//开启事务注解支持
@Import({JdbcConfig.class,TransactionConfig.class})
public class SpringConfig {
}
完整的测试代码在我的GitHub