最近使用开发的过程中出现了一个小问题,顺便记录一下原因和方法--配置事务

Spring.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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
"><!-- 引入属性文件 --><context:property-placeholder location="classpath:config.properties" /><!-- 主动扫描dao和service包(主动注入) --><context:component-scan base-package="com.zfy.db.dao,com.zfy.service" />
</beans>

spring-mybatis.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:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
"><!-- JNDI方式配置数据源 --><!--<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"><property name="jndiName" value="${jndiName}"></property></bean> --><!-- 配置数据源 --><bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"><property name="url" value="${jdbc_url}" /><property name="username" value="${jdbc_username}" /><property name="password" value="${jdbc_password}" /><property name="initialSize" value="0" /><property name="maxActive" value="20" /><property name="maxIdle" value="20" /><property name="minIdle" value="0" /><property name="maxWait" value="60000" /><property name="validationQuery" value="${validationQuery}" /><property name="testOnBorrow" value="false" /><property name="testOnReturn" value="false" /><property name="testWhileIdle" value="true" /><property name="timeBetweenEvictionRunsMillis" value="60000" /><property name="minEvictableIdleTimeMillis" value="25200000" /><property name="removeAbandoned" value="true" /><property name="removeAbandonedTimeout" value="1800" /><property name="logAbandoned" value="true" /><property name="filters" value="mergeStat" /></bean><!-- myBatis文件 --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" /><!-- 主动扫描entity目录, 免却Configuration.xml里的手工配置 --><property name="mapperLocations"><array><value>classpath:com/zfy/mapper/portal/*.xml</value><value>classpath:com/zfy/mapper/permission/*.xml</value><value>classpath:com/zfy/mapper/form/*.xml</value></array></property></bean><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.zfy.db.dao" /><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /></bean><!-- 配置事务管理器,注意这里的dataSource和SqlSessionFactoryBean的dataSource要一致,不然事务就没有作用了 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /></bean><!-- 注解方式配置事物 --><!-- <tx:annotation-driven transaction-manager="transactionManager" /> --><!-- 拦截器方式配置事物 --><tx:advice id="transactionAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="add*" propagation="REQUIRED" /><tx:method name="append*" propagation="REQUIRED" /><tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Exception"/><tx:method name="save*" propagation="REQUIRED" /><tx:method name="update*" propagation="REQUIRED" /><tx:method name="modify*" propagation="REQUIRED" /><tx:method name="edit*" propagation="REQUIRED" /><tx:method name="delete*" propagation="REQUIRED" /><tx:method name="remove*" propagation="REQUIRED" /><tx:method name="repair" propagation="REQUIRED" /><tx:method name="delAndRepair" propagation="REQUIRED" /><tx:method name="get*" propagation="SUPPORTS" /><tx:method name="find*" propagation="SUPPORTS" /><tx:method name="load*" propagation="SUPPORTS" /><tx:method name="all*" propagation="SUPPORTS" /><tx:method name="search*" propagation="SUPPORTS" /><tx:method name="datagrid*" propagation="SUPPORTS" /><tx:method name="*" propagation="SUPPORTS" /></tx:attributes></tx:advice><aop:config><aop:pointcut id="transactionPointcut" expression="execution(* com.zfy.service..*.*impl.*(..))" /><aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" /></aop:config><!-- 配置druid监控spring jdbc --><bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor"></bean><bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype"><property name="patterns"><list><value>com.zfy.service.*</value></list></property></bean><aop:config><aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut" /></aop:config></beans>
每日一道理
俄国作家契诃夫说:“有大狗,有小狗,小狗不该因为大狗的存在而心慌意乱。所有的狗都应该叫,就让他各自用上帝给他的声音。

项目架构

DAO接口:   com.zfy.db.dao.form.FormSubjectMapper

package com.zfy.db.dao.form;import com.zfy.db.model.form.FormSubject;public interface FormSubjectMapper {int insert(FormSubject record);}

Service :     com.zfy.service.form.FormSubjectService

package com.zfy.service.form;import com.zfy.db.model.form.FormSubject;/*** @author keke* @version 创立时间:2013-5-15 下昼11:05:45* */
public interface FormSubjectService {public    int insert(FormSubject record) throws Exception;
}

ServiceImpl: com.zfy.service.form.impl.FormSubjectServiceImpl

package com.zfy.service.form.impl;mport com.zfy.db.model.form.FormSubject;/*** @author keke* @version 创立时间:2013-5-15 下昼11:06:56* */
@Service
@Transactional
public class FormSubjectServiceImpl implements FormSubjectService {@Resourceprivate FormSubjectMapper mapper;public int insert(FormSubject record) throws Exception {return mapper.insert(record);}}

mybatis 配置文件 FormSubjectMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.zfy.db.dao.form.FormSubjectMapper" ><resultMap id="BaseResultMap" type="com.zfy.db.model.form.FormSubject" ><id column="FORM_CODE" property="formCode" jdbcType="INTEGER" /><result column="FORM_NAME" property="formNam" jdbcType="VARCHAR" /></resultMap><insert id="insert" parameterType="com.zfy.db.model.form.FormSubject" >insert into FORM_SUBJECT (FORM_CODE, FORM_NAME)values (#{formCode,jdbcType=INTEGER}, #{formName,jdbcType=VARCHAR})</insert></mapper>

Junit  测试类:

@Testpublic void  test1() throws Exception{try {FormSubject fs = new FormSubject();fs.setFormCode(3);fs.setFormName("张三123");for (int i = 0; i <2; i++) {mapper.insert( fs);}} catch (RuntimeException e) {e.printStackTrace();}}

开发线路: Spring3.1  mybatis 3.1

运行之后。“张三”是插入进去了,但“李四 ”存在 违反独一约束条件,没有回滚,所以事务没有起到作用。

请大家帮助看看。谢谢啦! 在线等……

文章结束给大家分享下程序员的一些笑话语录: 看到有人回帖“不顶不是中国人”,他的本意是想让帖子沉了。

转载于:https://www.cnblogs.com/xinyuyuanm/archive/2013/05/22/3093356.html

配置事务springmvc3.1 mybatis 3.1 事务不起作用相关推荐

  1. 【Mybatis+spring整合源码探秘】--- mybatis整合spring事务原理

    文章目录 1 mybatis整合spring事务原理 1 mybatis整合spring事务原理 本篇文章不再对源码进行具体的解读了,仅仅做了下面一张图: 该图整理了spring+mybatis整合后 ...

  2. SpringMVC、MyBatis声明式事务管理

    2019独角兽企业重金招聘Python工程师标准>>> 采用的基本搭建环境:SpringMVC.MyBatis.MySQL.tomcat         Spring事务管理分解了传 ...

  3. mybatis plus 事务管理器_Mybatis中的事务

    Mybatis中的事务 数据库中的事务可以保证在连续执行的多条写操作(增删改)时,这多条操作要么成功,要么全部失败,以保证数据和逻辑的完整及严谨 在使用mybatis时,无需考虑事务如何创建,如何提交 ...

  4. MyBatis及Spring事务初学总结

    MyBatis是从iBatis项目继承而来,是目前互联网公司的主流数据访问层框架. 在实际使用中与Spring一起使用.MyBatis出了MyBatis-Spring组件,用于两者之间的整合,使用Sp ...

  5. Spring事务如何集成到Mybatis之springboot事务

    SpringBoot中通过DataSourceTransactionManagerAutoConfiguration自动配置类,生成基于数据库的事务管理类:DataSourceTransactionM ...

  6. java day55【 Mybatis 连接池与事务深入 、 Mybatis 的动态 SQL 语句、 Mybatis 多表查询之一对多 、 Mybatis 多表查询之多对多】...

    第1章 Mybatis 连接池与事务深入 1.1 Mybatis 的连接池技术 1.1.1 Mybatis 连接池的分类 1.1.2 Mybatis 中数据源的配置 1.1.3 Mybatis 中 D ...

  7. Mybatis 中的事务

    1. Mybaits中的事务接口Transaction public interface Transaction {Connection getConnection() throws SQLExcep ...

  8. Spring-09 整合mybatis声明式事务

    声明式事务 回顾事务 事务在项目开发过程非常重要,涉及到数据的一致性的问题,不容马虎!事务管理是企业级应用程序开发中必备技术,用来确保数据的完整性和一致性. 事务就是把一系列的动作当成一个独立的工作单 ...

  9. Insight Mybatis JdbcTemplate 混合事务控制的实现

    混合使用的背景 最近项目中需要引入工作流引擎,实现业务和流程设计.流转的解耦. 工作流流引擎选用的是snaker,轻量.易上手.可定制.访问数据库用的是JdbcTemplate . 项目中原有的持久层 ...

最新文章

  1. JFrame 简单使用
  2. Mysql学习总结(11)——MySql存储过程与函数
  3. 交管12123显示当前环境存在风险_政策|取消驾驶证年龄上限、推行异地通办,12项交管新政来了...
  4. 根据数据集获取概率密度图像和概率分布图像
  5. Luogu P1031 均分纸牌(贪心)
  6. HDU4907小技巧
  7. 微软邮件服务器名称,邮箱服务器角色概述
  8. 线性代数与MATLAB2
  9. Xcode 与 macOS 系统版本的兼容问题
  10. 遍历查询+从非根节点开始遍历+从下向上遍历树+从层次化查询中删除节点和分支...
  11. WIN7 MBR转GPT分区实现UEFI启动
  12. 深度操作系统20.6发布!
  13. 寒霜3引擎再造经典极品飞车18
  14. spring boot火车票预订系统毕业设计-附源码091029
  15. 计算机考研院校难度排行榜
  16. mingw+msys windows下配置
  17. 跳入餐饮新消费水池,腾讯、字节、B站能否“如鱼得水”?
  18. APP界面常用的8种页面布局
  19. AppleScript学习笔记(一)初识AppleScript
  20. 转:sourceingsight 破解版下载安装

热门文章

  1. 这份美团架构师讲解的JVM知识,让我疫情期间,成功拿下阿里offer
  2. 如何搭建一个 MySQL 分布式集群
  3. Spring Boot 2.1.0 已发布,7 个重大更新! 1
  4. MYSQL:MYSQL索引为什么选择B+树?
  5. 前端JS——滑动滑块验证登录(源码及效果)
  6. android 数组赋值字符串_c语言中的字符数组与字符串
  7. 你真的认识 “ 数据中心网络 ” 吗?
  8. 关于对2020年数据中心的发展的8个预测
  9. mysql解决丢失更新_mysql 数据丢失更新的解决方法
  10. python猜拳游戏三局两胜制_python石头剪刀布小游戏(三局两胜制)