1. SpringBoot集成Mybatis


springboot集成mybatis需要以下几步:


1.1. 引入步骤

1.1.1. 引入pom依赖


<!--jdk版本及编码-->
<properties>
    <java.version>1.8</java.version> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.3</version>
    <relativePath/>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <!--web起步应用-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <scope>compile</scope>
    </dependency>
    <!--jackson依赖,版本应该大于2.7.0-->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.14.3</version>
    </dependency>
    <!--lombok-->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.20</version>
    </dependency>
    <!--mybatis起步依赖-->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.2.0</version>
    </dependency>
    <!--mysql驱动-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.47</version>
    </dependency>
    <!--可选:德鲁伊数据连接池-->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.2.9</version>
    </dependency>

    <!--单元测试-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.7.3</version>
        </plugin>
    </plugins>
</build>

1.1.2. 配置文件

在springboot配置文件application.properties中配置如下内容

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://ip:端口/数据库?characterEncoding=utf-8&userSSL=false
spring.datasource.username=root
spring.datasource.password=root

# 可选:把数据库连接池从默认的hikari切换为Druid(需要在依赖中引入Druid)
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

# xml映射文件的位置
mybatis.mapper-locations=classpath:路径/*.xml
# 自动将下划线转换为驼峰命名 a_column ------> aCloumn
mybatis.configuration.map-underscore-to-camel-case=true
  • 将所有的xml映射文件放在一个目录下,通过配置mybatis.mapper-locations=classpath:路径/*.xml项,可以实现由框架自动扫描该包下的xml映射文件。这样就不用把xml和mapper接口放在同一路径下了

或使用`application.yml`形式的配置文件

server:
  port: 8080

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://ip:端口/数据库?characterEncoding=utf-8&useSSL=false
    username: root
    password: root
    # 可选:把数据库连接池从默认的hikari切换为Druid(需要在依赖中引入Druid)
    type: com.alibaba.druid.pool.DruidDataSource

# 加载xml映射文件的位置
mybatis:
  mapper-locations: classpath:路径/*.xml
  configuration:
    # 日志输出
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    #开启mybatis的驼峰命名自动映射开关 a_column ------> aCloumn
    map-underscore-to-camel-case: true

1.2. 使用入门:单元测试

1.2.1. 编写测试用mapper

Mapper接口:

public interface UserMapper {
    User login(@Param("user") User user);
}

xml映射文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="link.xiaomo.boot.mapper.UserMapper">

    <resultMap id="userMap" type="link.xiaomo.boot.pojo.User" autoMapping="true">
        <!--id不能省略-->
        <id property="id" column="id" />
        <!--<result property="" column=""/>-->
    </resultMap>

    <select id="login" resultMap="userMap">
        select *
        from tb_user
        where username = #{user.username}
          and password = #{user.password}
    </select>
</mapper>

1.2.2. 启动类的包扫描配置

在启动类上,添加包扫描注释@MapperScan("mapper的包结构")

@SpringBootApplication
@MapperScan("mapper的包结构")
public class XxxApplication{
    public static void main(String[] args) {
        SpringApplication.run(XxxApplication.class, args);
    }
}

1.2.3. 编写单元测试类

在测试类的类名上添加@SpringBootTest(classes=启动类.class)

使用依赖注入的方式,获得mapper接口的对象。

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest(classes=启动类.class)
class 测试类名{
    @Autowired
    UserMapper userMapper;

    @Test
    public void 测试方法名(){
        结果 = userMapper.方法()
    }
}

**注意:**此处单元测试应该使用的SpringBoot的单元测试

如人饮水,冷暖自知。
最后更新于 2023-08-02