Springmvc + mybatis + spring 事物配置,报错回滚

首先要排出spring servlet对@service注解的扫描,不然会导致事物配置失效,过滤排除@service注解
Spring MVC启动时的配置文件,包含组件扫描、url映射以及设置freemarker参数,让spring不扫描带有@Service注解的类。为什么要这样设置?因为servlet-context.xml与service-context.xml不是同时加载,如果不进行这样的设置,那么,spring就会将所有带@Service注解的类都扫描到容器中,等到加载service-context.xml的时候,会因为容器已经存在Service类,使得cglib将不对Service进行代理,直接导致的结果就是在service-context中的事务配置不起作用,发生异常时,无法对数据进行回滚。另外能够将REST URL解析为请求映射的是DefaultAnnotationHandlerMapping这个类,它在启动时,对Controller中所有标注了@RequestMapping注解的方法,都放到了一个HandlerMapping对象中,当有请求时,就在这个对象中进行查找是否有与之匹配路径的处理方法,有则执行,没有就会输出一个Not Page Found警告信息。


springmvc.xml

<!-- 定义控制器注解扫描包路径,控制器注解为 @Controller ,必须排除@Service注解 -->
	<context:component-scan base-package="com.ykf.mattess">
		<context:include-filter expression="org.springframework.stereotype.Controller"
			type="annotation" />
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
	</context:component-scan>

spring.xml

<!-- 事务控制 -->
	<bean name="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="jdbcDataSource" />
	</bean>
	<tx:advice id="userTxAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="remove*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"
				no-rollback-for="java.lang.RuntimeException" />
			<tx:method name="delete*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"
				no-rollback-for="java.lang.RuntimeException" />
			<tx:method name="add*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.RuntimeException" />
			<tx:method name="insert*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.RuntimeException" />
			<tx:method name="batch*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.RuntimeException" />
			<tx:method name="save*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.RuntimeException" />
			<tx:method name="update*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />
			<tx:method name="publish*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />
			<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="list*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="query*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="page*" propagation="SUPPORTS" read-only="true" />
		</tx:attributes>
	</tx:advice>
	<aop:config>
		<aop:pointcut expression="execution(* com.ykf.mattess.imp.*.*(..))" id="pc" />
		<aop:advisor advice-ref="userTxAdvice" pointcut-ref="pc"  />
	</aop:config>
public AjaxResult savePtUser(Qkf_Matess_User qkf_Matess_User) {
		AjaxResult ajaxResult = null;
		Qkf_Matess_User u = this.mattessAotuDao.queryByUnam(qkf_Matess_User
				.getTitle());
		if (u == null) {
			u = new Qkf_Matess_User();
			u.setUname(qkf_Matess_User.getTitle());
			u.setPassword(Md5.getPwd(qkf_Matess_User.getPwd()));
			u.setCtime(System.currentTimeMillis());
			this.mattessAotuDao.saveUs(u);
			System.err.println(9 / 0); //故意整錯,上面的save是会回滚的
			ajaxResult = new AjaxResult(u);
		} else {
			// 用户被暂用
			ajaxResult = new AjaxResult(-3);
		}
		return ajaxResult;
	}

如果不在springmvc.xml配置文件中  排除@service注解,  如上9/0 虽然报错 数据还是会进入数据库的。加上则会回滚

猜你喜欢

转载自blog.csdn.net/wangbo54979/article/details/79757209
今日推荐