spring mybatis事务回滚配置
来源:互联网 发布:靠谱的淘宝代购 编辑:程序博客网 时间:2024/05/21 06:33
1、配置mybatis数据源
<!-- mybatis --> <bean id="writeTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="writeDataSource" /> </bean> <!-- 自动扫描事务注解 --> <tx:annotation-driven transaction-manager="writeTransactionManager" proxy-target-class = "true"/> <bean id="writeDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${master.driver}" /> <property name="url" value="${master.url}" /> <property name="username" value="${master.username}" /> <property name="password" value="${master.password}" /> <!-- 配置最大 最大空闲 最小空闲 最大延迟--> <property name="maxActive" value="${master.maxActive}"></property> <property name="maxIdle" value="${master.maxIdle}"></property> <property name="minIdle" value="${master.minIdle}"></property> <property name="maxWait" value="${master.maxWait}"></property> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="${master.timeBetweenEvictionRunsMillis}" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="${master.minEvictableIdleTimeMillis}"/> </bean> <bean id="writeSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="writeDataSource" /> <!--读取所有的mybatis配置的xml文件--> <property name="mapperLocations" value="classpath*:write/**/*.xml"></property> <!--mybatis的配置,包括一些类别名的配置--> <property name="configLocation" value="classpath:mybatis/mybatis-configuration.xml"></property> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.galaxy.fym.dao.write"/> <property name="sqlSessionFactoryBeanName" value="writeSqlSessionFactory"></property> </bean>
2、代码里在方法上设置@Transactional注解
3、只有继承自RuntimeException的异常才会被事务捕获然后回滚(这个是重点)
@Transactional public void insertLog() throws Exception{ logDao.insertTest(); Log log = new Log(); log.setDesc("1111111"); logDao.insert(log); if(true){ throw new RuntimeException("1111"); } }
还有个坑就是如果用MySQL数据库,数据库表你如果是自动建表,那么就需要把建表的Engine设置为InnoDB格式,自动建表的格式为:MyISAM,这中格式的是不支持事务管理的。这个我实验过,MyISAM确实不支持事务。
mybatis里#与的区别就是#会把传递的参数当字符串解析,而 就简单的直接把参数合成sql语句,一定程度上#可以防止sql注入。
0 0
- spring mybatis事务回滚配置
- SPRING配置事务回滚
- spring aop事务配置,事务回滚
- spring+ mybatis 事务不能回滚问题解决
- Spring事务回滚
- spring事务回滚
- spring事务回滚
- spring事务回滚
- spring回滚事务
- spring 事务回滚
- spring 事务回滚
- spring事务回滚
- Spring事务回滚
- [Spring]事务回滚
- Spring事务回滚
- spring事务回滚
- mybatis 手动回滚事务
- 配置Spring JDBC的事务回滚失效
- 二分查找
- 通用块层,scsi_cmd的产生及发送,mmc block实现.
- 关于realsense R200在室外outdoor得不得depth的问题解决
- BFS入门-Red and Black
- 框架中的自动验证
- spring mybatis事务回滚配置
- Centos Nginx搭建https
- OpenLayers3中wfs的属性查询
- You need tcl 8.5 or newer in order to run the Redis test
- 不用除法运算
- SSL/TLS协议运行机制的概述
- 关于Android Studio主Module与依赖Module同时引入so库的问题
- 【异常处理】Are you missing a call to unregisterReceiver()?
- MyBatis批量插入