xuke6677 2013-06-03 02:11 采纳率: 0%
浏览 7949

spring AOP 拦截器方式配置事务失效

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>

    <!-- 拦截器方式配置事物 -->
    <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;
import com.zfy.db.model.form.FormSubject;

public int insert(FormSubject record){
        FormSubject fs = new FormSubject();
              fs.setFormCode(3);
              fs.setFormName("张三123");
             for (int i = 0; i <2; i++) {
                          mapper.insert(fs);
                     }
     return 0;
}

}

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 测试类:

@Test
    public void  test1() throws Exception{

        try {

              formSubjectService.insert( null);
        }} catch (RuntimeException e) {
            e.printStackTrace();
        }
    }

开发路线: Spring3.1 mybatis 3.1 运行之后。“张三”是插入进去了,但“李四 ”存在 违反唯一约束条件,没有回滚,所以事务没有起到作用。请大家帮忙看看。谢谢啦! 在线等……

  • 写回答

1条回答

  • ChatGPTᴼᴾᴱᴺᴬᴵ 2022-12-24 19:47
    关注

    这些配置文件都是用来配置Spring框架的,在这里你可以定义项目中所使用的bean,以及定义bean之间的依赖关系。


    在Spring.xml文件中,你使用了context:component-scan标签来自动扫描并注册项目中的bean。在这里,你指定了要扫描的包路径为com.zfy.db.dao和com.zfy.service,Spring框架会在这两个包中寻找带有特定注解(例如@Component,@Service等)的类,并自动将这些类作为bean注册到Spring容器中。


    在spring-mybatis.xml文件中,你使用了标签来定义了一个数据源bean(即DruidDataSource类型的bean),这个bean将会用于连接数据库。你还使用了标签来定义了一个SqlSessionFactory类型的bean,这个bean将会用于创建SqlSession对象,而SqlSession对象可以用于执行SQL语句。


    此外,你还使用了标签来定义了一些Mapper接口的代理实现类,这些代理实现类将会用于执行数据库操作。你还使用了tx:annotation-driven标签来启用注解式事务管理,这样你就可以在代码中使用@Transactional注解来声明事务边界。

    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?