• 【SpringBoot】6.SpringBoot整合MybatisPlus


    1、新建项目

    开发环境:

    • jdk1.8
    • idea
    • mysql8.0
    • maven 3.6.3

    利用 idea 新建一个项目 springboot-mybatis-plus ,具体搭建过程,此处省略。

    2、引入依赖

    整个整合项目的 pom 依赖信息如下

    
    <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.0modelVersion>
        <parent>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
            <version>2.7.2version>
            <relativePath/> 
        parent>
        <groupId>com.yuhuofeigroupId>
        <artifactId>springboot-mybatis-plusartifactId>
        <version>0.0.1-SNAPSHOTversion>
        <name>springboot-mybatis-plusname>
        <description>Mybatis Plus for Spring Bootdescription>
        <properties>
            <java.version>1.8java.version>
        properties>
        <dependencies>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
                <scope>testscope>
            dependency>
    
            
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <scope>runtimescope>
            dependency>
    
            
            <dependency>
                <groupId>com.baomidougroupId>
                <artifactId>mybatis-plus-boot-starterartifactId>
                <version>3.5.1version>
            dependency>
    
            
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
                <optional>trueoptional>
            dependency>
    
        dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                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

    3、更改配置

    在 application.properties 文件中,加入以下配置:

    server.port=8081
    spring.datasource.username=root
    spring.datasource.password=pan
    spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    
    #整合mybatisPlus
    mybatis-plus.type-aliases-package=com.yuhuofei.entity
    mybatis-plus.mapper-locations=classpath:mapper/*.xml
    #开启驼峰字段映射
    mybatis-plus.configuration.map-underscore-to-camel-case=true
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    4、新建各个层级目录及对应的类

    entity层,新建一个 User.java

    package com.yuhuofei.entity;
    
    import com.baomidou.mybatisplus.annotation.TableField;
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    /**
     * @Description
     * @ClassName User
     * @Author yuhuofei
     * @Date 2022/8/6 0:34
     * @Version 1.0
     */
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class User {
    
        private Integer id;
    
        private String name;
    
        @TableField("password")
        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
    • 23
    • 24
    • 25
    • 26
    • 27

    在 mapper 层,新建 UserMapper.java

    package com.yuhuofei.mapper;
    
    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    import com.yuhuofei.entity.User;
    import org.apache.ibatis.annotations.Mapper;
    
    import java.util.List;
    
    /**
     * @Description
     * @InterfaceName UserMapper
     * @Author yuhuofei
     * @Date 2022/8/6 0:36
     * @Version 1.0
     */
    @Mapper
    public interface UserMapper extends BaseMapper<User> {
    
        List<User> queryUserList();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    在 resource 下,新建一个 mapper 目录,然后新建一个 userMapper.xml

    
    DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    
    <mapper namespace="com.yuhuofei.mapper.UserMapper">
        <resultMap id="userResultMap" type="com.yuhuofei.entity.User">
            <result column="id" property="id"/>
            <result column="name" property="name"/>
            <result column="password" property="passWord"/>
        resultMap>
    
        <sql id="Base_Column_List">
            id,name,password
        sql>
    
        <select id="queryUserList" resultType="com.yuhuofei.entity.User">
            select id,name,password
            from user
        select>
    
    mapper>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    service层,新建一个 UserService.java 接口

    package com.yuhuofei.service;
    
    import com.yuhuofei.entity.User;
    
    import java.util.List;
    
    /**
     * @Description
     * @InterfaceName UserService
     * @Author yuhuofei
     * @Date 2022/8/6 0:48
     * @Version 1.0
     */
    public interface UserService {
    
        List<User> queryUserList();
    
        List<User> queryUser();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    service 层接口实现类 UserServiceImpl.java

    package com.yuhuofei.service.impl;
    
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.yuhuofei.entity.User;
    import com.yuhuofei.mapper.UserMapper;
    import com.yuhuofei.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    
    /**
     * @Description
     * @ClassName UserServiceImpl
     * @Author yuhuofei
     * @Date 2022/8/6 0:50
     * @Version 1.0
     */
    @Service
    public class UserServiceImpl implements UserService {
    
        @Autowired
        private UserMapper userMapper;
    
        @Override
        public List<User> queryUserList() {
            return userMapper.queryUserList();
        }
    
        @Override
        public List<User> queryUser() {
            return userMapper.selectList(new QueryWrapper<User>().likeRight("name", "王"));
        }
    }
    
    
    • 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

    controller 层,新建一个 UserController.java

    package com.yuhuofei.controller;
    
    import com.yuhuofei.entity.User;
    import com.yuhuofei.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.List;
    
    /**
     * @Description
     * @ClassName UserController
     * @Author yuhuofei
     * @Date 2022/8/6 0:52
     * @Version 1.0
     */
    @RestController
    @RequestMapping("/user")
    public class UserController {
    
        @Autowired
        private UserService userService;
    
        @GetMapping("/queryAllUser")
        public List<User> queryAllUser() {
            return userService.queryUserList();
        }
    
        @GetMapping("/queryUser")
        public List<User> queryUser(){
            return userService.queryUser();
        }
    }
    
    
    • 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

    整个项目结构如下

    在这里插入图片描述

    5、测 试

    启动服务器,在浏览器输入 http://localhost:8081/user/queryAllUser ,得到的结果如下

    在这里插入图片描述

    启动服务器,在浏览器输入 http://localhost:8081/user/queryUser ,得到的结果如下

    在这里插入图片描述
    至此,初步整合完成,后续还可以在此基础上,增加日志打印的配置、自动生成代码、主键策略、自动填充字段等内容,这里不做赘述。

  • 相关阅读:
    使用TPDSS连接GaussDB数据库
    SpringBoot与Loki的那些事
    Oracle数据库体系结构(四)_存储参数
    《mysql》--mysql约束
    < Python全景系列-7 > 提升Python编程效率:模块与包全面解读
    扭线机控制
    在 Vue 中控制表单输入
    Vue驾校-从项目学Vue-6
    SecXOps 技术发展趋势
    LAL v0.35.4发布,OBS支持RTMP H265推流,我跟了
  • 原文地址:https://blog.csdn.net/Crezfikbd/article/details/126198322