栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring + Mybatis+事务

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring + Mybatis+事务

SM

配置

SqlMapConfig.xml


    
    
        
    

mapper.xml




    
    
    
    
        
        
        
        
    
    
    
        
        
        
        
        
        
    
    
    
        
    

service




    
    
    
    
    

    
    
        
        
     

    
    

trans,事务配置




    
    
    
    
    

    
    
        
        
     

    
    

jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123


Mapper
AccountsMapper

public interface AccountsMapper {

    //增加帐户
    int save(Accounts accounts);
}

AccountsMapper.xml



    
    
        insert into accounts values(#{aid},#{aname},#{acontent})
    

UserMapper

public interface UsersMapper {
    int insert(Users users);
}

UserMapper.xml



    
    
        insert into users values(#{userid},#{username},#{upass})
    

pojo
Accounts

public class Accounts {
    private Integer aid;
    private String aname;
    private String acontent;
}

Users

public class Users {
    private Integer userid;
    private String username;
    private String upass;
}

service
AccountsService

public interface AccountsService {
    int save(Accounts accounts);
}

UserService

public interface UsersService {
    //增加用户
    int insert(Users users);
}

service.Impl
AccountsServiceImpl

@Service
public class AccountsServiceImpl implements AccountsService {
    //一定会有数据访问层的对象
    @Autowired
    AccountsMapper accountsMapper;
    @Override
    public int save(Accounts accounts) {
        int num = 0;
        num = accountsMapper.save(accounts);
        System.out.println("增加帐户成功!num="+num);
        //手工抛出异常
        System.out.println(1/0);
        return num;
    }
}

UserServiceImpl

@Service  //交给Spring去创建对象
public class UsersServiceImpl implements UsersService {
    //切记切记:在所有的业务逻辑层中一定会有数据访问层的对象
    
    @Autowired
    UsersMapper usersMapper;

    @Autowired
    AccountsService accountsService;

    @Override
    public int insert(Users users) {
        int num = usersMapper.insert(users);
        System.out.println("用户增加成功!num="+num);

        //调用帐户的增加操作,调用的帐户的业务逻辑层的功能
        num = accountsService.save(new Accounts(300,"王五","帐户好的呢!"));
        return num;
    }
}

测试

    @Test
    public void testTrans(){
        //创建容器并启动
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext_trans.xml");
        //取出UsersServiceImpl
        UsersService uService = (UsersService) ac.getBean("usersServiceImpl");
        int num = uService.insert(new Users(100,"张三","123"));
        System.out.println(num);
    }
添加事务 基于注解的事务添加步骤

1)在applicationContext_service.xml文件中添加事务管理器

	
        
        
     

2)在applicationContext_service.xml文件中添加事务的注解驱动


3)在业务逻辑的实现类上添加注解@Transactional(propagation = Propagation.REQUIRED)
REQUIRED表示增删改操作时必须添加的事务传播特性

@Transactional注解参数详解
	@Transactional(propagation = Propagation.REQUIRED,//事务的传播特性
	        noRollbackForClassName = "ArithmeticException", //指定发生什么异常不回滚,使用的是异常的名称
	        noRollbackFor = ArithmeticException.class,//指定发生什么异常不回滚,使用的是异常的类型
	        rollbackForClassName = "",//指定发生什么异常必须回滚
	        rollbackFor = ArithmeticException.class,//指定发生什么异常必须回滚
	        timeout = -1, //连接超时设置,默认值是-1,表示永不超时
	        readOnly = false, //默认是false,如果是查询操作,必须设置为true.
	        isolation = Isolation.DEFAULT//使用数据库自已的隔离级别        
	)

Spring的两种事务处理方式
1)注解式的事务
使用@Transactional注解完成事务控制,此注解可添加到类上,则对类中所有方法执行事务的设定.此注解可添加到方法上,只是对此方法执行事务的处理.

2)声明式事务(必须掌握),在配置文件中添加一次,整个项目遵循事务的设定.

Spring中事务的五大隔离级别

1).未提交读(Read Uncommitted):允许脏读,也就是可能读取到其他会话中未提交事务修改的数据
2).提交读(Read Committed):只能读取到已经提交的数据。Oracle等多数数据库默认都是该级别 (不重复读)
3).可重复读(Repeated Read):可重复读。在同一个事务内的查询都是事务开始时刻一致的,InnoDB默认级别。在SQL标准中,该隔离级别消除了不可重复读,但是还存在幻象读,但是innoDB解决了幻读
4).串行读(Serializable):完全串行化的读,每次读都需要获得表级共享锁,读写相互都会阻塞
5).使用数据库默认的隔离级别isolation = Isolation.DEFAULT
MySQL:mysql默认的事务处理级别是’REPEATABLE-READ’,也就是可重复读
Oracle:oracle数据库支持READ COMMITTED 和 SERIALIZABLE这两种事务隔离级别。默认系统事务隔离级别是READ COMMITTED,也就是读已提交

为什么添加事务管理器

JDBC: Connection con.commit(); con.rollback();
MyBatis: SqlSession sqlSession.commit(); sqlSession.rollback();
Hibernate: Session session.commit(); session.rollback();
不同技术的操作是不一样的
事务管理器用来生成相应技术的连接+执行语句的对象.
如果使用MyBatis框架,必须使用DataSourceTransactionManager类完成处理
加了之后就可以去生成SqlSession 然后去完成提交或回滚

     
        
        
     

项目中的所有事务,必须添加到业务逻辑层上.

Spring事务的传播特性

@Transactional(propagation = Propagation.REQUIRED,//事务的传播特性
多个事务之间的合并,互斥等都可以通过设置事务的传播特性来解决.
常用
PROPAGATION_REQUIRED:必被包含事务(增删改就用这个)
PROPAGATION_REQUIRES_NEW:自己新开事务,不管之前是否有事务
PROPAGATION_SUPPORTS:支持事务,如果加入的方法有事务,则支持事务,如果没有,不单开事务
PROPAGATION_NEVER:不能运行中事务中,如果包在事务中,抛异常
PROPAGATION_NOT_SUPPORTED:不支持事务,运行在非事务的环境
不常用
PROPAGATION_MANDATORY:必须包在事务中,没有事务则抛异常
PROPAGATION_NESTED:嵌套事务

声明式事务

Spring非常有名的事务处理方式.声明式事务.
要求项目中的方法命名有规范
1)完成增加操作包含 add save insert set
2)更新操作包含 update change modify
3)删除操作包含 delete drop remove clear
4)查询操作包含 select find search get
也就是说,如果你的方法名带有这些名称,就会调用对应的事务配置

配置事务切面时可以使用通配符*来匹配所有方法


    
        
    

    
    
        
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
        
    
    
    
        
        
    

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/924834.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号