MyBatis_与spring的整合之添加事务

来源:互联网 发布:航盛导航端口 编辑:程序博客网 时间:2024/06/07 16:32


源码下载

    MyBatis与spring的整合-添加事务-src.zip


在上篇的基础上做如下修改(添加)


1. 添加jar


    com.springsource.org.aspectj.tools-1.6.6.RELEASE.jar


2. ApplicationContext.xml 事务配置

<tx:advice id="userTxAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="delete*" propagation="REQUIRED" /><tx:method name="save*" propagation="REQUIRED" /><tx:method name="update*" propagation="REQUIRED" /><tx:method name="find*" read-only="true" /><tx:method name="get*" read-only="true" /><tx:method name="select*" read-only="true" /></tx:attributes></tx:advice><aop:config><!-- 第一个"*"代表所有类,第二个"*"代表所有方法 ".."代表任意参数 --><aop:pointcut id="pc" expression="execution(* com.hehe.mybatis.service.*.*(..))" /><!--把事务控制在Service层--><aop:advisor pointcut-ref="pc" advice-ref="userTxAdvice" /></aop:config><!-- =================================================================================    -->

3. 添加 service 层


public interface UserService {        public void updateUser(User user);}public class UserServiceImpl implements UserService {    private UserDao userDao;        public void setUserDao(UserDao userDao) {        this.userDao = userDao;    }        @Override    public void updateUser(User user) {        userDao.updateUser(user);        System.out.println( 1/0 );    }}

4. junit test

 4.1 TestUserDao

/*<update id="updateUserByCondition" parameterType="User">update user<set><if test="name != null">name = #{name} ,</if><if test="address != null">address = #{address}</if></set>where id = #{id}</update> */@Testpublic void testUpdateUserByCondition() {UserDao userDao = (UserDao) context.getBean("userDao");User user = new User();user.setId("0001");user.setName("zhangsan44");userDao.updateUser(user);}
 4.2 TestUserService
@Testpublic void testUpdateUser() {ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");UserService userService = (UserService) context.getBean("userService");User user = new User();user.setId("0001");user.setName("zhangsan77");userService.updateUser(user);}

5. 完整的 ApplicationContext.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"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"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         http://www.springframework.org/schema/tx         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd         http://www.springframework.org/schema/aop          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"><!-- 配置数据源 简单数据源 --><bean id="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"/><property name="url" value="jdbc:mysql:///mybatis"/><property name="username" value="root"/><property name="password" value="root"/></bean><!-- SqlSessionFactory --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 数据源 --><property name="dataSource" ref="dataSource" /><!-- mybatis总配置文件的位置 --><property name="configLocation" value="classpath:sqlMapConfig.xml"/></bean><!-- ================================事务相关控制=================================================    --><bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"/></bean><tx:advice id="userTxAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="delete*" propagation="REQUIRED" /><tx:method name="save*" propagation="REQUIRED" /><tx:method name="update*" propagation="REQUIRED" /><tx:method name="find*" read-only="true" /><tx:method name="get*" read-only="true" /><tx:method name="select*" read-only="true" /></tx:attributes></tx:advice><aop:config><!-- 第一个"*"代表所有类,第二个"*"代表所有方法 ".."代表任意参数 --><aop:pointcut id="pc" expression="execution(* com.hehe.mybatis.service.*.*(..))" /><!--把事务控制在Service层--><aop:advisor pointcut-ref="pc" advice-ref="userTxAdvice" /></aop:config><!-- =================================================================================    --><bean id="userDao" class="com.hehe.mybatis.dao.impl.UserDaoImpl"><property name="sqlSessionFactory" ref="sqlSessionFactory" /></bean><bean id="userService" class="com.hehe.mybatis.service.impl.UserServiceImpl"><property name="userDao" ref="userDao"/></bean></beans>


0 0
原创粉丝点击