Mybatis+Spring 事物回滚配置

Spring配置文件中加入:

	<!-- 事务回滚配置 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="saveUserCustom" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="updateUserCustom" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
		</tx:attributes>
	</tx:advice>

 Action方法体上加入:

	/**
	 * 修改用户自定义
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class)
	public String updateUserCustom(){
		//userAccount = ((User) this.session.get("user2")).getUserName();
		String[] code = appCode.split(",");
		try {
			this.userAppService.delete(userAccount);
			for (int i = 0; i < code.length; i++) {
				UserApp userApp = new UserApp();
				userApp.setUserAccount(userAccount);
				userApp.setAppCode(code[i]);
				this.userAppService.add(userApp);
			}
			logger.info("修改用户自定义信息成功,用户" + userAccount + "自定义系统数量为:"
					+ code.length + "个。");
			return SUCCESS;
		} catch (Exception e) {
			logger.error("修改用户自定义信息失败,事物回滚!", e);
			return ERROR;
		}
	}

 

你可能感兴趣的:(spring,mybatis,配置,事物,回滚)