码库记事本

码库记事本

Mybatis-Plus快速入门

小诸哥 0

快速入门

创建数据库

  • 创建一个数据库db1

    1
    create database db1
  • 创建一个user表

    1
    2
    3
    4
    5
    6
    7
    8
    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)
    );
  • 插入数据

    1
    2
    3
    4
    5
    6
    INSERT INTO user (id, name, age, email) VALUES
    (1, 'Jone', 18, [email protected]'),
    (2, 'Jack', 20, [email protected]'),
    (3, 'Tom', 28, [email protected]'),
    (4, 'Sandy', 21, [email protected]'),
    (5, 'Billie', 24, [email protected] om');

编写主体类

  • 初始化工程(pom文件)

    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
    <?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.5.2</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.example</groupId>
        <artifactId>MyApplication</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>MyApplication</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</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>3.3.0</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.38</version>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
     
    </project>
  • application

    1
    2
    3
    4
    5
    6
    spring:
      datasource:
        username: root
        password: 123456
        url: jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
        driver-class-name: com.mysql.jdbc.Driver
  • User

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    package com.blue.bean;
     
    import lombok.Data;
     
    @Data
    public class User {
        private Long id;
        private String name;
        private Integer age;
        private String email;
    }
  • UserMapper

    1
    2
    3
    4
    5
    6
    7
    8
    package com.blue.mapper;
     
    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    import com.blue.bean.User;
     
    public interface UserMapper extends BaseMapper<User> {
     
    }
  • MyApplication

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    package com.blue;
     
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
     
    @SpringBootApplication
    @MapperScan("com.blue.mapper")
    public class MyApplication {
     
        public static void main(String[] args) {
            SpringApplication.run(MyApplication.class, args);
        }
    }

编写测试类

  • MyApplicationTests

    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
    package com.blue;
     
    import com.blue.bean.User;
    import com.blue.mapper.UserMapper;
    import org.junit.Assert;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
     
    import java.util.List;
     
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class MyApplicationTests {
     
        @Autowired
        private UserMapper userMapper;
     
        @Test
        public void testSelect() {
            System.out.println(("----- selectAll method test ------"));
            List<User> userList = userMapper.selectList(null);
            Assert.assertEquals(5, userList.size());
            userList.forEach(System.out::println);
        }
     
    }

实验结果

  • 控制台输出

标签: Mybatis-Plus 快速 入门