美文网首页mybatis-plus
9.MybatisPlus 高级部分简介

9.MybatisPlus 高级部分简介

作者: 神奇作手 | 来源:发表于2020-03-26 15:52 被阅读0次

    1.高级部分简介

    1.逻辑删除、自动填充、乐观锁
    2.性能分析插件、多租户SQL解析器
    3.动态表名SQL解析器、SQL注入器

    2.涉及技能

    1.Lamdba表达式
    2.熟悉springboot、Maven
    3.熟悉MP的核心功能
    4.JDK最低1.8

    3.基础数据表

    #创建用户表
    CREATE TABLE user (
        id BIGINT(20) PRIMARY KEY NOT NULL COMMENT '主键',
        name VARCHAR(30) DEFAULT NULL COMMENT '姓名',
        age INT(11) DEFAULT NULL COMMENT '年龄',
        email VARCHAR(50) DEFAULT NULL COMMENT '邮箱',
        manager_id BIGINT(20) DEFAULT NULL COMMENT '直属上级id',
        create_time DATETIME DEFAULT NULL COMMENT '创建时间',
        update_time DATETIME DEFAULT NULL COMMENT '修改时间',
        version INT(11) DEFAULT '1' COMMENT '版本',
        deleted INT(1) DEFAULT '0' COMMENT '逻辑删除标识(0.未删除,1.已删除)',
        CONSTRAINT manager_fk FOREIGN KEY (manager_id)
            REFERENCES user (id)
    )  ENGINE=INNODB CHARSET=UTF8;
    
    #初始化数据:
    INSERT INTO user (id, name, age, email, manager_id
        , create_time)
    VALUES (1087982257332887553, '大boss', 40, 'boss@baomidou.com', NULL
            , '2019-01-11 14:20:20'),
        (1088248166370832385, '王天风', 25, 'wtf@baomidou.com', 1087982257332887553
            , '2019-02-05 11:12:22'),
        (1088250446457389058, '李艺伟', 28, 'lyw@baomidou.com', 1088248166370832385
            , '2019-02-14 08:31:16'),
        (1094590409767661570, '张雨琪', 31, 'zjq@baomidou.com', 1088248166370832385
            , '2019-01-14 09:15:15'),
        (1094592041087729666, '刘红雨', 32, 'lhm@baomidou.com', 1088248166370832385
            , '2019-01-14 09:48:16');
    

    4.基本项目框架

    参考前面部分

    4.1. pom文件
    <?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.1.8.RELEASE</version>
            <relativePath/>
        </parent>
        <groupId>com.mp</groupId>
        <artifactId>hight_mp</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>hight_mp</name>
        <description>hight_mp</description>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <!-- springboot web启动器 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!-- mysql jdbc驱动 -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
            </dependency>
            <!-- lombok简化java代码 -->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <scope>provided</scope>
            </dependency>
            <!-- mybatis-plus启动器 -->
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>3.2.0</version>
            </dependency>
            <!-- commons-lang3 -->
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
                <version>3.9</version>
            </dependency>
            <!-- log4j -->
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.17</version>
            </dependency>
            <!-- springboot test启动器 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    </project>
    
    
    4.2. yml文件
    server:
      port: 8088
    
    spring:
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/MP?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8
        username: root
        password: 123456
    
    # mapper.xml文件位置
    mybatis-plus:
      #  指定mapper.xml文件位置
      mapper-locations: classpath*:mapper/*.xml
    
    logging:
      level:
        root: warn
        com.mp.dao: trace
      pattern:
        console: '%p%m%n'
    
    4.3. 实体类
    package com.mp.entity;
    
    import com.baomidou.mybatisplus.annotation.TableName;
    import lombok.Data;
    import lombok.experimental.Accessors;
    
    import java.time.LocalDateTime;
    
    /**
     * @Description 实体类
     * @Author LL
     * @Date 2020-03-26 16:10
     */
    @Data
    @Accessors(chain = true)
    @TableName("tb_user")
    public class User {
    
        private Long id;//主键
    
        private String name;//姓名
    
        private Integer age;//年龄
    
        private String email;//邮箱
    
        private Long managerId;//直属上级id
    
        private LocalDateTime createTime;//创建时间
    
        private LocalDateTime updateTime;//修改时间
    
        private Integer version;//版本
       
        private Integer deleted;//逻辑删除标识(0.未删除,1.已删除)
    }
    
    

    相关文章

      网友评论

        本文标题:9.MybatisPlus 高级部分简介

        本文链接:https://www.haomeiwen.com/subject/ihdquhtx.html