使用Mybatis-plus

时间:2024-6-11    作者:老大夫    分类: SSM


主要就是在mapper接口中继承baseMapper泛型为映射的类,MybatisPlus提供了完全的ORM,提供的方法可以不用再写sql语句了。

  1. 准备数据库脚本

    现有一张 User 表,其表结构如下:

    id name age email
    1 Jone 18 test1@baomidou.com
    2 Jack 20 test2@baomidou.com
    3 Tom 28 test3@baomidou.com
    4 Sandy 21 test4@baomidou.com
    5 Billie 24 test5@baomidou.com
    DROP TABLE IF EXISTS user;
    
    CREATE TABLE user
    (
       id BIGINT(20) NOT NULL COMMENT '主键ID',
       name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
       age INT(11) NULL DEFAULT NULL COMMENT '年龄',
       email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
       PRIMARY KEY (id)
    );

    INSERT INTO user (id, name, age, email) VALUES
    (1, 'Jone', 18, 'test1@baomidou.com'),
    (2, 'Jack', 20, 'test2@baomidou.com'),
    (3, 'Tom', 28, 'test3@baomidou.com'),
    (4, 'Sandy', 21, 'test4@baomidou.com'),
    (5, 'Billie', 24, 'test5@baomidou.com');

  2. 准备boot工程

  3. 导入依赖

    <?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">
       <modelVersion>4.0.0</modelVersion>
    
       <parent>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-parent</artifactId>
           <version>3.0.5</version>
       </parent>
       <groupId>com.atguigu</groupId>
       <artifactId>springboot-starter-mybatis-plus-06</artifactId>
       <version>1.0-SNAPSHOT</version>
    
       <properties>
           <maven.compiler.source>17</maven.compiler.source>
           <maven.compiler.target>17</maven.compiler.target>
           <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
       </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    
        <!-- 测试环境 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    
        <!-- mybatis-plus  -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.3.1</version>
        </dependency>
    
        <!-- 数据库相关配置启动器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
    
        <!-- druid启动器的依赖  -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-3-starter</artifactId>
            <version>1.2.18</version>
        </dependency>
    
        <!-- 驱动类-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.28</version>
        </dependency>
    
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.28</version>
        </dependency>
    
    </dependencies>
    
    <!--    SpringBoot应用打包插件-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

  4. 配置文件和启动类

    完善连接池配置:

    文件夹:META-INF.spring

    文件名:

    org.springframework.boot.autoconfigure.AutoConfiguration.imports

    内容:com.alibaba.druid.spring.boot3.autoconfigure.DruidDataSourceAutoConfigure

    application.yaml

    # 连接池配置
    spring:
     datasource:
       type: com.alibaba.druid.pool.DruidDataSource
       druid:
         url: jdbc:mysql:///day01
         username: root
         password: root
         driver-class-name: com.mysql.cj.jdbc.Driver
    

    启动类

    @MapperScan("com.atguigu.mapper")
    @SpringBootApplication
    public class MainApplication {
    
       public static void main(String[] args) {
           SpringApplication.run(MainApplication.class,args);
       }
    
    }
    
  5. 功能编码

    编写实体类 User.java(此处使用了 Lombok 简化代码)

    @Data
    public class User {
       private Long id;
       private String name;
       private Integer age;
       private String email;
    }
    

    编写 Mapper 包下的 UserMapper接口

    public interface UserMapper extends BaseMapper<User> {
    
    }
    

    继承mybatis-plus提供的基础Mapper接口,自带crud方法!

  6. 测试和使用

    添加测试类,进行功能测试:

    @SpringBootTest //springboot下测试环境注解
    public class SampleTest {
    
       @Autowired
       private UserMapper userMapper;
    
       @Test
       public void testSelect() {
           System.out.println(("----- selectAll method test ------"));
           List<User> userList = userMapper.selectList(null);
           userList.forEach(System.out::println);
       }
    }
    
  7. 小结

    通过以上几个简单的步骤,我们就实现了 User 表的 CRUD 功能,甚至连 XML 文件都不用编写!

    从以上步骤中,我们可以看到集成MyBatis-Plus非常的简单,只需要引入 starter 工程,并配置 mapper 扫描路径即可。


扫描二维码,在手机上阅读

推荐阅读: