Spring中的事务管理


1.声明式事务:AOP(不影响原有代码)


UserMapper接口:

package com.kakafa.mapper;
import com.kakafa.pojo.User;
import java.util.List;

public interface UserMapper {

        public List<User> selectUser();

        //添加一个用户
        public int addUser(User user);

        //通过id删除一个用户
        public int deleteUser(int id);

}

UserMapper.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.kakafa.mapper.UserMapper">

    <select id="selectUser" resultType="user">
        select * from mybatistest.user;
    </select>

    <insert id="addUser" parameterType="user" >
        insert into mybatistest.user(id,name,pwd)
        values(#{id},#{name},#{pwd});
    </insert>

    <delete id="deleteUser" parameterType="int">
        delete from mybatistest.user where id=#{id};
    </delete>

</mapper>


UserMapperImpl:

package com.kakafa.mapper;
import com.kakafa.pojo.User;
import org.mybatis.spring.SqlSessionTemplate;
import java.util.List;

public class UserMapperImpl implements UserMapper{

    //我们所有操作都使用sqlSession来执行,现在都使用sqlSessionTemplate,因为习惯用sqlSession,所以这里命名为sqlSession
    private SqlSessionTemplate sqlSession;

    //需要有set方法才能够在spring的bean注册中注入sqlSession的值-通过set方法注入:<property name="sqlSession" ref="sqlSession"/>
    public void setSqlSession(SqlSessionTemplate sqlSession) {
        this.sqlSession = sqlSession;
    }

    @Override
    public List<User> selectUser() {
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = mapper.selectUser();
        return users;
    }

    @Override
    public int addUser(User user) {
        return sqlSession.getMapper(UserMapper.class).addUser(user);
    }

    @Override
    public int deleteUser(int id) {
        return sqlSession.getMapper(UserMapper.class).deleteUser(id);
    }
}

spring-dao.xml:注意头文件里要加入aop和tx的东西

<?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: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.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">


    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatistest?useSSL=false&amp;useUnicode=true&amp;CharacterEncoding=UTF8&amp;serverTimezone=GMT"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--绑定MyBatis配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="mapperLocations" value="classpath:com/kakafa/mapper/UserMapper.xml"/>
    </bean>


    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>

    <bean id="userMapperImpl" class="com.kakafa.mapper.UserMapperImpl">
        <property name="sqlSession" ref="sqlSession"/>
    </bean>

    <!--配置声明式事务-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <constructor-arg ref="dataSource"/>
    </bean>

    <!--结合AOP实现事务的织入-->
    <!--配置事务通知:-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!--给哪些方法配置事务,以及配置事务的传播特性propagation,默认是REQUIRED-->
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="insert*" propagation="REQUIRED"/>
            <tx:method name="select*" read-only="true"/>
            <!--*表示所有的方法-->
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>

    <!--配置事务的切入-->
    <aop:config>
        <aop:pointcut id="txPointcut" expression="execution(* com.kakafa.mapper.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    </aop:config>


</beans>

Spring中的事务管理



2.编程式事务:需要在代码中进行事务的管理



上一篇:MyBatis中map的应用&&模糊查询


下一篇:mybatis 使用注解开发