0
点赞
收藏
分享

微信扫一扫

spring整合mybatis及声明式事务

钟罗敏 2022-04-08 阅读 71
javaspring

Mybatis-spring

整合方式一:

1.编写数据源配置

<!--DataSource:使用spring的数据源替换Mybatis的配置 c3p0 dbcp druid-->
<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?useUnicode=true&amp;characterEncoding=UTF-8&amp;useSSL=false"/>
    <property name="username" value="root"/>
    <property name="password" value="123456"/>
</bean>

2.sqlSessionFactory

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

3.sqlSessionTemple

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
    <!--只能使用构造器注入,因为没有set方法-->
    <constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>

4.给接口添加实现类

public class UserMapperImpl implements UserMapper {
    //我们的所有的操作都使用sqlsession现在使用sqlsessiontemplate
    private SqlSessionTemplate seqSession;

    public void setSeqSession(SqlSessionTemplate seqSession) {
        this.seqSession = seqSession;
    }

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

5.给类注入到spring中

<bean id="UserMapper" class="com.liu.mapper.UserMapperImpl">
    <property name="seqSession" ref="sqlSession"/>
</bean>

6.测试

public void test1() throws IOException {
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    UserMapper userMapper = context.getBean("UserMapper", UserMapper.class);
    List<User> users = userMapper.selectUser();
    for (User user : users) {
        System.out.println(user);
    }

整合方式二:

继承SqlSessionDaoSupport

不再需要手动创建sqlsession

public class UserMapperImpl2  extends SqlSessionDaoSupport implements UserMapper {

    public List<User> selectUser() {

        return getSqlSession().getMapper(UserMapper.class).selectUser();
    }
}

只需要给其父类一个sqlSessionFactory即可

<bean id="UserMapper2" class="com.liu.mapper.UserMapperImpl2">
    <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>

声明式事务

事务原则:ACID

  • 原子性:确保要么都成功或者都失败

  • 隔离性:多个业务可能操作同一个资源,防止数据损坏

  • 一致性:事务完成都提交,或者都失败

  • 持久性:事务一旦完成,无论系统发生什么,结果都不会被影响,被持久化写入到存储器中 !

Spring中的事务管理

  • 声明式事务:AOP
  • 编程式事务:需要在代码中进行事务的管理

配置事务通知

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

aop织入事务

<!--结合AOP实现事务的织入-->
<!--配置事务通知-->
<tx:advice id="txadvice" transaction-manager="transactionMannager">
    <tx:attributes>
        <tx:method name="*" propagation="REQUIRED"/>
    </tx:attributes>
</tx:advice>

配置切入点

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

扩展 propagation七大属性:

  • REQUIRED:支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
  • SUPPORTS:支持当前事务,如果当前没有事务,就以非事务方式执行。
  • MANDATORY:支持当前事务,如果当前没有事务,就抛出异常。
  • REQUIRES_NEW:新建事务,如果当前存在事务,把当前事务挂起。
  • NOT_SUPPORTED:以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
  • NEVER:以非事务方式执行,如果当前存在事务,则抛出异常。
  • NESTED:支持当前事务,如果当前事务存在,则执行一个嵌套事务,如果当前没有事务,就新建一个事务。

为什么使用事务?

  • 如果不配置事务,可能出现数据提交不一致的情况
  • 如果不在spring中配置声明式事务,就需要在代码中手动配置
  • 涉及数据的一致性和完整性
举报

相关推荐

0 条评论