【Spring框架】整合MyBatis的两种方式+声明式事务管理

前言:
学习自https://www.bilibili.com/video/BV1WE411d7Dv?p=27
在【整合MyBatis的两种方式】部分,因Spring配置方式很灵活,可以用注解,或者xml,或者混合使用,因此,只了解本文整体的思路即可,不必追究某处该用@Bean,@Autowired,或是XML方式更合适,等等

【整合MyBatis的两种方式】

方式一

步骤

1、配置spring-dao.xml
(1)dataSource
(2)sqlSessionFactory
(3)sqlSessionTemplate

2、编写实体类

3、编写Mappr接口

4、编写Mapper接口实现类,将sqlSessionTemplate注入此对应属性的set方法

5、集成主配置文件applicationContext.xml,将spring-dao.xml引入,将bean(Mapper实现类)注入

代码示例

1、配置spring-dao.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--dataSource-->
    <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/mybatisstudy?
        useSSL=true&amp;useUnicode=true&amp;characterEncoding=utf8"/>
        <property name="username" value="root"/>
        <property name="password" value=""/>
    </bean>

    <!--sqlSessionFactory:
        可在其中用property标签实施以前在mybatis核心配置文件中的settings标签配置
    -->
    <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:mapper/*.xml"/>
    </bean>

    <!--SqlSessionTemplate:
        SqlSessionTemplate 是 MyBatis-Spring 的核心。
        作为 SqlSession 的一个实现,这意味着可以使用它无缝代替你代码中已经在使用的 SqlSession
    -->
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <!--SqlSessionTemplate类只有构造方法,无set方法-->
        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>

</beans>

2、编写实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class User {
    private int id;
    private String name;
    private String pwd;
}

3、编写Mappr接口

public interface UserMapper {
    List<User> getAllUsers();
}

4、编写Mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zlc.mapper.UserMapper">
    <select id="getAllUsers" resultType="user">
        SELECT * FROM user
    </select>
</mapper>

5、编写Mapper接口实现类

public class UserMapperImpl implements UserMapper {

    private SqlSessionTemplate sqlSessionTemplate;

    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
        this.sqlSessionTemplate = sqlSessionTemplate;
    }

    @Override
    public List<User> getAllUsers() {
        UserMapper mapper = sqlSessionTemplate.getMapper(UserMapper.class);
        return mapper.getAllUsers();
    }
}

6、集成主配置文件

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

    <import resource="spring-dao.xml"/>
    
    <bean id="userMapper" class="com.zlc.mapper.UserMapperImpl">
        <property name="sqlSessionTemplate" ref="sqlSessionTemplate"/>
    </bean>

</beans>

测试

@Test
public void test() {
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    UserMapper userMapper = ctx.getBean("userMapper", UserMapper.class);
    List<User> users = userMapper.getAllUsers();
    for (User user : users) {
        System.out.println(user);
    }
}

在这里插入图片描述

方式二

在方式一的基础上更改

1、更改spring-dao.xml,删除SqlSessionTemplate的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--dataSource-->
    <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/mybatisstudy?
        useSSL=true&amp;useUnicode=true&amp;characterEncoding=utf8"/>
        <property name="username" value="root"/>
        <property name="password" value=""/>
    </bean>

    <!--sqlSessionFactory:
        可在其中用property标签实施以前在mybatis核心配置文件中的settings标签配置
    -->
    <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:mapper/*.xml"/>
    </bean>

</beans>

2、更改Mapper接口实现类,使其继承SqlSessionDaoSupport,从而可以使用getSqlSession()方法

public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper{
    @Override
    public List<User> getAllUsers() {
        SqlSession sqlSession = getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        return mapper.getAllUsers();
    }
}

3、更改applicationContext.xml中Mapper bean的属性(这个属性,是它的父类的属性)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <import resource="spring-dao.xml"/>

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

</beans>

【Spring事务】

事务管理的方法

1、声明式事务管理

在这里插入图片描述

(下图来源:https://blog.csdn.net/sayoko06/article/details/79164858)
在这里插入图片描述

(其他方法…)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

超周到的程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值