Spring事务之六(JUnit单测事务回滚)
来源:互联网 发布:西宁数据工程师招聘 编辑:程序博客网 时间:2024/06/05 16:52
更多文章:http://zhuqiuhui.space/
测试代码
Case 1: 插入成功后会回滚 输出:insert :1 rows! Rolled back transaction after test execution for test context...... @Test @Transactional public void testTx() throws Exception{ User user = new User(); user.setUsername("zhuqiuhui-testTx"); user.setAddress("河南省上蔡县-testTx"); user.setBirthday(new Date()); user.setSex("男-testTx"); int num = iUserService.insertUser(user); assert(num==1); System.out.println("insert :" + num + " rows!"); } Case 2: 正常插入,此过程若(1)处抛出异常(不论Exception、RuntimeException或者FileNotFoundException等)均正常插入,事务并不回滚 @Test @Transactional @Rollback(false) public void testTx() throws Exception{ User user = new User(); user.setUsername("zhuqiuhui-testTx"); user.setAddress("河南省上蔡县-testTx"); user.setBirthday(new Date()); user.setSex("男-testTx"); int num = iUserService.insertUser(user);--------(1) assert(num==1); System.out.println("insert :" + num + " rows!"); }xml配置(本环境结合mybatis+spring)
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" > <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/mybatisTest" /> <property name="username" value="root" /> <property name="password" value="" /></bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybatis.xml" /> <property name="mapperLocations"> <list> <value>classpath:User.xml</value> </list> </property></bean> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg ref="sqlSessionFactory"/></bean> <!-- 配置扫描Mapper接口的包路径 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 如果是多个包名可用",; \t\n"中任意符号分隔开,详见:MapperScannerConfigurer[269行] --> <property name="basePackage" value="com.mybatis.dao"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/></bean> <!--配置事务--><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /></bean><tx:annotation-driven transaction-manager="transactionManager"/>
0 0
- Spring事务之六(JUnit单测事务回滚)
- spring test junit 事务回滚
- Spring事务回滚
- spring事务回滚
- spring事务回滚
- spring事务回滚
- spring回滚事务
- spring 事务回滚
- spring 事务回滚
- spring事务回滚
- Spring事务回滚
- [Spring]事务回滚
- Spring事务回滚
- spring事务回滚
- spring jpa整合junit单元测试 ,设置事务自动回滚
- Spring TestContext 做带事务回滚的JUnit测试
- Spring事务之事务回滚、清理及提交
- (spring)什么是声明式事务,回滚事务
- Maven学习视频
- Linux下安装Nginx并配置一个图片服务器
- tcp三次握手与四次挥手
- ISE仿真器报错:ERROR:Simulator:861 – Failed to link the design 解决办法
- Java打印二维数组
- Spring事务之六(JUnit单测事务回滚)
- c++第四次作业
- Spring事务之七(事务自动提交)
- easyinstall、pip、numpy、scipy、pandas安装
- C++ list类存储结构体
- acm_线性表操作
- javascript 垃圾回收机制 小结
- Masonry比例用法
- git小技巧--如何从其他分支merge个别文件或文件夹