Spring -> Mybatis事务注解(@Transactional)实现回滚

1.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:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--配置数据源-->
    <!--数据源配置一次就不会变了-->
    <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=utf8"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>
    <!--配置sqlSessionFactory-->
    <!--配置一次就不会变了 mapper.xml可以放在任何位置都可以,只要是xxMapper.xml-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--关联Mybatis的mapper.xml-->
        <property name="mapperLocations" value="classpath*:**/**Mapper.xml"/>
    </bean>

    <!--配置事务管理器,且给属性dataSource输入指定的数据源-->
    <bean name="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--开启事务注解-->
    <tx:annotation-driven/>
</beans>

2.mapper接口,mapper实现类(@Transactional),mapper.xml

public interface BlogMapper {
  void setSelect(@Param("author") String author, @Param("id") int id);
}
//继承SqlSessionDaoSupport类,这个类的getSqlSession方法是获取session对象的
//获取session对象后,就正常的mybatis的getMapper.sql方法
public class BlogImp extends SqlSessionDaoSupport implements BlogMapper {

  @Transactional
  @Override
  public void setSelect(String author, int id) {
    getSqlSession().getMapper(BlogMapper.class).setSelect(author, id);
    //手动异常,看看会不会回滚
    int a = 10 / 0;
  }
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD com.Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mapper.BlogMapper">
    <update id="setSelect">
        update blog set author=#{author} where id=#{id}
    </update>
</mapper>

4.测试类

public class Test7 {
  @Test
  public void name() {
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationconfig.xml");
    BlogMapper mapper = (BlogMapper) context.getBean("blogImp");
    mapper.setSelect("ld", 5);
  }
}

5.结果是加了注解,事务配置了xml,是会回滚的

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值