• 18-SpringBoot 整合MyBatis-Plus


    1.MyBatisX插件

    在使用mybatis或者mybatis-plus时,我们可以安装IDEA的MyBatis的插件 - MyBatisX,
    这样我们就可以实现点击接口跳转到sql文件, 点击sql文件可以跳转到接口的功能, 很方便.这个插件的功能还有很多, 可以查看MyBatis-Plus官网

    安装方法:打开 IDEA,进入 File -> Settings -> Plugins -> Browse Repositories,输入 mybatisx 搜索并安装, 然后重启IDEA。
    在这里插入图片描述

    点击小鸟可实现跳转
    在这里插入图片描述

    在这里插入图片描述

    2.引入依赖

    在pom.xml添加如下依赖

     <!--数据库相关-->
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>3.4.1</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.6</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
                <version>1.1.17</version>
            </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    3.编写配置

    application.properties

    server.port=8080
    spring.datasource.url=jdbc:mysql://localhost:3306/db_springtest?useUnicode=true&characterEncoding=utf8
    spring.datasource.username=root
    spring.datasource.password=123456
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    
    #----------------------------配置mybatis-plus---------------------------
    #配置sql文件路径
    mybatis-plus.mapper-locations=classpath:/mapper/*.xml
    #开启驼峰命名映射
    mybatis-plus.configuration.map-underscore-to-camel-case=true
    ## 自定义sql中表名带前缀, 默认是实体名的小写, 如user, 但是数据库中是t_user, 所以设置加上前缀
    mybatis-plus.global-config.db-config.table-prefix=t_
    
    #----------------------------配置druid--------------------------------
    #监控SpringBean
    spring.datasource.druid.aop-patterns=com.limi.springboottest2.*  
    # 底层开启功能,stat(sql监控),wall(防火墙)
    spring.datasource.druid.filters=stat,wall
    
    # 配置监控页功能
    spring.datasource.druid.stat-view-servlet.enabled=true
    spring.datasource.druid.stat-view-servlet.login-username=admin
    spring.datasource.druid.stat-view-servlet.login-password=123456
    spring.datasource.druid.stat-view-servlet.reset-enable=false
    
    # 监控web
    spring.datasource.druid.web-stat-filter.enabled=true
    spring.datasource.druid.web-stat-filter.url-pattern=/*
    spring.datasource.druid.web-stat-filter.exclusions='*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'
    
    # 对上面filters里面的stat的详细配置
    spring.datasource.druid.filter.stat.slow-sql-millis=1000
    spring.datasource.druid.filter.stat.log-slow-sql=true
    spring.datasource.druid.filter.stat.enabled=true
    spring.datasource.druid.filter.wall.enabled=true
    spring.datasource.druid.filter.wall.config.drop-table-allow=false
    
    • 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

    4.编写接口

    在这里插入图片描述
    UserMapper

    package com.limi.springboottest2.mapper;
    
    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    import com.limi.springboottest2.entity.User;
    import org.springframework.stereotype.Repository;
    
    @Repository
    public interface UserMapper extends BaseMapper<User> {
    
        User getUserById(Integer id);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    在这里插入图片描述
    UserMapper.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">
    <!--
            namespace:必须有值,自定义的唯一字符串
            推荐使用:dao 接口的全限定名称 -->
    <mapper namespace="com.limi.springboottest2.mapper.UserMapper">
    
        <select id="getUserById" resultType="com.limi.springboottest2.entity.User">
            select * from t_user where id = #{id}
        </select>
    </mapper>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    注意要开启mapper接口文件的扫描
    SpringbootTest2Application

    package com.limi.springboottest2;
    
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.ConfigurableApplicationContext;
    
    @SpringBootApplication
    @MapperScan("com.limi.springboottest2.mapper")//扫描mapper
    public class SpringbootTest2Application {
    ;
        public static void main(String[] args) {
    
            //1、返回我们IOC容器
            ConfigurableApplicationContext run = SpringApplication.run(SpringbootTest2Application.class, args);
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    5.运行测试

    HelloController

    package com.limi.springboottest2.controller;
    
    
    import com.limi.springboottest2.entity.User;
    import com.limi.springboottest2.mapper.UserMapper;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import java.util.List;
    
    @Controller
    public class HelloController {
    
        @Autowired
        private UserMapper userMapper;
    
        //测试自己编写sql语句的查询方法
        @ResponseBody
        @GetMapping("/getUser/{id}")
        public User test1(@PathVariable("id") Integer id){
            User user = userMapper.getUserById(id);
    
            return user;
        }
        
        //测试mybatis-plus内置的查询方法
        @ResponseBody
        @GetMapping("/getAllUser")
        public List<User> test2(){
            List<User> userList = userMapper.selectList(null);//查询所有
            return userList;
        }
    }
    
    
    • 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

    在这里插入图片描述
    在这里插入图片描述

    6.完整代码

    在这里插入图片描述
    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 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.7.0</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.limi</groupId>
        <artifactId>springboot-test2</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>springboot-test2</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-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <optional>true</optional>
            </dependency>
    
            <!--数据库相关-->
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>3.4.1</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.6</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
                <version>1.1.17</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <excludes>
                            <exclude>
                                <groupId>org.projectlombok</groupId>
                                <artifactId>lombok</artifactId>
                            </exclude>
                        </excludes>
                    </configuration>
                </plugin>
    
                <!-- 下面插件作用是工程打包时,不将spring-boot-configuration-processor打进包内,让其只在编码的时候有用 -->
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <excludes>
                            <exclude>
                                <groupId>org.springframework.boot</groupId>
                                <artifactId>spring-boot-configuration-processor</artifactId>
                            </exclude>
                        </excludes>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
    </project>
    
    
    • 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
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97

    application.properties

    server.port=8080
    spring.datasource.url=jdbc:mysql://localhost:3306/db_springtest?useUnicode=true&characterEncoding=utf8
    spring.datasource.username=root
    spring.datasource.password=123456
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    
    #----------------------------配置mybatis-plus---------------------------
    #配置sql文件路径
    mybatis-plus.mapper-locations=classpath:/mapper/*.xml
    #开启驼峰命名映射
    mybatis-plus.configuration.map-underscore-to-camel-case=true
    ## 自定义sql中表名带前缀, 默认是实体名的小写, 如user, 但是数据库中是t_user, 所以设置加上前缀
    mybatis-plus.global-config.db-config.table-prefix=t_
    
    #----------------------------配置druid--------------------------------
    #监控SpringBean
    spring.datasource.druid.aop-patterns=com.limi.springboottest2.*  
    # 底层开启功能,stat(sql监控),wall(防火墙)
    spring.datasource.druid.filters=stat,wall
    
    # 配置监控页功能
    spring.datasource.druid.stat-view-servlet.enabled=true
    spring.datasource.druid.stat-view-servlet.login-username=admin
    spring.datasource.druid.stat-view-servlet.login-password=123456
    spring.datasource.druid.stat-view-servlet.reset-enable=false
    
    # 监控web
    spring.datasource.druid.web-stat-filter.enabled=true
    spring.datasource.druid.web-stat-filter.url-pattern=/*
    spring.datasource.druid.web-stat-filter.exclusions='*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'
    
    # 对上面filters里面的stat的详细配置
    spring.datasource.druid.filter.stat.slow-sql-millis=1000
    spring.datasource.druid.filter.stat.log-slow-sql=true
    spring.datasource.druid.filter.stat.enabled=true
    spring.datasource.druid.filter.wall.enabled=true
    spring.datasource.druid.filter.wall.config.drop-table-allow=false
    
    • 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

    UserMapper.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">
    <!--
            namespace:必须有值,自定义的唯一字符串
            推荐使用:dao 接口的全限定名称 -->
    <mapper namespace="com.limi.springboottest2.mapper.UserMapper">
    
        <select id="getUserById" resultType="com.limi.springboottest2.entity.User">
            select * from t_user where id = #{id}
        </select>
    </mapper>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    SpringbootTest2Application

    package com.limi.springboottest2;
    
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.ConfigurableApplicationContext;
    
    @SpringBootApplication
    @MapperScan("com.limi.springboottest2.mapper")//扫描mapper
    public class SpringbootTest2Application {
    ;
        public static void main(String[] args) {
    
            //1、返回我们IOC容器
            ConfigurableApplicationContext run = SpringApplication.run(SpringbootTest2Application.class, args);
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    User

    package com.limi.springboottest2.entity;
    
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import lombok.ToString;
    
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @ToString
    public class User {
    
        private Integer id;
    
        private String userName;
    
        private String passWord;
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    UserMapper

    package com.limi.springboottest2.entity;
    
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import lombok.ToString;
    
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @ToString
    public class User {
    
        private Integer id;
    
        private String userName;
    
        private String passWord;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    HelloController

    package com.limi.springboottest2.controller;
    
    
    import com.limi.springboottest2.entity.User;
    import com.limi.springboottest2.mapper.UserMapper;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import java.util.List;
    
    @Controller
    public class HelloController {
    
        @Autowired
        private UserMapper userMapper;
    
        //测试自己编写sql语句的查询方法
        @ResponseBody
        @GetMapping("/getUser/{id}")
        public User test1(@PathVariable("id") Integer id){
            User user = userMapper.getUserById(id);
    
            return user;
        }
    
        //测试mybatis-plus内置的查询方法
        @ResponseBody
        @GetMapping("/getAllUser")
        public List<User> test2(){
            List<User> userList = userMapper.selectList(null);//查询所有
            return userList;
        }
    }
    
    
    • 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
  • 相关阅读:
    商淘云:如何选择开源B2B2C多用户商城系统
    软件测试之Web项目实战解析
    怎么把录音转文字?只需三步,手把手教会你
    数据可视化:视觉感知与基本图表
    分组查询以及筛选的区别
    Spring(十)- Spring Bean的基本注解
    浙大陈越何钦铭数据结构08-图7 公路村村通【循环和最小堆版】
    Prerender.io 配置过程 给你的VUE单页面网站增加一点seo吧~
    分布式 ID
    Xilinx(AMD) 7系列FPGA——主BPI配置模式
  • 原文地址:https://blog.csdn.net/qq_41865229/article/details/125416345