Mybatis-Spring整合02(事务管理)

前提: Mybatis-Spring整合01中我们使用的Mybtis的写法,现在我们将它整合到Spring当中

1.我们将Mybtis注册到Spring-dao.xml当中

import com.feng.pojo.User;
import org.mybatis.spring.SqlSessionTemplate;
import java.util.List;

public class UserMapperImpl implements UserMapper {
//    原来的操作都是sqlsession来进行,现在都是有sqlsessiontemplate来进行
    private SqlSessionTemplate sqlSession;

    public void setSqlSession(SqlSessionTemplate sqlSession) {
        this.sqlSession = sqlSession;
    }

    public List<User> selectUser() {
        User user =new User(8,"Black","9876540");
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.addUser(user);
        mapper.deleteUser(8);
        return mapper.selectUser();
    }

    public int deleteUser(int id) {
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.deleteUser(id);
        return 0;
    }

    public int addUser(User user) {
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.addUser(user);
        return 0;
    }
}

这是实现了UserMapper接口的实体类1

import com.feng.pojo.User;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import java.util.List;

public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper {

    public List<User> selectUser() {
        UserMapper userMapper = getSqlSession().getMapper(UserMapper.class);
        return userMapper.selectUser();
    }

    public int deleteUser(int id) {
        return getSqlSession().getMapper(UserMapper.class).deleteUser(id);
    }

    public int addUser(User user) {
        return  getSqlSession().getMapper(UserMapper.class).addUser(user);
    }
}

这是实现了UserMapper接口的实体类2(特殊的地方:继承SqlSessionDaoSupport下面它的注入和实体类1会有所不同

<?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"
       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">
<!--使用Spring的数据源替换Mybatis的数据源,这里使用Spring提供的Jdbc,此处是固定格式写死的-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSl=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
        <property name="username" value="填入所对应数据库的名字"/>
        <property name="password" value="填入所对应数据库的密码"/>
    </bean>
<!--sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
<!--绑定Mybatis文件(可绑可不绑定,最好绑定),更改这里的classpath就可以在项目中直接套用-->
        <property name="configLocation" value="classpath:Mybatis-config.xml"/>
        <property name="mapperLocations" value="classpath:com/feng/Mapper/*.xml"/>
    </bean>
<!--SqlSessionTemplate就是我们使用的sqlSession-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!--只能使用构造器注入,因为它没有Set方法-->
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>

    <bean id="userMapper" class="com.feng.Mapper.UserMapperImpl">
        <property name="sqlSession" ref="sqlSession"/>
    </bean>
</beans>

以上部分是固定写死部分,接下来我们注册对象到里面并开启事务

 <bean id="userMapper" class="com.feng.Mapper.UserMapperImpl">
        <property name="sqlSession" ref="sqlSession"/>
    </bean>

    <bean id="userMapper2" class="com.feng.Mapper.UserMapperImpl2">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>
<!--配置声明式事务-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <constructor-arg ref="dataSource" />
    </bean>
<!--结合Aop实现事务的织入-->
<!--配置事务的通知-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
<!--给哪些方法配置事务-->
<!--配置事务传播的新特性:new propagation(默认REQUIRED)所以一般不写-->
        <tx:attributes>
<!--            <tx:method name="add" propagation="REQUIRED"/>-->
<!--            <tx:method name="delete"/>-->
<!--            <tx:method name="update"/>-->
<!--            <tx:method name="query"/>-->
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
<!--配置事务切入-->
    <aop:config>
        <aop:pointcut id="txpointcut" expression="execution(* com.feng.Mapper.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txpointcut"/>
    </aop:config>

在这里插入图片描述

数据不一致: 删除语句和添加语言写在了一起,删除语句出现错误执行失败,但是却把添加用户写入了数据库;

测试(这里把Spring-dao.xml交给了apapplicationContext.xml管理):
apapplicationContext.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:aop="http://www.springframework.org/schema/aop"
       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">
    <!--使用Spring的数据源替换Mybatis的数据源,这里使用Spring提供的Jdbc,此处是固定格式写死的-->
    <import resource="Spring-dao.xml"/>

</beans>
public class Mytest {
    @Test
    public void test() throws IOException {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserMapper userMapper = context.getBean("userMapper",UserMapper.class);
        List<User> users = userMapper.selectUser();
        userMapper.addUser(new User(10,"我是新加的","9876540"));
        for (User user : users) {
            System.out.println(user);
        }
    }
}

遇到的坑:在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值