Spring 整合 MyBatis

一、第一种方式:纯粹使用配置文件

1.1 基本流程

(1)在 Spring 的相关配置文件中配置 数据源(DataSource)
(2)在 Spring 的配置文件中配置 SqlSessionFactoryBean
(3)在 Spring 的配置文件中配置 SqlSessionTemplate
(4)为 dao 层接口创建实现类,在对应的实现类中定义 SqlSessionTemplate 字段,然后在各个方法中使用 SqlSessionTemplate来实现对数据库的访问。
(5)在 Spring 的配置文件中将 dao 层实现类注入,并且将 SqlSessionTemplate 字段注入
使用 MyBatis

1.2 实操

(1)相关条件的准备

pom.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>MyBatis</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>spring-mybatis</artifactId>
    <dependencies>
        <!-- junit 包 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
        </dependency>

        <!-- mybatis 包-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.5</version>
        </dependency>

        <!-- mysql 连接数据库的包 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.21</version>
        </dependency>

        <!-- druid 数据库连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.23</version>
        </dependency>

        <!-- spring 相关的包 -->
        <!-- 导入 webmvc 会将其他 spring 的模块全部导入 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.7.RELEASE</version>
        </dependency>
        <!-- 导入 spring-jdbc 相关的包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>

        <!-- aspectJ AOP 织入器 -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.6</version>
        </dependency>

        <!-- mybatis-spring 整合包【重要】 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.5</version>
        </dependency>

        <!-- log4j 包 -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
    </dependencies>

    <build>
        <!-- 设置不需要过滤资源文件的目录 -->
        <resources>
            <resource>
                <directory>${basedir}/src/main/resources</directory> <!-- 指定目录 -->
                <includes>																					 <!-- 指定需要包括的文件 -->
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>													<!-- 指定是否过滤 -->
            </resource>
            <resource>
                <directory>${basedir}/src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>
</project>

MyBatis 核心配置文件(mybatis-config.xml)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<!-- 这个设置需不需要都可以,如果需要使用日志,可以使用该 setting 标签 -->
    <settings>
        <setting name="logImpl" value="LOG4J"/>
    </settings>
</configuration>

数据库相关的配置文件(db.properties)

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai
username=wang
password=wang
(2)spring 配置文件: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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
	<!-- 引用了 db.properties 文件 -->
    <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>

    <!-- 0、配置相关的数据源,这里使用 druid 数据库连接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${driver}"></property>
        <property name="url" value="${url}"></property>
        <property name="username" value="${username}"></property>
        <property name="password" value="${password}"/>
    </bean>

    <!-- 1、创建 SqlSessionFactory 对象-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 配置数据源 -->
        <property name="dataSource" ref="dataSource"></property>
        <!-- 指定相关的 MyBatis 配置文件的地址 -->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        <!-- 配置映射文件 -->
        <property name="mapperLocations" value="classpath:wang/guang/dao/*.xml"></property>
    </bean>

    <!-- 2、通过 SqlSessionFactory 对象来创建 SqlSessionTemplate 对象 -->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>
    </bean>
</beans>
(3)dao 层的接口 与 接口的实现类

接口

/**
 * dao 层的接口
 */
public interface IAccountMapper {
       /**
     * 使用注解实现查询所有账户信息
     * @return
     */
    List<Account> findAll();
}

接口的实现类

package wang.guang.dao;

import org.mybatis.spring.SqlSessionTemplate;
import wang.guang.pojo.Account;

import java.util.List;

public class AccountMapperImpl implements IAccountMapper{

    // 定义 SqlSessionTemplate 对象,并且提供注入的方法
    private SqlSessionTemplate sqlSessionl;
    public void setSqlSessionl(SqlSessionTemplate sqlSessionl) {
        this.sqlSessionl = sqlSessionl;
    }

    // 实现了 dao 层接口的方法,内部实际上是利用了 MyBatis 来实现对数据库的访问
    @Override
    public List<Account> findAll() {
        IAccountMapper accountMapper = sqlSessionl.getMapper(IAccountMapper.class);
        return accountMapper.findAll();
    }
}
(4)spring 的核心配置文件
<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 引入其他的 spring 核心配置文件 -->
    <import resource="spring-dao.xml"></import>
    <!-- 将接口实现类注入spring 容器中,并且为将 SqlSessionTemplate 作为字段注入其中 -->
    <bean id="accountMapperImpl" class="wang.guang.dao.AccountMapperImpl">
        <property name="sqlSessionl" ref="sqlSession"></property>
    </bean>

</beans>

二、第二种方式:继承 SqlSessionDaoSupport 类

2.1 基本流程

第二中方式是第一中方法的简化形式,原理上与第一种方法一致。在 SqlSessionDaoSupport 中维护了一个 SqlSessionTemplate 对象,在其中有传入 SqlSessionFactory 的 set 方法,在该方法中会将 SqlSessionTemplate 对象进行初始化。所以如果我们的实现类继承了抽象类,则会继承该 set 方法,然后我们在构造该实现类时只需要传入一个 SqlSessionFactory 对象进去即可。

(1)在 Spring 配置文件中配置 数据源(DataSource)
(2)在 Spring 配置文件中配置 SqlSessionFactoryBean——需要使用到数据源
(3)在 dao 层创建接口,然后创建接口的实现类,并且将该类 extends SqlSessionDaoSupport 类
(4)在 Spring 配置文件中将 接口的实现类 注入容器中,并且在将 SqlSessionFactory 也作为属性注入其中。

2.2 实操

(1)前期的准备

内容与第一种方式中的一致,这里就不过多显示。

(2)配置 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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>  <!-- 这里引入了上面的数据库配置文件 -->

    <!-- 1、配置相关的数据源 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${driver}"></property>
        <property name="url" value="${url}"></property>
        <property name="username" value="${username}"></property>
        <property name="password" value="${password}"/>
    </bean>

    <!-- 2、创建 SqlSessionFactory 对象-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 配置数据源 -->
        <property name="dataSource" ref="dataSource"></property>
        <!-- 指定相关的 MyBatis 配置文件的地址 -->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        <!-- 配置映射文件 -->
        <property name="mapperLocations" value="classpath:wang/guang/dao/*.xml"></property>
    </bean>
</beans>
(3)对应 dao 层接口和 实现类

这里重点需要注意的是实现类需要继承 SqlSessionDaoSupport 这个抽象类。
dao 层接口

/**
 * dao 层的接口
 */
public interface IAccountMapper {
       /**
     * 使用注解实现查询所有账户信息
     * @return
     */
    List<Account> findAll();
}

dao 层接口的实现类

/**
 * @author 王光浩
 * @describe IAccountMapper 接口的实现类
 * 需要注意:实现类继承了 SqlSessionDaoSupport 这个抽象类
 */
public class IAccountMapperImpl extends SqlSessionDaoSupport implements IAccountMapper {
    @Override
    public List<Account> findAll() {
        SqlSessionTemplate sqlSessionTemplate = getSqlSessionTemplate();
        return sqlSessionTemplate.getMapper(IAccountMapper.class).findAll();
    }
}
(4)Spring 的核心配置文件
<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 引入其他的 spring 核心配置文件 -->
    <import resource="spring-dao.xml"></import> <!-- 一般我们将 spring 与不同框架整合的配置文件作为外置的 spring 配置文件,然后导入到核心 spring 配置文件中 -->
    <!-- 注册其他的 bean 对象 -->
    <bean id="iaccountMapperImpl" class="wang.guang.dao.IAccountMapperImpl">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> <!-- 这里需要将 SqlSessionFactory 对象注入到其中 -->
    </bean>
</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值