日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

SSM中进行注解式和XML配置式事务管理

發布時間:2025/3/19 asp.net 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SSM中进行注解式和XML配置式事务管理 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

場景

前面實現SSM簡單整合以及CRUD參照:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/85161018

SSM中配置事務管理所需jar包:

https://download.csdn.net/download/badao_liumang_qizhi/10867618

Mysql的表必須是INNODB的引擎才支持

實現

導入jar包

將事務所需jar包放在項目lib目錄下

修改service

給接口添加deleteAll方法以及addTwo()方法

package com.badao.service;import java.util.List;import com.badao.pojo.User; import com.badao.util.Page;public interface UserService {List<User> selectAllUser();User selectUser(Integer id);void updateUser(User user);void deleteUser(int id);void addUser(User user);void addTwo();void deleteAll();//List<User> selectAllUser(Page page);//int total();}

修改serviceImpl

先刪除所有User,然后再插入兩個User,其中第二個User故意設置名字過長。

package com.badao.service.impl;import java.util.List;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional;import com.badao.mapper.UserMapper; import com.badao.pojo.User; import com.badao.service.UserService; import com.badao.util.Page; @Service public class UserServiceImpl implements UserService {@AutowiredUserMapper userMapper;public List<User> selectAllUser() {// TODO Auto-generated method stubreturn userMapper.selectAllUser();} /*?@Overridepublic List<User> selectAllUser(Page page) {// TODO Auto-generated method stubreturn userMapper.selectAllUser(page);}@Overridepublic int total() {// TODO Auto-generated method stubreturn userMapper.total();}*/@Overridepublic User selectUser(Integer id) {// TODO Auto-generated method stubreturn userMapper.selectUser(id);}@Overridepublic void updateUser(User user) {// TODO Auto-generated method stubuserMapper.updateUser(user);}@Overridepublic void deleteUser(int id) {// TODO Auto-generated method stubuserMapper.deleteUser(id);}@Overridepublic void addUser(User user) {// TODO Auto-generated method stubuserMapper.addUser(user);}@Override/*@Transactional(propagation=Propagation.REQUIRED,rollbackForClassName="Exception")*/public void addTwo() {// TODO Auto-generated method stubUser user1 = new User();user1.setName("張三");user1.setAge(22);userMapper.addUser(user1);//用戶2故意設置名字長度過長User user2 = new User();user2.setName("嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿");user2.setAge(25);userMapper.addUser(user2);}@Overridepublic void deleteAll() {// TODO Auto-generated method stubList<User> userList = selectAllUser();for (User user : userList) {userMapper.deleteUser(user.getId());}}}

編寫測試類

package com.badao.test;import java.util.List;import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.annotation.Rollback; import org.springframework.test.context.ContextConfiguration; import com.badao.mapper.UserMapper; import com.badao.pojo.User; import com.badao.service.UserService; import com.badao.util.Page; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;//使用junit4時報錯導這個包 @RunWith(SpringJUnit4ClassRunner.class)//使用junit4進行測試 @ContextConfiguration("classpath:applicationContext.xml")//加載配置文件 public class InsertTest {@Autowired//自動注入private UserMapper userMapper;@Autowired//自動注入private UserService userService;@Test//標明是測試方法@Rollback(false)? //標明使用完此方法后事務不回滾,true時為回滾public void testAdd() {for (int i = 0; i < 100; i++) {User user = new User();user.setName("user"+i);userMapper.addUser(user);}}@Test//標明是測試方法public void testAddTwo() {//刪除全部沒用數據userService.deleteAll();//執行插入兩個useruserService.addTwo();}/*? @Testpublic void testTotal() {int total = userMapper.total();System.out.println(total);}@Testpublic void testList() {Page p = new Page();p.setStart(2);p.setCount(3);List<User> cs=userMapper.selectAllUser(p);for (User c : cs) {System.out.println(c.getName());}}*/ }

配置applicationContext.xml

進行事務配置,添加事務管理器和事務注解掃描器

<tx:annotation-driven transaction-manager="transactionManager"/> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /></bean>

完整代碼

<?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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"><context:annotation-config /><context:component-scan base-package="com.badao.service" /><!-- ?<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">?<property name="driverClassName">?<value>com.mysql.jdbc.Driver</value>?</property>?<property name="url">?<value>jdbc:mysql://localhost:3306/ssmtest?characterEncoding=UTF-8</value>?</property>?<property name="username">?<value>root</value>?</property>?<property name="password">?<value>523627</value>?</property>? ?</bean> --><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"><!-- 基本屬性 url、user、password --><property name= "url"value="jdbc:mysql://localhost:3306/ssmtest?characterEncoding=UTF-8" /><property name="username" value="root" /><property name="password" value="523627" /><property name="driverClassName" value="com.mysql.jdbc.Driver" /><!-- 配置初始化大小、最小、最大 --><property name="initialSize" value="3" /><property name="minIdle" value="3" /><property name="maxActive" value="20" /><!-- 配置獲取連接等待超時的時間 --><property name="maxWait" value="60000" /><!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒 --><property name="timeBetweenEvictionRunsMillis" value="60000" /><!-- 配置一個連接在池中最小生存的時間,單位是毫秒 --><property name="minEvictableIdleTimeMillis" value="300000" /><property name="validationQuery" value="SELECT 1" /><property name="testWhileIdle" value="true" /><property name="testOnBorrow" value="false" /><property name="testOnReturn" value="false" /><!-- 打開PSCache,并且指定每個連接上PSCache的大小 --><property name="poolPreparedStatements" value="true" /><property name="maxPoolPreparedStatementPerConnectionSize" value="20" /></bean><bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="typeAliasesPackage" value="com.badao.pojo" /><property name="dataSource" ref="dataSource"/><property name="mapperLocations" value="classpath:com/badao/mapper/*.xml"/><property name="plugins"><array><bean class="com.github.pagehelper.PageInterceptor"><property name="properties"><!--使用下面的方式配置參數,一行配置一個 --><value></value></property></bean></array></property>??</bean><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.badao.mapper"/></bean><tx:annotation-driven transaction-manager="transactionManager"/><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /></bean></beans>

使用注解方式

為serviceImpl里的addTwo()方法加上事務注解

@Override@Transactional(propagation=Propagation.REQUIRED,rollbackForClassName="Exception")public void addTwo() {// TODO Auto-generated method stubUser user1 = new User();user1.setName("張三");user1.setAge(22);userMapper.addUser(user1);//用戶2故意設置名字長度過長User user2 = new User();user2.setName("嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿");user2.setAge(25);userMapper.addUser(user2);}

運行測試

運行測試類中的testAddTwo()方法

@Test//標明是測試方法public void testAddTwo() {//刪除全部沒用數據userService.deleteAll();//執行插入兩個useruserService.addTwo();}

先將數據庫中的數據刪除干凈,然后再執行插入兩個User,

因為第二個user的名字過長,所以報異常,導致事務回滾,所以第一個user的插入會回滾,可以看到數據庫中是一條數據都沒有。

使用XML配置事務

注釋掉上面注解的部分,包括serviceImpl中的方法上的注解,修改applicationContext.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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"><context:annotation-config /><context:component-scan base-package="com.badao.service" /><!-- ?<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">?<property name="driverClassName">?<value>com.mysql.jdbc.Driver</value>?</property>?<property name="url">?<value>jdbc:mysql://localhost:3306/ssmtest?characterEncoding=UTF-8</value>?</property>?<property name="username">?<value>root</value>?</property>?<property name="password">?<value>523627</value>?</property>? ?</bean> --><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"><!-- 基本屬性 url、user、password --><property name= "url"value="jdbc:mysql://localhost:3306/ssmtest?characterEncoding=UTF-8" /><property name="username" value="root" /><property name="password" value="523627" /><property name="driverClassName" value="com.mysql.jdbc.Driver" /><!-- 配置初始化大小、最小、最大 --><property name="initialSize" value="3" /><property name="minIdle" value="3" /><property name="maxActive" value="20" /><!-- 配置獲取連接等待超時的時間 --><property name="maxWait" value="60000" /><!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒 --><property name="timeBetweenEvictionRunsMillis" value="60000" /><!-- 配置一個連接在池中最小生存的時間,單位是毫秒 --><property name="minEvictableIdleTimeMillis" value="300000" /><property name="validationQuery" value="SELECT 1" /><property name="testWhileIdle" value="true" /><property name="testOnBorrow" value="false" /><property name="testOnReturn" value="false" /><!-- 打開PSCache,并且指定每個連接上PSCache的大小 --><property name="poolPreparedStatements" value="true" /><property name="maxPoolPreparedStatementPerConnectionSize" value="20" /></bean><bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="typeAliasesPackage" value="com.badao.pojo" /><property name="dataSource" ref="dataSource"/><property name="mapperLocations" value="classpath:com/badao/mapper/*.xml"/><property name="plugins"><array><bean class="com.github.pagehelper.PageInterceptor"><property name="properties"><!--使用下面的方式配置參數,一行配置一個 --><value></value></property></bean></array></property>??</bean><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.badao.mapper"/></bean><!-- <tx:annotation-driven transaction-manager="transactionManager"/> --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /></bean><tx:advice id="txadvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="add*" propagation="REQUIRED" rollback-for="Exception" /><tx:method name="del*" propagation="REQUIRED" rollback-for="Exception"/><tx:method name="edit*" propagation="REQUIRED" rollback-for="Exception" /><tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/><tx:method name="list*" propagation="REQUIRED" rollback-for="Exception"/></tx:attributes></tx:advice><aop:config><aop:pointcut id="serviceMethod" expression="execution(* com.badao.service.*.*(..))"/><aop:advisor pointcut-ref="serviceMethod" advice-ref="txadvice"/></aop:config>?????</beans>

再次運行測試類

再執行上面的測試一樣的效果。

源碼下載

https://download.csdn.net/download/badao_liumang_qizhi/10867762

總結

以上是生活随笔為你收集整理的SSM中进行注解式和XML配置式事务管理的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 天堂在线成人 | 日本不卡在线视频 | 高清福利视频 | 精品人妻一区二区三区久久夜夜嗨 | 香蕉久久精品 | 三级全黄做爰龚玥菲在线 | youjizzxxx69| 正在播放老肥熟妇露脸 | 国产欧美久久久久 | 96国产在线 | 波多野结衣影片 | 日韩av一区二区三区在线 | 亚洲精品视频观看 | а√中文在线资源库 | 日韩一二区 | 国产91精品一区二区麻豆亚洲 | 伊人在线 | 91精品国产99久久久久久 | 亚洲黄色片 | 91爱爱爱 | 一区二区三区久久久 | 女人和拘做爰正片视频 | 天天想你免费观看完整版高清电影 | 99av在线| 欧美性色19p | 欧美干| 国产激情免费视频 | 一级少妇精品久久久久久久 | 制服丝袜中文字幕在线 | 中文字幕视频在线播放 | 色呦呦 | 久草福利资源站 | 国产精品久久久91 | 日韩在线高清 | 91性高潮久久久久久久久 | 怡红院久久 | 国产三级三级看三级 | 免费毛片在线 | 久久久激情网 | 性av免费 | 爱情岛论坛自拍亚洲品质极速最新章 | 伊人免费在线观看高清版 | 樱空桃在线观看 | 国产精彩视频一区 | 国产精品成人99一区无码 | 亚洲天堂99 | 激情五月五月婷婷 | 日本不卡不卡 | 黄色一及毛片 | 美女国产毛片a区内射 | 美国性生活大片 | 亚洲网站av| 免费激情av | 免费一区 | 一级大片免费观看 | av色图 | 国产又粗又黄视频 | 亚洲午夜剧场 | 国产免费看黄 | 国产激情视频一区 | 国产精品69毛片高清亚洲 | 在线看污片 | 国产精品无码午夜福利 | 久久久久久久久久艹 | 综合久久av | 老男人av| 国产成人无码av在线播放dvd | 18禁网站免费无遮挡无码中文 | 一本色道久久综合亚洲精品图片 | 色欲人妻综合网 | 国产视频二| 日韩精品视频在线看 | 假日游船法国满天星 | 欧美日韩免费观看一区=区三区 | 色噜噜日韩精品欧美一区二区 | 亚洲性网| 国产精品一品二品 | 欧美日韩成人一区 | 色综合网站 | 让男按摩师摸好爽 | 国产成人高清 | 欧美日韩资源 | 无码一区二区三区在线观看 | 国产午夜片 | 免费黄色小视频网站 | 国产专区视频 | 少妇做爰免费视频播放 | 风韵少妇性饥渴推油按摩视频 | 极品美女开粉嫩精品 | 成人性生交免费看 | 国产免费a级片 | 国产成人在线免费视频 | 亚洲色图13p | aa一级视频 | 午夜成年人视频 | 夜夜嗨av色一区二区不卡 | 色婷婷综合久久 | 国产精品人人做人人爽 | 美女超碰在线 |