操作数据库,需要严谨,所以需要事务。
例如,在做转账的时候,如果不严谨,就会造成一方的钱减少了,另一方的钱却没增加;或者是一方的钱增加了,另一方的钱却没减少。
首先通过配置方式,来实现事务。
UserDao.java (数据库中已有数据)
public class UserDao{
//下面这个就是访问数据库的对象
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
/**
* 钱增加的方法
*/
public void addMoney(){
int k=1/0;
String sql="update t_money set money=money+1000 where name='李四'";
jdbcTemplate.update(sql);
}
/**
* 钱减少的方法
*/
public void minusMoney(){
String sql="update t_money set money=money-1000 where name='张三'";
jdbcTemplate.update(sql);
}
}
UserService.java
public class UserService{
private UserDao userDao=null;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
/**
* 转账这个方法
*/
public void zhuanzhang(){
userDao.minusMoney();
userDao.addMoney();
}
}
bean.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--配置我们的数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="acquireIncrement" value="2"></property>
<property name="maxPoolSize" value="100"></property>
<property name="minPoolSize" value="2"></property>
<property name="maxStatements" value="100"></property>
<!--连接数据库的信息 -->
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///test01"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<!--dao -->
<bean id="userDao" class="com.wc.transactionx.dao.UserDao"
p:jdbcTemplate-ref="jdbcTemplate"></bean>
<!--Service -->
<bean id="userService" class="com.wc.transactionx.service.UserService"
p:userDao-ref="userDao"></bean>
<!--配置访问数据库的类 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--下面进行事务的配置 -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置我们的事务增强 (配置如何使用我们的事务) -->
<tx:advice transaction-manager="txManager" id="txAdvice">
<tx:attributes>
<!--
read-only="false":事务的类型 (只读事务 、 读写事务 false读写事务)
isolation="DEFAULT":事务的隔离级别(默认使用数据库的就OK了)
no-rollback-for="Exception.class":遇到什么异常不回滚
propagation="REQUIRED":事务的传播行为
timeout="-1":事务的过期时间 -1 永不过期
-->
<tx:method name="*" timeout="-1" read-only="false"/>
</tx:attributes>
</tx:advice>
<!--配置我们的aop-->
<aop:config>
<!--配置的是切入点表达式-->
<aop:pointcut expression="(execution(* com.wc.transactionx.service.*.*(..)))" id="pt"/>
<!--应用我们的事物增强-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
</aop:config>
</beans>
Test.java
@Test
public void testDataBase() throws Exception {
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("com/wc/transactionx/bean.xml");
//获取我们的Service对象
UserService userService=context.getBean(UserService.class);
userService.zhuanzhang();
}
第二种是通过注解方式实现
bean.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--配置我们的数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="acquireIncrement" value="2"></property>
<property name="maxPoolSize" value="100"></property>
<property name="minPoolSize" value="2"></property>
<property name="maxStatements" value="100"></property>
<!--连接数据库的信息 -->
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///test01"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<!--配置访问数据库的类 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--下面进行事务的配置 -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--添加一个aop的注解扫描-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!--设置应用事务-->
<tx:annotation-driven transaction-manager="txManager"/>
<!--Spring中IOC/DI的注解扫描-->
<context:component-scan base-package="com.wc.transation"></context:component-scan>
</beans>
UserDao.java
@Repository
public class UserDao{
//下面这个就是访问数据库的对象
@Autowired
private JdbcTemplate jdbcTemplate;
/**
* 钱增加的方法
*/
public void addMoney(){
int k=1/0;
String sql="update t_money set money=money+1000 where name='李四'";
jdbcTemplate.update(sql);
}
/**
* 钱减少的方法
*/
@Transactional(propagation=Propagation.REQUIRED)//添加事务
public void minusMoney(){
String sql="update t_money set money=money-1000 where name='张三'";
jdbcTemplate.update(sql);
}
}
UserService.java
@Service
public class UserService{
@Autowired
private UserDao userDao=null;
/**
* 转账这个方法
*/
@Transactional(propagation=Propagation.REQUIRED)
public void zhuanzhang(){
userDao.minusMoney();
userDao.addMoney();
}
}
Test.java
public static void main(String[] args) {
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("com/wc/transation/bean.xml");
//获取我们的Service对象
UserService userService=(UserService) context.getBean("userService");
userService.zhuanzhang();
}