我是靠谱客的博主 专注大象,最近开发中收集的这篇文章主要介绍mybatis,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

目录

    • 第一章 MyBatis-Plus3概述
      • 1.1、简介
      • 1.2、特性
      • 1.3、框架结构
      • 1.4、项目地址
      • 1.5、版本介绍
      • 1.6、快速安装
      • 1.7、开发环境
    • 第二章 MyBatis-Plus3增删改查
      • 2.1、项目搭建
      • 2.2、项目配置(1)
      • 2.3、项目配置(2)
      • 2.4、数据导入
      • 2.5、创建实体
      • 2.6、创建接口
      • 2.7、测试准备
      • 2.8、增删改查
        • 2.8.1、insert
        • 2.8.2、updateById
        • 2.8.3、selectById
        • 2.8.4、selectByMap
        • 2.8.5、selectBatchIds
        • 2.8.6、deleteById
        • 2.8.7、deleteByMap
        • 2.8.8、deleteBatchIds
    • 第三章 MyBatis-Plus3注解介绍
      • 3.1、@TableName
      • 3.2、@TableId
      • 3.3、@TableField
      • 3.4、@Version
      • 3.5、@EnumValue
      • 3.6、@TableLogic
      • 3.7、@SqlParser
      • 3.8、@KeySequence
    • 第四章 MyBatis-Plus3条件构造器
      • 4.1、数据导入
      • 4.2、构造器简介
      • 4.3、构造器使用(1)
        • 4.3.1、带条件的查询
        • 4.3.2、带条件的修改
        • 4.3.3、带条件的删除
      • 4.4、构造器使用(2)
        • 4.4.1、allEq
        • 4.4.2、eq
        • 4.4.3、ne
        • 4.4.4、gt
        • 4.4.5、ge
        • 4.4.6、lt
        • 4.4.7、le
        • 4.4.8、between
        • 4.4.9、notBetween
        • 4.4.10、like
        • 4.4.11、notLike
        • 4.4.12、likeLeft
        • 4.4.13、likeRight
        • 4.4.14、isNull
        • 4.4.15、isNotNull
        • 4.4.16、in
        • 4.4.17、notIn
        • 4.4.18、inSql
        • 4.4.19、notInSql
        • 4.4.20、groupBy
        • 4.4.21、orderByAsc
        • 4.4.22、orderByDesc
        • 4.4.23、orderBy
        • 4.4.24、having
        • 4.4.25、func
        • 4.4.26、or
        • 4.4.27、and
        • 4.4.28、nested
        • 4.4.29、apply
        • 4.4.30、last
        • 4.4.31、exists
        • 4.4.32、notExists
    • 第五章 MyBatis-Plus3代码生成器
      • 5.1、数据导入
      • 5.2、代码生成器简介
      • 5.3、代码生成器使用
        • 5.3.1、添加依赖
        • 5.3.2、添加配置
        • 5.3.3、启动配置
        • 5.3.4、代码生成
        • 5.3.5、工程结构
        • 5.3.6、添加代码
        • 5.3.7、启动运行
        • 5.3.8、测试方法
        • 5.3.9、温馨提示
      • 5.4、代码生成器方法
        • 5.4.1、save
        • 5.4.2、saveOrUpdate
        • 5.4.3、remove
        • 5.4.4、update
        • 5.4.5、get
        • 5.4.6、list
        • 5.4.7、page
        • 5.4.8、count
        • 5.4.9、chain
    • 第六章 MyBatis-Plus3配置详解
      • 6.1、配置概述
      • 6.2、配置方式
      • 6.3、配置选项
        • 6.3.1、mapperLocations
        • 6.3.2、typeAliasesPackage
        • 6.3.3、typeHandlersPackage
        • 6.3.4、typeEnumsPackage
        • 6.3.5、checkConfigLocation
        • 6.3.6、executorType
        • 6.3.7、configurationProperties
        • 6.3.8、configuration
          • 6.3.8.1、mapUnderscoreToCamelCase
          • 6.3.8.2、defaultEnumTypeHandler
          • 6.3.8.3、aggressiveLazyLoading
          • 6.3.8.4、autoMappingBehavior
          • 6.3.8.5、autoMappingUnknownColumnBehavior
          • 6.3.8.6、localCacheScope
          • 6.3.8.7、cacheEnabled
          • 6.3.8.8、callSettersOnNulls
          • 6.3.8.9、configurationFactory
          • 6.3.8.10、MyBatis3的配置属性
        • 6.3.9、globalConfig
          • 6.3.9.1、banner
          • 6.3.9.2、enableSqlRunner
          • 6.3.9.3、superMapperClass
          • 6.3.9.4、dbConfig
            • 6.3.9.4.1、idType
            • 6.3.9.4.2、tablePrefix
            • 6.3.9.4.3、schema
            • 6.3.9.4.4、columnFormat
            • 6.3.9.4.5、propertyFormat
            • 6.3.9.4.6、tableUnderline
            • 6.3.9.4.7、capitalMode
            • 6.3.9.4.8、logicDeleteField
            • 6.3.9.4.9、logicDeleteValue
            • 6.3.9.4.10、logicNotDeleteValue
            • 6.3.9.4.11、insertStrategy
            • 6.3.9.4.12、updateStrategy
            • 6.3.9.4.13、selectStrategy
      • 6.4、配置小结
    • 第七章 MyBatis-Plus3插件扩展
      • 7.1、分页插件
      • 7.2、执行分析插件
      • 7.3、性能分析插件
      • 7.4、乐观锁插件
      • 7.5、快速开发插件
    • 第八章 MyBatis-Plus3其它功能
      • 8.1、Sql 注入器
      • 8.2、逻辑删除
      • 8.3、通用枚举
        • 8.3.1、保存枚举值
        • 8.3.2、保存枚举名称
      • 8.4、自动填充功能
      • 8.5、字段类型处理器
      • 8.6、自定义ID生成器
      • 8.7、Sequence主键


配套资料,免费下载
链接:https://pan.baidu.com/s/1yQS9hGP3r_zZbkuo-jlijA
提取码:4v88
复制这段内容后打开百度网盘手机App,操作更方便哦

第一章 MyBatis-Plus3概述

1.1、简介

MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

我们的愿景是成为 MyBatis 最好的搭档,就像魂斗罗中的1P、2P,基友搭配,效率翻倍。

1.2、特性

  • 无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑
  • 损耗小:启动即会自动注入基本 CURD,性能基本无损耗,直接面向对象操作
  • 强大的 CRUD 操作:内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求
  • 支持 Lambda 形式调用:通过 Lambda 表达式,方便的编写各类查询条件,无需再担心字段写错
  • 支持主键自动生成:支持多达 4 种主键策略(内含分布式唯一 ID 生成器 - Sequence),可自由配置,完美解决主键问题
  • 支持 ActiveRecord 模式:支持 ActiveRecord 形式调用,实体类只需继承 Model 类即可进行强大的 CRUD 操作
  • 支持自定义全局通用操作:支持全局通用方法注入( Write once, use anywhere )
  • 内置代码生成器:采用代码或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 层代码,支持模板引擎,更有超多自定义配置等您来使用
  • 内置分页插件:基于 MyBatis 物理分页,开发者无需关心具体操作,配置好插件之后,写分页等同于普通 List 查询
  • 分页插件支持多种数据库:支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer 等多种数据库
  • 内置性能分析插件:可输出 Sql 语句以及其执行时间,建议开发测试时启用该功能,能快速揪出慢查询
  • 内置全局拦截插件:提供全表 delete 、 update 操作智能分析阻断,也可自定义拦截规则,预防误操作

1.3、框架结构

framework

1.4、项目地址

官网地址:点击打开

源码地址:点击打开

文档地址:点击打开

配置地址:点击打开

1.5、版本介绍

全新的 MyBatis-Plus 3.0 版本基于 JDK8,提供了 lambda 形式的调用,所以安装集成 MP3.0 要求如下:

  • JDK 8+
  • Maven or Gradle

1.6、快速安装

  • Spring Boot

    • Maven:

      <dependency>
          <groupId>com.baomidou</groupId>
          <artifactId>mybatis-plus-boot-starter</artifactId>
          <version>3.4.0</version>
      </dependency>
      
             
             
      • 1
      • 2
      • 3
      • 4
      • 5
    • Gradle:

      compile group: 'com.baomidou', name: 'mybatis-plus-boot-starter', version: '3.4.0'
      
             
             
      • 1
  • Spring MVC

    • Maven:

      <dependency>
          <groupId>com.baomidou</groupId>
          <artifactId>mybatis-plus</artifactId>
          <version>3.4.0</version>
      </dependency>
      
             
             
      • 1
      • 2
      • 3
      • 4
      • 5
    • Gradle:

      compile group: 'com.baomidou', name: 'mybatis-plus', version: '3.4.0'
      
             
             
      • 1

警告:引入 MyBatis-Plus 之后请不要再次引入 MyBatis 以及 MyBatis-Spring,以避免因版本差异导致的问题。

1.7、开发环境

  • Jdk:jdk1.8.0_261
  • Idea:IntelliJ IDEA 2020.1.2 x64
  • Maven:apache-maven-3.3.9
  • MySQL:mysql-5.5.61-win64

第二章 MyBatis-Plus3增删改查

2.1、项目搭建

image-20201002120850548

image-20201002121010118

image-20201002121134665

image-20201002121200279

新建完成以后,打开pom.xml后添加以下依赖:

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.0</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.49</version>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.12</version>
    <scope>provided</scope>
</dependency>

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

在src/mian/java目录中,创建以下包文件

  • com.caochenlei.mpdemo.pojo
  • com.caochenlei.mpdemo.mapper

2.2、项目配置(1)

MyBatis-Plus 的配置异常的简单,我们仅需要一些简单的配置即可使用 MyBatis-Plus 的强大功能!

  • Spring Boot 工程:

    • 配置 MapperScan 注解

      @SpringBootApplication
      @MapperScan("com.caochenlei.mpdemo.mapper")
      public class MpDemoApplication {
      
      <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
          SpringApplication<span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span>MpDemoApplication<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span> args<span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span>
      

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • Spring MVC 工程:

    • 配置 MapperScan 对象

      <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
          <property name="basePackage" value="com.caochenlei.mpdemo.mapper"/>
      </bean>
      
          
          
      • 1
      • 2
      • 3
    • 调整 SqlSessionFactory 为 MyBatis-Plus 的 SqlSessionFactory

      <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
          <property name="dataSource" ref="dataSource"/>
      </bean>
      
          
          
      • 1
      • 2
      • 3
  • 2.3、项目配置(2)

    application.properties

    #mysql
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.url=jdbc:mysql://localhost:3306/mp?useUnicode=true&characterEncoding=utf8
    spring.datasource.username=root
    spring.datasource.password=123456
    

    #mybatis-plus
    mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    2.4、数据导入

    ## 创建库
    CREATE DATABASE mp;
    ## 使用库
    USE mp;
    ## 创建表
    CREATE TABLE tbl_employee(
       id INT(11) PRIMARY KEY AUTO_INCREMENT,
       last_name VARCHAR(50),
       email VARCHAR(50),
       gender CHAR(1),
       age INT
    );
    ## 导入数据
    INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Tom','tom@qq.com',1,22);
    INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Jerry','jerry@qq.com',0,25);
    INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Black','black@qq.com',1,30);
    INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('White','white@qq.com',0,35);
    INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Tiger','tiger@qq.com',1,28);
    INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Bobby','bobby@qq.com',0,16);
    ## 查询数据
    SELECT * FROM tbl_employee;
    
     
     
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    2.5、创建实体

    com.caochenlei.mpdemo.pojo.Employee

    @TableName("tbl_employee")
    public class Employee {
        @TableId(value = "id", type = IdType.AUTO)
        private Integer id;
        @TableField(value = "last_name")
        private String lastName;
        @TableField(value = "email")
        private String email;
        @TableField(value = "gender")
        private Integer gender;
        @TableField(value = "age")
        private Integer age;
    
    <span class="token keyword">public</span> <span class="token function">Employee</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token punctuation">}</span>
    
    <span class="token keyword">public</span> <span class="token function">Employee</span><span class="token punctuation">(</span>Integer id<span class="token punctuation">,</span> String lastName<span class="token punctuation">,</span> String email<span class="token punctuation">,</span> Integer gender<span class="token punctuation">,</span> Integer age<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>id <span class="token operator">=</span> id<span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>lastName <span class="token operator">=</span> lastName<span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>email <span class="token operator">=</span> email<span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>gender <span class="token operator">=</span> gender<span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>age <span class="token operator">=</span> age<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
    <span class="token keyword">public</span> Integer <span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
        <span class="token keyword">return</span> id<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setId</span><span class="token punctuation">(</span>Integer id<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>id <span class="token operator">=</span> id<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
    <span class="token keyword">public</span> String <span class="token function">getLastName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
        <span class="token keyword">return</span> lastName<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setLastName</span><span class="token punctuation">(</span>String lastName<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>lastName <span class="token operator">=</span> lastName<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
    <span class="token keyword">public</span> String <span class="token function">getEmail</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
        <span class="token keyword">return</span> email<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setEmail</span><span class="token punctuation">(</span>String email<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>email <span class="token operator">=</span> email<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
    <span class="token keyword">public</span> Integer <span class="token function">getGender</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
        <span class="token keyword">return</span> gender<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setGender</span><span class="token punctuation">(</span>Integer gender<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>gender <span class="token operator">=</span> gender<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
    <span class="token keyword">public</span> Integer <span class="token function">getAge</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
        <span class="token keyword">return</span> age<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setAge</span><span class="token punctuation">(</span>Integer age<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>age <span class="token operator">=</span> age<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> String <span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
        <span class="token keyword">return</span> <span class="token string">"Employee{"</span> <span class="token operator">+</span>
                <span class="token string">"id="</span> <span class="token operator">+</span> id <span class="token operator">+</span>
                <span class="token string">", lastName='"</span> <span class="token operator">+</span> lastName <span class="token operator">+</span> <span class="token string">'''</span> <span class="token operator">+</span>
                <span class="token string">", email='"</span> <span class="token operator">+</span> email <span class="token operator">+</span> <span class="token string">'''</span> <span class="token operator">+</span>
                <span class="token string">", gender="</span> <span class="token operator">+</span> gender <span class="token operator">+</span>
                <span class="token string">", age="</span> <span class="token operator">+</span> age <span class="token operator">+</span>
                <span class="token string">'}'</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    

    }

    • 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
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74

    2.6、创建接口

    com.caochenlei.mpdemo.mapper.EmployeeMapper

    @Repository
    public interface EmployeeMapper extends BaseMapper<Employee> {
    

    }

    • 1
    • 2
    • 3
    • 4

    2.7、测试准备

    com.caochenlei.mpdemo.MpDemoApplicationTests

    @SpringBootTest
    class MpDemoApplicationTests {
        @Autowired
        private EmployeeMapper employeeMapper;
    
    <span class="token annotation punctuation">@Test</span>
    <span class="token keyword">void</span> <span class="token function">contextLoads</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
        List<span class="token generics function"><span class="token punctuation">&lt;</span>Employee<span class="token punctuation">&gt;</span></span> employees <span class="token operator">=</span> employeeMapper<span class="token punctuation">.</span><span class="token function">selectList</span><span class="token punctuation">(</span>null<span class="token punctuation">)</span><span class="token punctuation">;</span>
        employees<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span>System<span class="token punctuation">.</span>out<span class="token operator">:</span><span class="token operator">:</span>println<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    

    }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2.8、增删改查

    2.8.1、insert

    需求描述:插入一个员工,员工姓名为“张三”、邮箱为"zhangsan@qq.com"、男性、25岁

    @Test
    void testInsert() {
        int result = employeeMapper.insert(new Employee(null, "zhangsan", "zhangsan@qq.com", 0, 25));
        System.out.println("result:" + result);
    }
    
     
     
    • 1
    • 2
    • 3
    • 4
    • 5
    2.8.2、updateById

    需求信息:将id为1的员工的姓名更改为"Jennie"

    @Test
    void testUpdateById() {
        // 先查询
        Employee employee = employeeMapper.selectById(1);
        employee.setLastName("Jennie");
        // 再修改
        int result = employeeMapper.updateById(employee);
        System.out.println(result);
    }
    
     
     
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    2.8.3、selectById

    需求描述:查询id为1的员工信息

    @Test
    void testSelectById() {
        Employee employee = employeeMapper.selectById(1);
        System.out.println(employee);
    }
    
     
     
    • 1
    • 2
    • 3
    • 4
    • 5
    2.8.4、selectByMap

    需求描述:查询性别为男性(0)且年龄在25岁的员工信息

    @Test
    void testSelectByMap() {
        Map<String, Object> map = new HashMap<>();
        map.put("gender",0);
        map.put("age",25);
        List<Employee> employees = employeeMapper.selectByMap(map);
        employees.forEach(System.out::println);
    }
    
     
     
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    2.8.5、selectBatchIds

    需求描述:查询id分别为1、2、3的员工的信息

    @Test
    void testSelectBatchIds() {
        List<Employee> employees = employeeMapper.selectBatchIds(Arrays.asList(1, 2, 3));
        employees.forEach(System.out::println);
    }
    
     
     
    • 1
    • 2
    • 3
    • 4
    • 5
    2.8.6、deleteById

    需求信息:删除id为1的员工信息

    @Test
    void testDeleteById() {
        int result = employeeMapper.deleteById(1);
        System.out.println(result);
    }
    
     
     
    • 1
    • 2
    • 3
    • 4
    • 5
    2.8.7、deleteByMap

    需求描述:删除性别为男性(0)且年龄在25岁的员工信息

    @Test
    void testDeleteByMap() {
        Map<String, Object> map = new HashMap<>();
        map.put("gender", 0);
        map.put("age", 25);
        int result = employeeMapper.deleteByMap(map);
        System.out.println(result);
    }
    
     
     
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    2.8.8、deleteBatchIds

    需求描述:删除id分别为4、5、6的员工的信息

    @Test
    void testDeleteBatchIds() {
        int result = employeeMapper.deleteBatchIds(Arrays.asList(4, 5, 6));
        System.out.println(result);
    }
    
     
     
    • 1
    • 2
    • 3
    • 4
    • 5

    第三章 MyBatis-Plus3注解介绍

    3.1、@TableName

    描述:表名注解

    属性类型必须指定默认值描述
    valueString“”表名
    schemaString“”schema
    keepGlobalPrefixbooleanfalse是否保持使用全局的 tablePrefix 的值(如果设置了全局 tablePrefix 且自行设置了 value 的值)
    resultMapString“”xml 中 resultMap 的 id
    autoResultMapbooleanfalse是否自动构建 resultMap 并使用(如果设置 resultMap 则不会进行 resultMap 的自动构建并注入)

    3.2、@TableId

    描述:主键注解

    属性类型必须指定默认值描述
    valueString“”主键字段名
    typeEnumIdType.NONE主键类型

    IdType

    描述
    AUTO数据库ID自增
    NONE无状态,该类型为未设置主键类型(注解里等于跟随全局,全局里约等于 INPUT)
    INPUTinsert前自行set主键值
    ASSIGN_ID分配ID(主键类型为Number(Long和Integer)或String)(since 3.3.0),使用接口IdentifierGenerator的方法nextId(默认实现类为DefaultIdentifierGenerator雪花算法)
    ASSIGN_UUID分配UUID,主键类型为String(since 3.3.0),使用接口IdentifierGenerator的方法nextUUID(默认default方法)

    3.3、@TableField

    描述:字段注解(非主键)

    属性类型必须指定默认值描述
    valueString“”数据库字段名
    elString“”映射为原生 #{ ... } 逻辑,相当于写在 xml 里的 #{ ... } 部分
    existbooleantrue是否为数据库表字段
    conditionString“”字段 where 实体查询比较条件,有值设置则按设置的值为准,没有则为默认全局的 %s=#{%s}
    updateString“”字段 update set 部分注入, 例如:update="%s+1":表示更新时会set version=version+1(该属性优先级高于 el 属性)
    insertStrategyEnumNDEFAULT举例:NOT_NULL: insert into table_a(<if test="columnProperty != null">column</if>) values (<if test="columnProperty != null">#{columnProperty}</if>)
    updateStrategyEnumNDEFAULT举例:IGNORED: update table_a set column=#{columnProperty}
    whereStrategyEnumNDEFAULT举例:NOT_EMPTY: where <if test="columnProperty != null and columnProperty!=''">column=#{columnProperty}</if>
    fillEnumFieldFill.DEFAULT字段自动填充策略
    selectbooleantrue是否进行 select 查询
    keepGlobalFormatbooleanfalse是否保持使用全局的 format 进行处理
    jdbcTypeJdbcTypeJdbcType.UNDEFINEDJDBC类型 (该默认值不代表会按照该值生效)
    typeHandlerClass<? extends TypeHandler>UnknownTypeHandler.class类型处理器 (该默认值不代表会按照该值生效)
    numericScaleString“”指定小数点后保留的

    FieldStrategy

    描述
    IGNORED忽略判断
    NOT_NULL非NULL判断
    NOT_EMPTY非空判断(只对字符串类型字段,其他类型字段依然为非NULL判断)
    DEFAULT追随全局配置

    FieldFill

    描述
    DEFAULT默认不处理
    INSERT插入时填充字段
    UPDATE更新时填充字段
    INSERT_UPDATE插入和更新时填充字段

    3.4、@Version

    描述:乐观锁注解、标记 @Verison 在字段上

    3.5、@EnumValue

    描述:通枚举类注解(注解在枚举字段上)

    3.6、@TableLogic

    描述:表字段逻辑处理注解(逻辑删除)

    属性类型必须指定默认值描述
    valueString“”逻辑未删除值
    delvalString“”逻辑删除值

    3.7、@SqlParser

    描述:租户注解,支持method上以及mapper接口上

    属性类型必须指定默认值描述
    filterbooleanfalsetrue: 表示过滤SQL解析,即不会进入ISqlParser解析链,否则会进解析链并追加例如tenant_id等条件

    3.8、@KeySequence

    描述:序列主键策略 oracle

    属性:value、resultMap

    属性类型必须指定默认值描述
    valueString“”序列名
    clazzClassLong.classid的类型, 可以指定String.class,这样返回的Sequence值是字符串"1"

    第四章 MyBatis-Plus3条件构造器

    4.1、数据导入

    ## 使用库
    USE mp;
    ## 清空表
    TRUNCATE TABLE tbl_employee;	
    ## 导入数据
    INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Allan0','123@qq.com',0,21);
    INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Allan1','123@qq.com',0,22);
    INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Allan2','123@qq.com',0,23);
    INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Allan3','123@qq.com',0,24);
    INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Allan4','123@qq.com',0,25);
    INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Allan5','123@qq.com',0,26);
    INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Allan6','123@qq.com',0,27);
    INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Allan7','123@qq.com',0,28);
    INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Allan8','123@qq.com',0,29);
    INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Allan9','123@qq.com',0,30);
    INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Baby0','123@qq.com',1,21);
    INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Baby1','123@qq.com',0,22);
    INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Baby2','123@qq.com',1,23);
    INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Baby3','123@qq.com',0,24);
    INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Baby4','123@qq.com',1,25);
    INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Baby5','123@qq.com',0,26);
    INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Baby6','123@qq.com',1,27);
    INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Baby7','123@qq.com',0,28);
    INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Baby8','123@qq.com',1,29);
    INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Baby9','123@qq.com',0,30);
    INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Tom0','123@qq.com',1,21);
    INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Tom1','123@qq.com',0,22);
    INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Tom2','123@qq.com',1,23);
    INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Tom3','123@qq.com',0,24);
    INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Tom4','123@qq.com',1,25);
    INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Tom5','123@qq.com',0,26);
    INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Tom6','123@qq.com',1,27);
    INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Tom7','123@qq.com',0,28);
    INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Tom8','123@qq.com',1,29);
    INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Tom9','123@qq.com',0,30);
    ## 查询数据
    SELECT * FROM tbl_employee;
    
     
     
    • 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

    4.2、构造器简介

    MyBatis-Plus 通过 EntityWrapper(简称 EW,MP 封装的一个查询条件构造器)或者 Condition(与 EW 类似) 来让用户自由的构建查询条件,简单便捷,没有额外的负担, 能够有效提高开发效率,它主要用于处理 sql 拼接,排序,实体参数查询等。

    注意:使用的是数据库字段,不是 Java 属性!

    警告:MyBatis-Plus不支持以及不赞成在 RPC 调用中把 Wrapper 进行传输,Wrapper 很重,传输 Wrapper 可以类比为你的 controller 用 map 接收值(开发一时爽,维护火葬场),正确的 RPC 调用姿势是写一个 DTO 进行传输,被调用方再根据 DTO 执行相应的操作。

    4.3、构造器使用(1)

    4.3.1、带条件的查询

    需求描述:查询所有姓名的包含B、且姓名为女(1)、且年龄大于24岁的员工信息

    @Test
    void testSelectList1() {
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .like("last_name","B")
                .eq("gender",1)
                .gt("age",24);
        List<Employee> employees = employeeMapper.selectList(queryWrapper);
        employees.forEach(System.out::println);
    }
    
     
     
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    需求描述:查询所有员工信息

    @Test
    void testSelectList2() {
        List<Employee> employees = employeeMapper.selectList(null);
        employees.forEach(System.out::println);
    }
    
     
     
    • 1
    • 2
    • 3
    • 4
    • 5

    需求描述:查询所有女生的数量(1)

    @Test
    void testSelectList3() {
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("gender", 1);
        Integer count = employeeMapper.selectCount(queryWrapper);
        System.out.println(count);
    }
    
     
     
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    4.3.2、带条件的修改

    需求信息:将年龄大于25岁的女生(1)的性别修改为男生(0)

    @Test
    void testUpdate() {
        UpdateWrapper<Employee> updateWrapper = new UpdateWrapper<>();
        updateWrapper
            .eq("gender", 1)
            .gt("age", 25)
            ;
        Employee employee = new Employee();
        employee.setGender(0);
        employeeMapper.update(employee, updateWrapper);
    }
    
     
     
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    4.3.3、带条件的删除

    需求信息:将姓名带有“Tom”的员工信息删除

    @Test
    void testDelete() {
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("last_name", "Tom");
        int result = employeeMapper.delete(queryWrapper);
        System.out.println(result);
    }
    
     
     
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    4.4、构造器使用(2)

    参数说明

    • 以下出现的第一个入参boolean condition表示该条件是否加入最后生成的sql中
    • 以下代码块内的多个方法均为从上往下补全个别boolean类型的入参,默认为true
    • 以下出现的泛型Param均为Wrapper的子类实例(均具有AbstractWrapper的所有方法)
    • 以下方法在入参中出现的R为泛型,在普通wrapper中是String,在LambdaWrapper中是函数(例:Entity::getId,Entity为实体类,getId为字段idgetMethod)
    • 以下方法入参中的R column均表示数据库字段,当R具体类型为String时则为数据库字段名(字段名是数据库关键字的自己用转义符包裹)!而不是实体类数据字段名,另当R具体类型为SFunction时项目runtime不支持eclipse自家的编译器
    • 以下举例均为使用普通wrapper,入参为MapList的均以json形式表现
    • 使用中如果入参的Map或者List,则不会加入最后生成的sql中

    AbstractWrapper

    说明:AbstractWrapper 是 QueryWrapper(LambdaQueryWrapper) 和 UpdateWrapper(LambdaUpdateWrapper) 的父类用于生成 sql 的 where 条件,entity 属性也用于生成 sql 的 where 条件,注意 entity 生成的 where 条件与使用各个 api 生成的 where 条件没有任何关联行为

    4.4.1、allEq
    allEq(Map<R, V> params)
    allEq(Map<R, V> params, boolean null2IsNull)
    allEq(boolean condition, Map<R, V> params, boolean null2IsNull)
    
     
     
    • 1
    • 2
    • 3
    • 全部 eq (或个别 isNull

    个别参数说明:

    paramskey为数据库字段名,value为字段值
    null2IsNull:为true则在mapvaluenull时调用 isNull方法,为false时则忽略valuenull

    • 例1: allEq({id:1,name:"老王",age:null})—>id = 1 and name = '老王' and age is null
    • 例2: allEq({id:1,name:"老王",age:null}, false)—>id = 1 and name = '老王'
    allEq(BiPredicate<R, V> filter, Map<R, V> params)
    allEq(BiPredicate<R, V> filter, Map<R, V> params, boolean null2IsNull)
    allEq(boolean condition, BiPredicate<R, V> filter, Map<R, V> params, boolean null2IsNull) 
    
     
     
    • 1
    • 2
    • 3

    个别参数说明:

    filter : 过滤函数,是否允许字段传入比对条件中
    paramsnull2IsNull : 同上

    • 例1: allEq((k,v) -> k.indexOf("a") >= 0, {id:1,name:"老王",age:null})—>name = '老王' and age is null
    • 例2: allEq((k,v) -> k.indexOf("a") >= 0, {id:1,name:"老王",age:null}, false)—>name = '老王'
    4.4.2、eq
    eq(R column, Object val)
    eq(boolean condition, R column, Object val)
    
     
     
    • 1
    • 2
    • 等于 =
    • 例: eq("name", "老王")—>name = '老王'
    4.4.3、ne
    ne(R column, Object val)
    ne(boolean condition, R column, Object val)
    
     
     
    • 1
    • 2
    • 不等于 <>
    • 例: ne("name", "老王")—>name <> '老王'
    4.4.4、gt
    gt(R column, Object val)
    gt(boolean condition, R column, Object val)
    
     
     
    • 1
    • 2
    • 大于 >
    • 例: gt("age", 18)—>age > 18
    4.4.5、ge
    ge(R column, Object val)
    ge(boolean condition, R column, Object val)
    
     
     
    • 1
    • 2
    • 大于等于 >=
    • 例: ge("age", 18)—>age >= 18
    4.4.6、lt
    lt(R column, Object val)
    lt(boolean condition, R column, Object val)
    
     
     
    • 1
    • 2
    • 小于 <
    • 例: lt("age", 18)—>age < 18
    4.4.7、le
    le(R column, Object val)
    le(boolean condition, R column, Object val)
    
     
     
    • 1
    • 2
    • 小于等于 <=
    • 例: le("age", 18)—>age <= 18
    4.4.8、between
    between(R column, Object val1, Object val2)
    between(boolean condition, R column, Object val1, Object val2)
    
     
     
    • 1
    • 2
    • BETWEEN 值1 AND 值2
    • 例: between("age", 18, 30)—>age between 18 and 30
    4.4.9、notBetween
    notBetween(R column, Object val1, Object val2)
    notBetween(boolean condition, R column, Object val1, Object val2)
    
     
     
    • 1
    • 2
    • NOT BETWEEN 值1 AND 值2
    • 例: notBetween("age", 18, 30)—>age not between 18 and 30
    4.4.10、like
    like(R column, Object val)
    like(boolean condition, R column, Object val)
    
     
     
    • 1
    • 2
    • LIKE ‘%值%’
    • 例: like("name", "王")—>name like '%王%'
    4.4.11、notLike
    notLike(R column, Object val)
    notLike(boolean condition, R column, Object val)
    
     
     
    • 1
    • 2
    • NOT LIKE ‘%值%’
    • 例: notLike("name", "王")—>name not like '%王%'
    4.4.12、likeLeft
    likeLeft(R column, Object val)
    likeLeft(boolean condition, R column, Object val)
    
     
     
    • 1
    • 2
    • LIKE ‘%值’
    • 例: likeLeft("name", "王")—>name like '%王'
    4.4.13、likeRight
    likeRight(R column, Object val)
    likeRight(boolean condition, R column, Object val)
    
     
     
    • 1
    • 2
    • LIKE ‘值%’
    • 例: likeRight("name", "王")—>name like '王%'
    4.4.14、isNull
    isNull(R column)
    isNull(boolean condition, R column)
    
     
     
    • 1
    • 2
    • 字段 IS NULL
    • 例: isNull("name")—>name is null
    4.4.15、isNotNull
    isNotNull(R column)
    isNotNull(boolean condition, R column)
    
     
     
    • 1
    • 2
    • 字段 IS NOT NULL
    • 例: isNotNull("name")—>name is not null
    4.4.16、in
    in(R column, Collection<?> value)
    in(boolean condition, R column, Collection<?> value)
    
     
     
    • 1
    • 2
    • 字段 IN (value.get(0), value.get(1), …)
    • 例: in("age",{1,2,3})—>age in (1,2,3)
    in(R column, Object... values)
    in(boolean condition, R column, Object... values)
    
     
     
    • 1
    • 2
    • 字段 IN (v0, v1, …)
    • 例: in("age", 1, 2, 3)—>age in (1,2,3)
    4.4.17、notIn
    notIn(R column, Collection<?> value)
    notIn(boolean condition, R column, Collection<?> value)
    
     
     
    • 1
    • 2
    • 字段 NOT IN (value.get(0), value.get(1), …)
    • 例: notIn("age",{1,2,3})—>age not in (1,2,3)
    notIn(R column, Object... values)
    notIn(boolean condition, R column, Object... values)
    
     
     
    • 1
    • 2
    • 字段 NOT IN (v0, v1, …)
    • 例: notIn("age", 1, 2, 3)—>age not in (1,2,3)
    4.4.18、inSql
    inSql(R column, String inValue)
    inSql(boolean condition, R column, String inValue)
    
     
     
    • 1
    • 2
    • 字段 IN ( sql语句 )
    • 例: inSql("age", "1,2,3,4,5,6")—>age in (1,2,3,4,5,6)
    • 例: inSql("id", "select id from table where id < 3")—>id in (select id from table where id < 3)
    4.4.19、notInSql
    notInSql(R column, String inValue)
    notInSql(boolean condition, R column, String inValue)
    
     
     
    • 1
    • 2
    • 字段 NOT IN ( sql语句 )
    • 例: notInSql("age", "1,2,3,4,5,6")—>age not in (1,2,3,4,5,6)
    • 例: notInSql("id", "select id from table where id < 3")—>id not in (select id from table where id < 3)
    4.4.20、groupBy
    groupBy(R... columns)
    groupBy(boolean condition, R... columns)
    
     
     
    • 1
    • 2
    • 分组:GROUP BY 字段, …
    • 例: groupBy("id", "name")—>group by id,name
    4.4.21、orderByAsc
    orderByAsc(R... columns)
    orderByAsc(boolean condition, R... columns)
    
     
     
    • 1
    • 2
    • 排序:ORDER BY 字段, … ASC
    • 例: orderByAsc("id", "name")—>order by id ASC,name ASC
    4.4.22、orderByDesc
    orderByDesc(R... columns)
    orderByDesc(boolean condition, R... columns)
    
     
     
    • 1
    • 2
    • 排序:ORDER BY 字段, … DESC
    • 例: orderByDesc("id", "name")—>order by id DESC,name DESC
    4.4.23、orderBy
    orderBy(boolean condition, boolean isAsc, R... columns)
    
     
     
    • 1
    • 排序:ORDER BY 字段, …
    • 例: orderBy(true, true, "id", "name")—>order by id ASC,name ASC
    4.4.24、having
    having(String sqlHaving, Object... params)
    having(boolean condition, String sqlHaving, Object... params)
    
     
     
    • 1
    • 2
    • HAVING ( sql语句 )
    • 例: having("sum(age) > 10")—>having sum(age) > 10
    • 例: having("sum(age) > {0}", 11)—>having sum(age) > 11
    4.4.25、func
    func(Consumer<Children> consumer)
    func(boolean condition, Consumer<Children> consumer)
    
     
     
    • 1
    • 2
    • func 方法(主要方便在出现if…else下调用不同方法能不断链)
    • 例: func(i -> if(true) {i.eq("id", 1)} else {i.ne("id", 1)})
    4.4.26、or
    or()
    or(boolean condition)
    
     
     
    • 1
    • 2
    • 拼接 OR

    注意事项:

    主动调用or表示紧接着下一个方法不是用and连接!(不调用or则默认为使用and连接)

    • 例: eq("id",1).or().eq("name","老王")—>id = 1 or name = '老王'
    or(Consumer<Param> consumer)
    or(boolean condition, Consumer<Param> consumer)
    
     
     
    • 1
    • 2
    • OR 嵌套
    • 例: or(i -> i.eq("name", "李白").ne("status", "活着"))—>or (name = '李白' and status <> '活着')
    4.4.27、and
    and(Consumer<Param> consumer)
    and(boolean condition, Consumer<Param> consumer)
    
     
     
    • 1
    • 2
    • AND 嵌套
    • 例: and(i -> i.eq("name", "李白").ne("status", "活着"))—>and (name = '李白' and status <> '活着')
    4.4.28、nested
    nested(Consumer<Param> consumer)
    nested(boolean condition, Consumer<Param> consumer)
    
     
     
    • 1
    • 2
    • 正常嵌套 不带 AND 或者 OR
    • 例: nested(i -> i.eq("name", "李白").ne("status", "活着"))—>(name = '李白' and status <> '活着')
    4.4.29、apply
    apply(String applySql, Object... params)
    apply(boolean condition, String applySql, Object... params)
    
     
     
    • 1
    • 2
    • 拼接 sql

    注意事项:

    该方法可用于数据库函数动态入参的params对应前面applySql内部的{index}部分,这样是不会有sql注入风险的,反之会有!

    • 例: apply("id = 1")—>id = 1
    • 例: apply("date_format(dateColumn,'%Y-%m-%d') = '2008-08-08'")—>date_format(dateColumn,'%Y-%m-%d') = '2008-08-08'")
    • 例: apply("date_format(dateColumn,'%Y-%m-%d') = {0}", "2008-08-08")—>date_format(dateColumn,'%Y-%m-%d') = '2008-08-08'")
    4.4.30、last
    last(String lastSql)
    last(boolean condition, String lastSql)
    
     
     
    • 1
    • 2
    • 无视优化规则直接拼接到 sql 的最后

    注意事项:

    只能调用一次,多次调用以最后一次为准,有sql注入的风险,请谨慎使用

    • 例: last("limit 1")
    4.4.31、exists
    exists(String existsSql)
    exists(boolean condition, String existsSql)
    
     
     
    • 1
    • 2
    • 拼接 EXISTS ( sql语句 )
    • 例: exists("select id from table where age = 1")—>exists (select id from table where age = 1)
    4.4.32、notExists
    notExists(String notExistsSql)
    notExists(boolean condition, String notExistsSql)
    
     
     
    • 1
    • 2
    • 拼接 NOT EXISTS ( sql语句 )
    • 例: notExists("select id from table where age = 1")—>not exists (select id from table where age = 1)

    第五章 MyBatis-Plus3代码生成器

    5.1、数据导入

    ## 删除表
    DROP TABLE IF EXISTS `tbl_user`;
    ## 创建表
    CREATE TABLE `tbl_user` (
      `id` BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
      `name` VARCHAR(30) DEFAULT NULL COMMENT '姓名',
      `age` INT(11) DEFAULT NULL COMMENT '年龄',
      `email` VARCHAR(30) DEFAULT NULL COMMENT '邮箱',
      PRIMARY KEY (`id`)
    ) ENGINE=INNODB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COMMENT='用户';
    ## 导入数据
    INSERT  INTO `tbl_user`(`id`,`name`,`age`,`email`) VALUES (1,'Jone',18,'test1@baomidou.com');
    INSERT  INTO `tbl_user`(`id`,`name`,`age`,`email`) VALUES (2,'Jack',20,'test2@baomidou.com');
    INSERT  INTO `tbl_user`(`id`,`name`,`age`,`email`) VALUES (3,'Tom',28,'test3@baomidou.com');
    INSERT  INTO `tbl_user`(`id`,`name`,`age`,`email`) VALUES (4,'Sandy',21,'test4@baomidou.com');
    INSERT  INTO `tbl_user`(`id`,`name`,`age`,`email`) VALUES (5,'Billie',24,'test5@baomidou.com');
    
     
     
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    5.2、代码生成器简介

    AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码,极大的提升了开发效率。

    5.3、代码生成器使用

    5.3.1、添加依赖

    pom.xml

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>com.baomidou<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>mybatis-plus-boot-starter<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>3.4.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>mysql<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>mysql-connector-java<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>5.1.49<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>com.baomidou<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>mybatis-plus-generator<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>3.4.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.apache.velocity<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>velocity-engine-core<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>2.2<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.projectlombok<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>lombok<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>1.18.12<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>scope</span><span class="token punctuation">&gt;</span></span>provided<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>scope</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
    

    </dependencies>

    • 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
    <build>
        <!-- 插件管理 -->
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.5</version>
            </plugin>
        </plugins>
        <!-- 资源管理 -->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                    <include>**/*.conf</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                    <include>**/*.conf</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>
    
     
     
    • 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
    5.3.2、添加配置

    application.properties

    #server
    server.port=8080
    

    #mysql
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.url=jdbc:mysql://localhost:3306/mp?useUnicode=true&characterEncoding=utf8
    spring.datasource.username=root
    spring.datasource.password=123456

    #mybatis-plus
    mybatis-plus.mapper-locations=classpath*????*/mapper/xml/*.xml
    mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    5.3.3、启动配置

    MpDemoApplication.java

    @SpringBootApplication
    @MapperScan("com.caochenlei.mpdemo.mapper")
    public class MpDemoApplication {
    
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
        SpringApplication<span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span>MpDemoApplication<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span> args<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    

    }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    5.3.4、代码生成

    CodeGenerator.java

    public class CodeGenerator {
    
    <span class="token keyword">public</span> <span class="token keyword">static</span> String <span class="token function">scanner</span><span class="token punctuation">(</span>String tip<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
        Scanner scanner <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Scanner</span><span class="token punctuation">(</span>System<span class="token punctuation">.</span>in<span class="token punctuation">)</span><span class="token punctuation">;</span>
        StringBuilder help <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">StringBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        help<span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span><span class="token string">"请输入"</span> <span class="token operator">+</span> tip <span class="token operator">+</span> <span class="token string">":"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>help<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>scanner<span class="token punctuation">.</span><span class="token function">hasNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
            String ipt <span class="token operator">=</span> scanner<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>StringUtils<span class="token punctuation">.</span><span class="token function">isNotBlank</span><span class="token punctuation">(</span>ipt<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
                <span class="token keyword">return</span> ipt<span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">MybatisPlusException</span><span class="token punctuation">(</span><span class="token string">"请输入正确的"</span> <span class="token operator">+</span> tip <span class="token operator">+</span> <span class="token string">"!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
        <span class="token comment">// 代码生成器</span>
        AutoGenerator mpg <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">AutoGenerator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    
        <span class="token comment">// 全局配置</span>
        GlobalConfig gc <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">GlobalConfig</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        String projectPath <span class="token operator">=</span> System<span class="token punctuation">.</span><span class="token function">getProperty</span><span class="token punctuation">(</span><span class="token string">"user.dir"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        gc<span class="token punctuation">.</span><span class="token function">setOutputDir</span><span class="token punctuation">(</span>projectPath <span class="token operator">+</span> <span class="token string">"/src/main/java"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//设置代码生成路径</span>
        gc<span class="token punctuation">.</span><span class="token function">setFileOverride</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//是否覆盖以前文件</span>
        gc<span class="token punctuation">.</span><span class="token function">setOpen</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//是否打开生成目录</span>
        gc<span class="token punctuation">.</span><span class="token function">setAuthor</span><span class="token punctuation">(</span><span class="token string">"caochenlei"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//设置项目作者名称</span>
        gc<span class="token punctuation">.</span><span class="token function">setIdType</span><span class="token punctuation">(</span>IdType<span class="token punctuation">.</span>AUTO<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//设置主键策略</span>
        gc<span class="token punctuation">.</span><span class="token function">setBaseResultMap</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//生成基本ResultMap</span>
        gc<span class="token punctuation">.</span><span class="token function">setBaseColumnList</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//生成基本ColumnList</span>
        gc<span class="token punctuation">.</span><span class="token function">setServiceName</span><span class="token punctuation">(</span><span class="token string">"%sService"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//去掉服务默认前缀</span>
        mpg<span class="token punctuation">.</span><span class="token function">setGlobalConfig</span><span class="token punctuation">(</span>gc<span class="token punctuation">)</span><span class="token punctuation">;</span>
    
        <span class="token comment">// 数据源配置</span>
        DataSourceConfig dsc <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">DataSourceConfig</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        dsc<span class="token punctuation">.</span><span class="token function">setUrl</span><span class="token punctuation">(</span><span class="token string">"jdbc:mysql://localhost:3306/mp?useSSL=false&amp;useUnicode=true&amp;characterEncoding=utf8"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        dsc<span class="token punctuation">.</span><span class="token function">setDriverName</span><span class="token punctuation">(</span><span class="token string">"com.mysql.jdbc.Driver"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        dsc<span class="token punctuation">.</span><span class="token function">setUsername</span><span class="token punctuation">(</span><span class="token string">"root"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        dsc<span class="token punctuation">.</span><span class="token function">setPassword</span><span class="token punctuation">(</span><span class="token string">"123456"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        mpg<span class="token punctuation">.</span><span class="token function">setDataSource</span><span class="token punctuation">(</span>dsc<span class="token punctuation">)</span><span class="token punctuation">;</span>
    
        <span class="token comment">// 包配置</span>
        PackageConfig pc <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">PackageConfig</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        pc<span class="token punctuation">.</span><span class="token function">setParent</span><span class="token punctuation">(</span><span class="token string">"com.caochenlei.mpdemo"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        pc<span class="token punctuation">.</span><span class="token function">setMapper</span><span class="token punctuation">(</span><span class="token string">"mapper"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        pc<span class="token punctuation">.</span><span class="token function">setXml</span><span class="token punctuation">(</span><span class="token string">"mapper.xml"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        pc<span class="token punctuation">.</span><span class="token function">setEntity</span><span class="token punctuation">(</span><span class="token string">"pojo"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        pc<span class="token punctuation">.</span><span class="token function">setService</span><span class="token punctuation">(</span><span class="token string">"service"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        pc<span class="token punctuation">.</span><span class="token function">setServiceImpl</span><span class="token punctuation">(</span><span class="token string">"service.impl"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        pc<span class="token punctuation">.</span><span class="token function">setController</span><span class="token punctuation">(</span><span class="token string">"controller"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        mpg<span class="token punctuation">.</span><span class="token function">setPackageInfo</span><span class="token punctuation">(</span>pc<span class="token punctuation">)</span><span class="token punctuation">;</span>
    
        <span class="token comment">// 策略配置</span>
        StrategyConfig sc <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">StrategyConfig</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        sc<span class="token punctuation">.</span><span class="token function">setNaming</span><span class="token punctuation">(</span>NamingStrategy<span class="token punctuation">.</span>underline_to_camel<span class="token punctuation">)</span><span class="token punctuation">;</span>
        sc<span class="token punctuation">.</span><span class="token function">setColumnNaming</span><span class="token punctuation">(</span>NamingStrategy<span class="token punctuation">.</span>underline_to_camel<span class="token punctuation">)</span><span class="token punctuation">;</span>
        sc<span class="token punctuation">.</span><span class="token function">setEntityLombokModel</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        sc<span class="token punctuation">.</span><span class="token function">setRestControllerStyle</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        sc<span class="token punctuation">.</span><span class="token function">setControllerMappingHyphenStyle</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        sc<span class="token punctuation">.</span><span class="token function">setTablePrefix</span><span class="token punctuation">(</span><span class="token string">"tbl_"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        sc<span class="token punctuation">.</span><span class="token function">setInclude</span><span class="token punctuation">(</span><span class="token function">scanner</span><span class="token punctuation">(</span><span class="token string">"表名,多个英文逗号分割"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">","</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        mpg<span class="token punctuation">.</span><span class="token function">setStrategy</span><span class="token punctuation">(</span>sc<span class="token punctuation">)</span><span class="token punctuation">;</span>
    
        <span class="token comment">// 生成代码</span>
        mpg<span class="token punctuation">.</span><span class="token function">execute</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    

    }

    • 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
    • 65
    • 66
    • 67
    • 68
    5.3.5、工程结构

    image-20201003150519754

    5.3.6、添加代码

    UserController.java

    @RestController
    @RequestMapping("/user")
    public class UserController {
        @Autowired
        private UserService userService;
    
    <span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/all"</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> List<span class="token generics function"><span class="token punctuation">&lt;</span>User<span class="token punctuation">&gt;</span></span> <span class="token function">getAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
        <span class="token keyword">return</span> userService<span class="token punctuation">.</span><span class="token function">list</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    

    }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    5.3.7、启动运行

    MpDemoApplication.java 中运行主方法以此来启动整个工程

    @SpringBootApplication
    @MapperScan("com.caochenlei.mpdemo.mapper")
    public class MpDemoApplication {
    
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
        SpringApplication<span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span>MpDemoApplication<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span> args<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    

    }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    5.3.8、测试方法

    使用浏览器打开:http://localhost:8080/user/all

    image-20201003150941091

    5.3.9、温馨提示

    需要Lombok插件支持,只需要安装一下就可以了,打开 IDEA,进入 File -> Settings -> Plugins,安装完成后重启

    image-20201004212357130

    5.4、代码生成器方法

    通用 Service CRUD 封装 IService 接口,进一步封装 CRUD 采用 get 查询单行 remove 删除 list 查询集合 page 分页 前缀命名方式区分 Mapper 层避免混淆,泛型 T 为任意实体对象,建议如果存在自定义通用 Service 方法的可能,请创建自己的 IBaseService 继承 Mybatis-Plus 提供的基类,对象 Wrapper 为 条件构造器。

    5.4.1、save
    // 插入一条记录(选择字段,策略插入)
    boolean save(T entity);
    // 插入(批量)
    boolean saveBatch(Collection<T> entityList);
    // 插入(批量)
    boolean saveBatch(Collection<T> entityList, int batchSize);
    
     
     
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    参数说明:

    类型参数名描述
    Tentity实体对象
    Collection<T>entityList实体对象集合
    intbatchSize插入批次数量
    5.4.2、saveOrUpdate
    // TableId 注解存在更新记录,否插入一条记录
    boolean saveOrUpdate(T entity);
    // 根据updateWrapper尝试更新,否继续执行saveOrUpdate(T)方法
    boolean saveOrUpdate(T entity, Wrapper<T> updateWrapper);
    // 批量修改插入
    boolean saveOrUpdateBatch(Collection<T> entityList);
    // 批量修改插入
    boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize);
    
     
     
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    参数说明:

    类型参数名描述
    Tentity实体对象
    Wrapper<T>updateWrapper实体对象封装操作类 UpdateWrapper
    Collection<T>entityList实体对象集合
    intbatchSize插入批次数量
    5.4.3、remove
    // 根据 entity 条件,删除记录
    boolean remove(Wrapper<T> queryWrapper);
    // 根据 ID 删除
    boolean removeById(Serializable id);
    // 根据 columnMap 条件,删除记录
    boolean removeByMap(Map<String, Object> columnMap);
    // 删除(根据ID 批量删除)
    boolean removeByIds(Collection<? extends Serializable> idList);
    
     
     
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    参数说明:

    类型参数名描述
    Wrapper<T>queryWrapper实体包装类 QueryWrapper
    Serializableid主键ID
    Map<String, Object>columnMap表字段 map 对象
    Collection<? extends Serializable>idList主键ID列表
    5.4.4、update
    // 根据 UpdateWrapper 条件,更新记录 需要设置sqlset
    boolean update(Wrapper<T> updateWrapper);
    // 根据 whereEntity 条件,更新记录
    boolean update(T entity, Wrapper<T> updateWrapper);
    // 根据 ID 选择修改
    boolean updateById(T entity);
    // 根据ID 批量更新
    boolean updateBatchById(Collection<T> entityList);
    // 根据ID 批量更新
    boolean updateBatchById(Collection<T> entityList, int batchSize);
    
     
     
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    参数说明:

    类型参数名描述
    Wrapper<T>updateWrapper实体对象封装操作类 UpdateWrapper
    Tentity实体对象
    Collection<T>entityList实体对象集合
    intbatchSize更新批次数量
    5.4.5、get
    // 根据 ID 查询
    T getById(Serializable id);
    // 根据 Wrapper,查询一条记录。结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last("LIMIT 1")
    T getOne(Wrapper<T> queryWrapper);
    // 根据 Wrapper,查询一条记录
    T getOne(Wrapper<T> queryWrapper, boolean throwEx);
    // 根据 Wrapper,查询一条记录
    Map<String, Object> getMap(Wrapper<T> queryWrapper);
    // 根据 Wrapper,查询一条记录
    <V> V getObj(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);
    
     
     
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    参数说明:

    类型参数名描述
    Serializableid主键ID
    Wrapper<T>queryWrapper实体对象封装操作类 QueryWrapper
    booleanthrowEx有多个 result 是否抛出异常
    Tentity实体对象
    Function<? super Object, V>mapper转换函数
    5.4.6、list
    // 查询所有
    List<T> list();
    // 查询列表
    List<T> list(Wrapper<T> queryWrapper);
    // 查询(根据ID 批量查询)
    Collection<T> listByIds(Collection<? extends Serializable> idList);
    // 查询(根据 columnMap 条件)
    Collection<T> listByMap(Map<String, Object> columnMap);
    // 查询所有列表
    List<Map<String, Object>> listMaps();
    // 查询列表
    List<Map<String, Object>> listMaps(Wrapper<T> queryWrapper);
    // 查询全部记录
    List<Object> listObjs();
    // 查询全部记录
    <V> List<V> listObjs(Function<? super Object, V> mapper);
    // 根据 Wrapper 条件,查询全部记录
    List<Object> listObjs

最后

以上就是专注大象为你收集整理的mybatis的全部内容,希望文章能够帮你解决mybatis所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(38)

评论列表共有 0 条评论

立即
投稿
返回
顶部