spring 注解 事务

下面我的例子是基于spring + mybatis的

 

首先配置spring

<!-- 数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
	<property name="driverClassName">
		<value>com.mysql.jdbc.Driver</value>
	</property>
	<property name="url">
		<value>jdbc:mysql://127.0.0.1:3306/myspring</value>
	</property>
	<property name="username">
		<value>root</value>
	</property>
	<property name="password">
		<value>admin</value>
	</property>
</bean>

<!-- 指明操作数据库的配置文件位置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	<property name="dataSource" ref="dataSource"></property>
	<property name="configLocation" value="classpath:../config/mybatis/MyBatis-Configuration.xml"></property>
</bean>

<!-- 使用事物处理 -->
<bean id="tm" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<property name="dataSource" ref="dataSource"></property>
</bean>

<tx:annotation-driven transaction-manager="tm" />

<bean id="userService" class="adtec.userManager.service.imp.UserManagerServiceImpl">
	<property name="userdao" ref="userDao"></property>
</bean>

<bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
	<property name="mapperInterface" value="adtec.userManager.dao.UserDao"></property>
	<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>

 

MyBatis-Configuration.xml内容

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <mappers>
    	<mapper resource="adtec/userManager/dao/UserDao.xml"/>
    </mappers>
</configuration>

 

UserDao.xml内容

<?x ml 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="adtec.userManager.dao.UserDao">
	<resultMap type="adtec.userManager.model.User"
		id="querys">
		<id property="Uid" column="Uid"></id>
		<result property="userName" column="userName"></result>
		<result property="userPassword" column="userPassword"></result>
	</resultMap>


	<!-- 添加新用户 -->
	<insert id="insert" parameterType="adtec.userManager.model.User">
		insert into user(userName,userPassword)
		values(#{userName},#{userPassword})
	</insert>

</mapper>

 

@Transactional(propagation=Propagation.REQUIRED)
public class UserManagerServiceImpl implements UserManagerService {
	
	Logger log = Logger.getLogger(UserManagerServiceImpl.class);

	private UserDao userdao;
	
	public UserDao getUserdao() {
		return userdao;
	}

	public void setUserdao(UserDao userdao) {
		this.userdao = userdao;
	}

	/**
	 * 用户添加方法
	 */
    @Override
	public boolean add(User user) throws Exception{
		try{
			this.userdao.insert(user);
			//Integer.parseInt("d");
			log.debug("this.userdao.insert(user)  ok");
		}catch(Exception e){
			log.error("this.userdao.insert(user)  error");
			throw e;
		}
		return true;
	}
	
}

备注:操作数据库的操作如果出现异常一定要抛出来,否则只是代码处理了,事务没有捕捉到异常,因此不会起作用。 

 

调用severce的controller层

@RequestMapping(value = "/insertAction", method = RequestMethod.POST)
	public String AddRegisterAction(HttpServletRequest request,
			HttpServletResponse response) {
		log.info("AddRegisterAction");
		log.info("log001");
		String userName = request.getParameter("userName");
		String userPassword = request.getParameter("userPassword");
		//将密码进行MD5加密
		String userPasswordMd5 = Md5Util.getMD5String(userPassword);
		User user = new User();
		user.setUserName(userName);
		user.setUserPassword(userPasswordMd5);
		try {
			userService.add(user);
		} catch (Exception e) {
			log.error("添加用户失败");
		}
		//添加后的跳转提示
		String PromptMsg = "InsertMsg";
		
		//跳转到登陆页面
		return "redirect:/user/list.do?PromptMsg="+PromptMsg;
	}

 

你可能感兴趣的:(spring)