配置事务springmvc3.1 mybatis 3.1 事务不起作用
最近使用開發(fā)的過程中出現(xiàn)了一個小問題,順便記錄一下原因和方法--配置事務(wù)
????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方式配置數(shù)據(jù)源 --><!--<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"><property name="jndiName" value="${jndiName}"></property></bean> --><!-- 配置數(shù)據(jù)源 --><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><!-- 配置事務(wù)管理器,注意這里的dataSource和SqlSessionFactoryBean的dataSource要一致,不然事務(wù)就沒有作用了 --><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監(jiān)控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> 每日一道理俄國作家契訶夫說:“有大狗,有小狗,小狗不該因為大狗的存在而心慌意亂。所有的狗都應(yīng)該叫,就讓他各自用上帝給他的聲音。
????
????項目架構(gòu)?
????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 創(chuàng)立時間: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 創(chuàng)立時間: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();}}????
?
????
開發(fā)線路: Spring3.1? mybatis 3.1?
????運行之后。“張三”是插入進去了,但“李四 ”存在 違反獨一約束條件,沒有回滾,所以事務(wù)沒有起到作用。
?
????請大家?guī)椭纯础Vx謝啦! 在線等……
文章結(jié)束給大家分享下程序員的一些笑話語錄: 看到有人回帖“不頂不是中國人”,他的本意是想讓帖子沉了。
轉(zhuǎn)載于:https://www.cnblogs.com/xinyuyuanm/archive/2013/05/22/3093356.html
總結(jié)
以上是生活随笔為你收集整理的配置事务springmvc3.1 mybatis 3.1 事务不起作用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ubuntu装机配置相关
- 下一篇: C++ Primer 有感(类)