Spring管理iBatis事务

Spring+ibatis 保留ibatis事务的配置 SPRING与IBATIS整合采用保留IBATIS事务,可自己控制回滚! 立即下载

Spring管理iBatis事务

< sqlMapConfig >
    
< sqlMap  resource ="com/angi/ibatis/maps/User.xml"   />
</ sqlMapConfig >
以上配置省去了transactionManager的配置,就会使用external(外部)事务管理(ExternalTransaction),即等同如下配置:
复制代码
< sqlMapConfig >
    
< transactionManager  type ="EXTERNAL" >
                
<!-- 这个数据源其实没有什么意义,还是取上面的省略方式吧 -->
        
< dataSource  type ="DBCP" >
        
</ dataSource >
    
</ transactionManager >
    
< sqlMap  resource ="com/angi/ibatis/maps/User.xml"   />
</ sqlMapConfig >
复制代码
1、TransactionProxyFactoryBean
复制代码
<? 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:p ="http://www.springframework.org/schema/p"
    xmlns:aop
="http://www.springframework.org/schema/aop"  xmlns:tx ="http://www.springframework.org/schema/tx"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd " >
    
<!--  DataSource  -->
    
< bean  id ="dataSource"  class ="org.apache.commons.dbcp.BasicDataSource" >
        
< property  name ="driverClassName" >
            
< value > com.mysql.jdbc.Driver </ value >
        
</ property >
        
<!-- <property name="defaultAutoCommit" value="false"/> -->
        
< property  name ="url" >
            
< value > jdbc:mysql://localhost/test </ value >
        
</ property >
        
< property  name ="username" >
            
< value > root </ value >
        
</ property >
        
< property  name ="password" >
            
< value > mysql </ value >
        
</ property >
    
</ bean >
    
<!--  Spring iBatis Template  -->
    
< bean  id ="sqlMapClient"  class ="org.springframework.orm.ibatis.SqlMapClientFactoryBean" >
        
< property  name ="configLocation"  value ="SqlMapConfig.xml"   />
        
< property  name ="dataSource"  ref ="dataSource"   />
    
</ bean >
    
< bean  id ="transactionManager"
        class
="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
        
< property  name ="dataSource" >
            
< ref  local ="dataSource"   />
        
</ property >
    
</ bean >
    
< bean  id ="userDAO"  class ="com.angi.ibatis.dao.UserDaoImpl" >
        
< property  name ="sqlMapClient" >
            
< ref  bean ="sqlMapClient"   />
        
</ property >
    
</ bean >
    
< bean  id ="userService"  class ="com.angi.ibatis.service.UserService" >
        
< property  name ="userDao" >
            
< ref  bean ="userDAO"   />
        
</ property >
    
</ bean >
    
< bean  id ="userServiceProxy"
        class
="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" >
        
< property  name ="transactionManager" >
            
< ref  bean ="transactionManager"   />
        
</ property >
        
< property  name ="target" >
            
< ref  local ="userService"   />
        
</ property >
        
< property  name ="transactionAttributes" >
            
< props >
                
<!--  这里的方法签名可以精确到方法, 先懒惰一下全配置上  -->
                
< prop  key ="*" > PROPAGATION_REQUIRED </ prop >
            
</ props >
        
</ property >
    
</ bean >
</ beans >
复制代码
2、TransactionInterceptor
复制代码
<? 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:p ="http://www.springframework.org/schema/p"
    xmlns:aop
="http://www.springframework.org/schema/aop"  xmlns:tx ="http://www.springframework.org/schema/tx"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd " >
    
<!--  DataSource  -->
    
< bean  id ="dataSource"  class ="org.apache.commons.dbcp.BasicDataSource" >
        
< property  name ="driverClassName" >
            
< value > com.mysql.jdbc.Driver </ value >
        
</ property >
        
<!-- <property name="defaultAutoCommit" value="false"/> -->
        
< property  name ="url" >
            
< value > jdbc:mysql://localhost/test </ value >
        
</ property >
        
< property  name ="username" >
            
< value > root </ value >
        
</ property >
        
< property  name ="password" >
            
< value > mysql </ value >
        
</ property >
    
</ bean >
    
<!--  Spring iBatis Template  -->
    
< bean  id ="sqlMapClient"  class ="org.springframework.orm.ibatis.SqlMapClientFactoryBean" >
        
< property  name ="configLocation"  value ="SqlMapConfig.xml"   />
        
< property  name ="dataSource"  ref ="dataSource"   />
    
</ bean >
    
< bean  id ="transactionManager"
        class
="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
        
< property  name ="dataSource" >
            
< ref  local ="dataSource"   />
        
</ property >
    
</ bean >
    
< bean  id ="userDAO"  class ="com.angi.ibatis.dao.UserDaoImpl" >
        
< property  name ="sqlMapClient" >
            
< ref  bean ="sqlMapClient"   />
        
</ property >
    
</ bean >
    
< bean  id ="userService"  class ="com.angi.ibatis.service.UserService" >
        
< property  name ="userDao" >
            
< ref  bean ="userDAO"   />
        
</ property >
    
</ bean >
    
< bean
        
class ="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator" >
        
< property  name ="beanNames" >
            
< list >
                
< value > userService </ value >
            
</ list >
        
</ property >
        
< property  name ="interceptorNames" >
            
< list >
                
< value > transactionInterceptor </ value >
            
</ list >
        
</ property >
    
</ bean >
    
< bean  id ="transactionInterceptor"
        class
="org.springframework.transaction.interceptor.TransactionInterceptor" >
        
< property  name ="transactionManager"  ref ="transactionManager"   />
        
< property  name ="transactionAttributes" >
            
< props >
                
<!--  这里的方法签名可以精确到方法, 先懒惰一下全配置上  -->
                
< prop  key ="*" > PROPAGATION_REQUIRED </ prop >
            
</ props >
        
</ property >
    
</ bean >
</ beans >
复制代码
3、AOP和TX配置
复制代码
<? 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:p ="http://www.springframework.org/schema/p"
    xmlns:aop
="http://www.springframework.org/schema/aop"  xmlns:tx ="http://www.springframework.org/schema/tx"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd " >
    
<!--  DataSource  -->
    
< bean  id ="dataSource"  class ="org.apache.commons.dbcp.BasicDataSource"
        destroy-method
="close" >
        
< property  name ="driverClassName" >
            
< value > com.mysql.jdbc.Driver </ value >
        
</ property >
        
< property  name ="url" >
            
< value > jdbc:mysql://localhost/test </ value >
        
</ property >
        
< property  name ="username" >
            
< value > root </ value >
        
</ property >
        
< property  name ="password" >
            
< value > mysql </ value >
        
</ property >
    
</ bean >
    
<!--  Spring iBatis Template  -->
    
< bean  id ="sqlMapClient"  class ="org.springframework.orm.ibatis.SqlMapClientFactoryBean" >
        
< property  name ="configLocation"  value ="SqlMapConfig.xml"   />
        
< property  name ="dataSource"  ref ="dataSource"   />
    
</ bean >
    
< bean  id ="transactionManager"
        class
="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
        
< property  name ="dataSource"  ref ="dataSource"   />
    
</ bean >
    
<!--  需要引入aop的命名空间  -->
    
< aop:config >
        
<!--  切入点指明了在所有方法产生事务拦截操作  -->
        
< aop:pointcut  id ="serviceMethods"
            expression
="execution(* com.angi.ibatis.service.*.*(..))"   />
        
<!--  定义了将采用何种拦截操作,这里引用到 txAdvice  -->
        
< aop:advisor  advice-ref ="txAdvice"  pointcut-ref ="serviceMethods"   />
    
</ aop:config >
    
<!--  需要引入tx的命名空间  -->
    
<!--  这是事务通知操作,使用的事务管理器引用自 transactionManager  -->
    
< tx:advice  id ="txAdvice"  transaction-manager ="transactionManager" >
        
< tx:attributes >
            
<!--  指定哪些方法需要加入事务,这里懒惰一下全部加入,可以使用通配符来只加入需要的方法  -->
            
< tx:method  name ="*"  propagation ="REQUIRED"   />
        
</ tx:attributes >
    
</ tx:advice >
    
< bean  id ="userDAO"  class ="com.angi.ibatis.dao.UserDaoImpl" >
        
< property  name ="sqlMapClient" >
            
< ref  bean ="sqlMapClient"   />
        
</ property >
    
</ bean >
    
< bean  id ="userService"  class ="com.angi.ibatis.service.UserService" >
        
< property  name ="userDao" >
            
< ref  bean ="userDAO"   />
        
</ property >
    
</ bean >
</ beans >
复制代码
4、anotation
复制代码
<? 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:p ="http://www.springframework.org/schema/p"
    xmlns:tx
="http://www.springframework.org/schema/tx"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd " >
    
<!--  需要引入tx的命名空间  -->
    
< tx:annotation-driven  transaction-manager ="transactionManager"   />
    
<!--  DataSource  -->
    
< bean  id ="dataSource"  class ="org.apache.commons.dbcp.BasicDataSource" >
        
< property  name ="driverClassName" >
            
< value > com.mysql.jdbc.Driver </ value >
        
</ property >
        
<!-- <property name="defaultAutoCommit" value="false"/> -->
        
< property  name ="url" >
            
< value > jdbc:mysql://localhost/test </ value >
        
</ property >
        
< property  name ="username" >
            
< value > root </ value >
        
</ property >
        
< property  name ="password" >
            
< value > mysql </ value >
        
</ property >
    
</ bean >
    
<!--  Spring iBatis Template  -->
    
< bean  id ="sqlMapClient"  class ="org.springframework.orm.ibatis.SqlMapClientFactoryBean" >
        
< property  name ="configLocation"  value ="SqlMapConfig.xml"   />
        
< property  name ="dataSource"  ref ="dataSource"   />
    
</ bean >
    
< bean  id ="transactionManager"
        class
="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
        
< property  name ="dataSource" >
            
< ref  local ="dataSource"   />
        
</ property >
    
</ bean >
    
< bean  id ="userDAO"  class ="com.angi.ibatis.dao.UserDaoImpl" >
        
< property  name ="sqlMapClient" >
            
< ref  bean ="sqlMapClient"   />
        
</ property >
    
</ bean >
    
< bean  id ="userService"  class ="com.angi.ibatis.service.UserService" >
        
< property  name ="userDao" >
            
< ref  bean ="userDAO"   />
        
</ property >
    
</ bean >
</ beans >
复制代码
Java代码:
复制代码
@Transactional
    
public   void  doTransaction() {
        User user 
=   new  User();
        user.setName(
" 11111 " );
        user.setSex(
1 );
        userDao.saveUser(user);
        User user1 
=   new  User();
        user1.setName(
" Angikkk " );
        user1.setSex(
1 );
        userDao.saveUser(user1);
复制代码

    } 

spring ibatis 配置(包括事务管理) spring ibatis 配置spring ibatis 配置spring ibatis 配置spring ibatis 配置 立即下载

相关推荐

如何处理Spring、Ibatis结合MySQL数据库使用时的事务操作Demo

本工程以“银行账号转账”为例子演示了如何处理Spring、Ibatis结合MySQL数据库使用时的事务操作 本工程编码方式:UTF-8 须执行的SQL语句: CREATE DATABASE `test`; USE `test`; CREATE TABLE `lm_bank_card` ( `id` char(36) NOT NULL COMMENT 'ID', `account` varchar(50) NOT NULL COMMENT '账号', `deposit` varchar(50) NOT NULL COMMENT '存款', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='银行卡表'; insert into `lm_bank_card`(`id`,`account`,`deposit`) values ('03a2fb24-62f6-4ff7-85ba-74a51351b975','father','10000'),('b5bddc58-4fe2-451a-9bb3-b3a54203c6da','sun','0');

spring整合ibatis事务管理(各种AOP,比较乱)

Spring通过DAO模式,提供了对iBATIS的良好支持。SqlMapClient对象是iBATIS中的主要对象,我们可以通过配置让spring来管理SqlMapClient对象的创建。   与hibernate类似,Spring 提供了SqlMapClientDaoSupport对象,我们的DAO可以继承这个类,通过它所提供的SqlMapClientTemplate对象来操纵数据库

徐行 3897

记一次从零到getshell的渗透历程

0x00 前期 拿到测试范围清单后,首先用脚本获取了下各个子站的标题,基本都是XXX管理系统。浏览器查看后,各个子站也大同小异,纯登录系统,带验证码。这些系统基本都是jsp写的,逮到了两个没有验证码的小宝贝,一番暴力破解,小宝贝对我说:别爱我,没结果。又看到了两个站返回的Set-Cookie头:rememberMe=deleteMe; 很明显,这是Apache Shiro。打了一波exp,没有反应,放弃。搞了三天啥都没搞出来,和女朋友出去玩散散心,还是很高兴。 0x01 初显进展 测试范围内有六个门户

CanMeng'Blog - 一个WEB安全渗透的技术爱好者 3970

事务,为什么你不回滚

Java 提供了两类主要的异常 :runtime exception 和 checked exception 。 checked 异常也就是我们经常遇到的 IO 异常,以及 SQL 异常都是这种异常。 对于这种异常, JAVA 编译器强制要求我们必需对出现的这些异常进行 catch 。所以,面对这种异常不管我们是否愿意,只能自己去写一大堆 catch 块去处理可能的异常。     但是另外一种

我喂自己袋盐 2620

使用PackStack工具自动化部署OpenStack

搭建 OpenStack 环境,每个组件的安装和配置都需要熟悉其特定的依赖关系、参数设置等,对于初学者来说容易出错。PackStack 工具通过集成一系列脚本和配置文件,便于快速搭建OpenStack练习环境。CPU:支持 Intel VT - x 或 AMD - V 硬件虚拟化技术,并且要在 BIOS 里开启此功能。内存:16GB以上存储:200GB以上可用空间(2)VMware WorkStation16.0以上版本(3)远程连接工具(如Xshell、MobaXterm)

2301_80440447的博客 4095

Comsol 管道导波裂纹检测:从建模到求解的完整流程解析

本文详细解析了使用Comsol Multiphysics进行管道导波裂纹检测的完整仿真流程。从几何建模、材料定义、物理场设置到网格划分与求解后处理,系统阐述了如何构建高保真虚拟模型以模拟导波与裂纹的相互作用。重点分享了激励信号设置、频散效应处理及参数化扫描等实战技巧,旨在帮助工程师与研究人员高效优化无损检测方案,降低实验成本。

weixin_29207439的博客 74

spring+ibatis事务处理配置

(1)applicationContext-resources.xml 配数据源:                                                                                        (2)applicationContext-ibatis.xml  符合Ibatis的DAO配置:              

xingtuyang的专栏 1万+

示波器学习(一):示波器的作用、类型和基本结构

示波器的作用、使用场景;示波器的类型和基本结构介绍

qq_41300605的博客 9142

SpringMVC+ibatis数据库事务控制

由于在系统中使用了关联表,逻辑上是关联的,实际数据库并没有做外键。就需要写大量的数据库事务操作。之前一直没有比较好的方式,直接在dao层侵入的方式进行,虽然该方式可以实现事务的管理,但是总觉得有些问题。实现方式如下: public int insert(ChuxinRecord t) { SqlMapClient sqlMapClient = getWriteTemplate(

piaomiao2080的专栏 3332

单片机通识之UART通信协议(如何用汇编实现)

使用汇编实现UART通信

smart_num_1的博客 998

基于Simulink的发动机管理系统中的自适应增压控制系统设计

通过上述详细设计步骤,我们成功开发了一个基于Simulink的自适应增压控制系统。该系统不仅能够实时监测和预测发动机的运行状态,动态调整增压压力目标值,还具备实时监控、故障诊断和保护功能,确保系统的安全性和可靠性。此外,通过仿真实验和硬件在环测试,我们验证了系统的性能,并对其进行了优化,使其能够更好地满足实际应用需求。这个项目展示了如何利用MATLAB/Simulink的强大功能,结合控制理论和工程实践,开发出高效、可靠的自适应增压控制系统。

MHD0815的博客 221

spring整合ibatis事务管理

原文出处: http://chenkangxian.iteye.com/blog/1264159 Spring通过DAO模式,提供了对iBATIS的良好支持。SqlMapClient对象是iBATIS中的主要对象,我们可以通过配置让spring来管理SqlMapClient对象的创建。   与hibernate类似,Spring 提供了SqlMapClientDaoS

xiaoshang的专栏 625

华为面试手撕真题【判断是ipv4还是ipv6地址】

编写一个函数来验证输入的字符串是否是有效的 IPv4 或 IPv6 地址。

MISAYAONE的博客 1万+

ibatis(学习2)-- spring + ibatis的事务管理

前言:我们知道事务的管理分为声明式和编程式,这里主要针对spring和ibatis结合用spring的声明式管理方式,管理ibatis链接数据库的事务。  条件:数据库支持事务,如果是mysql数据库需要先确定数据库的引擎是否是innodb,只有这个数据库引擎支持事务。  环境:myeclipse+spring3.X+ibatis2  配置文件主要信息  sqlMapCon

博观而约取 厚积而薄发 383

Android软键盘系统深度解析:架构、原理与实现

Android软键盘系统深度解析:架构与实现原理 本文系统剖析了Android软键盘的核心工作机制。作为系统级输入服务,软键盘具有双重属性:既是可安装的独立应用,又深度集成于系统框架中。文章详细解读了软键盘的系统架构,包括其与InputMethodManagerService(IMMS)和WindowManagerService(WMS)的交互机制,以及通过InputConnection实现的跨进程通信流程。同时分析了输入法窗口的特殊管理策略、布局调整模式,并深入探讨了输入预测引擎的实现原理,包括词典加载、

保持专注,保持热爱 117

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

spring管理ibatis事务的方式

Spring管理iBatis事务 &lt;sqlMapConfig&gt;    &lt;sqlMap resource="com/angi/ibatis/maps/User.xml" /&gt;&lt;/sqlMapConfig&gt; 以上配置省去了transactionManager的配置,就会使用external(外部)事务管理(ExternalTransaction...

zengshaotao的专栏 260

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

spring管理ibatis事务

2019独角兽企业重金招聘Python工程师标准>>> ...

weixin_34392906的博客 114

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

spring ibatis事务管理

Spring通过DAO模式,提供了对iBATIS的良好支持。SqlMapClient对象是iBATIS中的主要对象,我们可以通过配置让spring来管理SqlMapClient对象的创建,继而整合iBatis和Spring。 与hibernate类似,Spring 提供了SqlMapClientDaoSupport对象,我们的DAO可以继承这个类,通过它所提供的SqlMapClientTemp...

qingqingdd1199的博客 146

spring集成iBATIS声明式事务管理配置及测试

applicationContext.xml文件如下: [code="java"] ...

nalan 172

spring下iBATIS事务管理-使用AOP XML配置管理

  Spring下由三种途径对事物进行管理:编程式事务管理、声明式事务管理和AOP事务管理。其中AOP事务管理又分AOP注解事务管理和AOP XML配置两种,这里记录下述其中的AOP XML配置管理,这也是spring最推荐的方式。   参照&lt;spring高级程序设计&gt;中的银行转账的例子。 1.Spring的数据源设置 &lt;bean id="dataSource" cla...

JackAndroid的专栏 262
上一篇: Hibernate 所有缓存机制详解
下一篇: Spring 框架的设计理念与设计模式分析
zljjava
博客等级 码龄15年 155粉丝 15原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值