步骤:
引入jar包
spring-core相关jar包
spring-jdbc相关jar包
spring-aop相关jar包
xml方式实现声明式事务管理:
先创建一个Dept类
package cn.qblank.a_tx;
public class Dept {
private int deptId;
private String deptName;
public int getDeptId() {
return deptId;
}
public void setDeptId(int deptId) {
this.deptId = deptId;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
}
package cn.qblank.a_tx;
import org.springframework.jdbc.core.JdbcTemplate;
public class DeptDao {
//容器注入jdbcTemplate对象
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void save(Dept dept){
String sql = "insert into dept(deptName) value(?) ";
jdbcTemplate.update(sql,dept.getDeptName());
}
}
DeptService类
package cn.qblank.a_tx;
public class DeptService {
//注入IOC容器
private DeptDao deptDao;
public void setDeptDao(DeptDao deptDao) {
this.deptDao = deptDao;
}
public void save(Dept dept){
deptDao.save(dept);
//发生异常,全部回滚
int i = 1/0;
}
}
jdbc.user=root
jdbc.password=root
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///day09
jdbc.initPoolSize=5
jdbc.maxPoolSize=10
jdbc.maxStatements=100
jdbc.acquireIncrement=2
接下来就是关键的一部,使用spring配置连接配置信息,注入相关实例,并进行声明事务管理配置
package cn.qblank.a_tx;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("cn/qblank/a_tx/bean.xml");
Dept dept = new Dept();
dept.setDeptName("软件测试部");
DeptService deptService = (DeptService) ac.getBean("deptService");
deptService.save(dept);
System.out.println("添加成功");
ac = null;
}
}
数据库信息
由此看出,事务发生了回滚。
使用注解方式实现声明式事务管理
实体类对象和上面一样
接下来编写DeptDao类
package cn.qblank.anno;
import javax.annotation.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
@Repository("deptDao")
public class DeptDao {
//容器注入jdbcTemplate对象
@Resource(name="jdbcTemplate")
private JdbcTemplate jdbcTemplate;
public void save(Dept dept){
String sql = "insert into dept(deptName) value(?) ";
jdbcTemplate.update(sql,dept.getDeptName());
}
}
package cn.qblank.anno;
import javax.annotation.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Repository("logDao")
public class LogDao {
@Resource(name="jdbcTemplate")
private JdbcTemplate jdbcTemplate;
@Transactional(propagation = Propagation.REQUIRED)
public void insertLog(){
jdbcTemplate.update("insert into t_deptLog(logName) value('插入一条dept表!!!')");
}
}
package cn.qblank.anno;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Service
public class DeptService {
//注入IOC容器
@Resource
private DeptDao deptDao;
@Resource
private LogDao logDao;
/**
* 事务控制
* @param dept
*/
public void saveLog(){
logDao.insertLog();
}
/**
* 保存
* @param dept
*/
@Transactional(
readOnly= false, //设置只读属性
timeout = -1, //设置不限时
isolation = Isolation.DEFAULT, //隔离级别:默认
propagation = Propagation.REQUIRED //数据库的传播行为
)
public void save(Dept dept){
deptDao.save(dept);
saveLog();
}
}
首先弹出错误信息
接下来我们看看数据库是否发生回滚
可以发现数据库的数据并没有发生变化,所以我们回滚成功了!!!