一篇文章带你学习Mybatis-Plus(新手入门)

时间:2022-06-16 16:53:38

Mybatis-Plus

 

1.快速入门地址:安装 | MyBatis-Plus (baomidou.com)

 

2.创建数据库mybatis-plus

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");

以上数据的运行均在cmd命令运行

 

3.创建springboot项目,配置yml文件

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/mybatis_plus?userSSL=false&Unicode=true&CharacterEncoding=utf-8&serverTimezone=GMT%2B8
    driver-class-name: com.mysql.cj.jdbc.Driver

 

4.导入依赖

<?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.sxau</groupId>
    <artifactId>mybatis_plus</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mybatis_plus</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-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
<!--    mysql-connector-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
<!--        mybatis-plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.0</version>
        </dependency>
<!--        lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

 

5.创建pojo包,新建User实体类

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    //主键自增配合 数据库主键自增使用
    @TableId(type = IdType.AUTO)
    private Long id;
    private String name;
    private int age;
    private String email;
}

 

6.创建mapper包,新建UserMapper实体类

package com.sxau.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sxau.pojo.User;
import org.springframework.stereotype.Repository;
@Repository
public interface Usermapper extends BaseMapper<User> {
    //所有CRUD操作已经完成
    //不需要一大堆的。xml配置文件
}

 

7.在主启动类中加上Mapperscan注解扫描mapper映射文件

@MapperScan("com.sxau.mapper")

 

8.查询所有数据测试类

package com.sxau.mybatis_plus;
import com.baomidou.mybatisplus.annotation.IdType;
import com.sxau.mapper.Usermapper;
import com.sxau.pojo.User;
import org.junit.jupiter.api.Test;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@MapperScan("com.sxau.mapper")
@SpringBootTest
class MybatisPlusApplicationTests {
    @Autowired
    Usermapper usermapper;
    @Test
    void contextLoads() {
        List<User> users = usermapper.selectList(null);
        users.forEach(System.out::println);
    }
}

遇到这种情况,直接解决

一篇文章带你学习Mybatis-Plus(新手入门)

一篇文章带你学习Mybatis-Plus(新手入门)

 

9.开启mybatis-plus日志配置文件

#mybatis-plus日志配置文件
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

一篇文章带你学习Mybatis-Plus(新手入门)

 

10.主键生成策略

默认 ID_WORKER 全局唯一id 对应数据库中的主键(uuid.自增id.雪花算法.redis.zookeeper)

分布式系统唯一id生成:https://www.cnblogs.com/haoxinyue/p/5208136.html

雪花算法