服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - 一篇超详细的Spring Boot整合Mybatis文章

一篇超详细的Spring Boot整合Mybatis文章

2021-10-24 14:10牛哄哄的柯南 Java教程

大家都知道springboot搭建一个spring框架只需要秒秒钟。下面通过实例代码给大家介绍一下springboot与mybatis的完美融合,非常不错,具有参考借鉴价值,感兴趣的朋友一起看看吧

配置文件形式

一篇超详细的Spring Boot整合Mybatis文章

pom.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelversion>4.0.0</modelversion>
    <parent>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-starter-parent</artifactid>
        <version>2.4.4</version>
        <relativepath/> <!-- lookup parent from repository -->
    </parent>
    <groupid>com.keafmd</groupid>
    <artifactid>spring-boot-09-mybatis</artifactid>
    <version>0.0.1-snapshot</version>
    <name>spring-boot-09-mybatis</name>
    <description>demo project for spring boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-jdbc</artifactid>
        </dependency>
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-web</artifactid>
        </dependency>
        <dependency>
            <groupid>org.mybatis.spring.boot</groupid>
            <artifactid>mybatis-spring-boot-starter</artifactid>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupid>mysql</groupid>
            <artifactid>mysql-connector-java</artifactid>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-test</artifactid>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupid>org.springframework.boot</groupid>
                <artifactid>spring-boot-maven-plugin</artifactid>
            </plugin>
        </plugins>
    </build>
 
</project>

配置数据源

在yml文件中配置数据源。

application.yml:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
server:
  port: 80
 
# 配置数据源
spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/ssm-java1?usessl=false
    driver-class-name: com.mysql.cj.jdbc.driver
    username: root
    password: 18044229
 
# 整合mybatis
mybatis:
  #  typealiasespackage: com.neuedu.entity
  mapper-locations: classpath*:com/neuedu/boot/mapper/*.xml

usermapper.xml

这里注意!!!:一定是和usermapper相同的目录,是个三级目录,创建时仿照这样创建com/keafm/mapper(正确的) 别这样com.keafam.mapper(错误的),这样错误的创建的话,是个一级目录,不是三级的,后面运行的时候可能会提示找不到mapper。

?
1
2
3
4
5
6
7
<?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="com.keafmd.mapper.usermapper">
    <select id="list" resulttype="map">
        select * from user
    </select>
</mapper>

usermapper

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.keafmd.mapper;
import org.apache.ibatis.annotations.mapper;
import java.util.list;
/**
 * keafmd
 *
 * @classname: usermapper
 * @description:
 * @author: 牛哄哄的柯南
 * @date: 2021-04-08 16:09
 * @blog: https://keafmd.blog.csdn.net/
 */
public interface usermapper {
    list list();
}

配置springboot整合mybatis

在application.yml中配置:

?
1
2
3
4
# 整合mybatis
mybatis:
  #  typealiasespackage: com.neuedu.entity
  mapper-locations: classpath*:com/neuedu/boot/mapper/*.xml

在运行类上添加@mapperscan注解

springboot09mybatisapplication:

?
1
2
3
4
5
6
7
8
9
10
11
package com.keafmd;
import org.mybatis.spring.annotation.mapperscan;
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
@springbootapplication
@mapperscan("com.keafmd.mapper")
public class springboot09mybatisapplication {
    public static void main(string[] args) {
        springapplication.run(springboot09mybatisapplication.class, args);
    }
}

测试类

usermappertest :

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.keafmd.mapper;
import com.keafmd.springboot09mybatisapplication;
import org.junit.jupiter.api.test;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.boot.test.context.springboottest;
import java.util.list;
import static org.junit.jupiter.api.assertions.*;
@springboottest(classes = springboot09mybatisapplication.class)
class usermappertest {
    @autowired
    usermapper usermapper;
    @test
    void list(){
        list list = usermapper.list();
        for (object o : list) {
            system.out.println(o);
        }
    }
}

效果

一篇超详细的Spring Boot整合Mybatis文章

总结

本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注服务器之家的更多内容!

原文链接:https://blog.csdn.net/weixin_43883917/article/details/115528174

延伸 · 阅读

精彩推荐