If you integrate mybatis-plus according to the framework

1. Why use mybatis-plus?

  • No intrusion: only enhancement and no change, the introduction of it will not affect the existing project, as smooth as silk
  • Low loss: the basic CURD will be automatically injected when it is started, the performance is basically lossless, and the object-oriented operation is directly performed
  • Powerful CRUD operations: Built-in general Mapper and general Service, most of the CRUD operations on a single table can be realized with only a small amount of configuration, and there is a powerful condition constructor to meet various usage needs
  • Support Lambda form call: through Lambda expressions, it is convenient to write various query conditions, no need to worry about field typos
  • Supports automatic primary key generation: supports up to 4 primary key strategies (including a distributed unique ID generator - Sequence), which can be freely configured to perfectly solve the primary key problem
  • Support ActiveRecord mode: support ActiveRecord form call, the entity class only needs to inherit the Model class to perform powerful CRUD operations
  • Support custom global general operations: support global general method injection ( Write once, use anywhere )
  • Built-in code generator: use code or Maven plug-ins to quickly generate Mapper, Model, Service, and Controller layer codes, support template engines, and have a lot of custom configurations waiting for you to use
  • Built-in paging plug-in: Based on MyBatis physical paging, developers don't need to care about specific operations. After configuring the plug-in, writing paging is equivalent to ordinary List query
  • The paging plug-in supports multiple databases: supports MySQL, MariaDB, Oracle, DB2, H2, HSQL, SQLite, Postgre, SQLServer and other databases
  • Built-in performance analysis plug-in: It can output SQL statements and their execution time. It is recommended to enable this function during development and testing, which can quickly find out slow queries
  • Built-in global interception plug-in: Provides intelligent analysis and blocking of delete and update operations on the entire table, and can also customize interception rules to prevent misoperations

2. Integration steps

1. Import the pom file

<!--   mybatis-plus     -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.5.3.1</version>
</dependency>

I am a dependency of mybatis-plus introduced under the ruoyi-common module

2. Increase the configuration class of mybatis-plus

The location of the new package is as follows

insert image description here
Under the mybatisplus package, create the class
CreateAndUpdateMetaObjectHandler

code show as below

package com.spider.framework.mybatisplus;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.spider.common.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;

import java.util.Date;


public class CreateAndUpdateMetaObjectHandler implements MetaObjectHandler {
    
    
    @Override
    public void insertFill(MetaObject metaObject) {
    
    
        //region 处理创建人信息
        Object createBy = this.getFieldValByName("createBy", metaObject);
        Object createTime = this.getFieldValByName("createTime", metaObject);
        if (createBy == null) {
    
    
            createBy = SecurityUtils.getUsername();
            this.setFieldValByName("createBy", createBy, metaObject);
        }
        if (createTime == null) {
    
    
            createTime = new Date();
            this.setFieldValByName("createTime", createTime, metaObject);
        }
        //endregion
        //region 处理修改人信息
        Object updateBy = this.getFieldValByName("updateBy", metaObject);
        Object updateTime = this.getFieldValByName("updateTime", metaObject);
        if (updateBy == null) {
    
    
            updateBy = createBy;
            this.setFieldValByName("updateBy", updateBy, metaObject);
        }
        if (updateTime == null) {
    
    
            updateTime = createTime;
            this.setFieldValByName("updateTime", updateTime, metaObject);
        }
        //endregion
    }

    @Override
    public void updateFill(MetaObject metaObject) {
    
    
        //region 处理修改人信息
        Object updateBy = this.getFieldValByName("updateBy", metaObject);
        Object updateTime = this.getFieldValByName("updateTime", metaObject);
        if (updateBy == null) {
    
    
            updateBy = SecurityUtils.getUsername();
            this.setFieldValByName("updateBy", updateBy, metaObject);
        }
        if (updateTime == null) {
    
    
            updateTime = new Date();
            this.setFieldValByName("updateTime", updateTime, metaObject);
        }
        //endregion
    }

    @Override
    public boolean openInsertFill() {
    
    
        return true;
    }

    @Override
    public boolean openUpdateFill() {
    
    
        return true;
    }
}

3. Modify the yml configuration file

insert image description here

Comment out the original mybatis configuration and add the following code

# MyBatis配置
#mybatis:
#    # 搜索指定包别名
#    typeAliasesPackage: com.spider.**.domain
#    # 配置mapper的扫描,找到所有的mapper.xml映射文件
#    mapperLocations: classpath*:mapper/**/*Mapper.xml
#    # 加载全局的配置文件
#    configLocation: classpath:mybatis/mybatis-config.xml

mybatis-plus:
  # 对应的 XML 文件位置
  mapperLocations: classpath*:mapper/**/*Mapper.xml
  # 实体扫描,多个package用逗号或者分号分隔
  typeAliasesPackage: com.spider.**.domain
  # 针对 typeAliasesPackage,如果配置了该属性,则仅仅会扫描路径下以该类作为父类的域对象
  #typeAliasesSuperType: Class<?>
  # 如果配置了该属性,SqlSessionFactoryBean 会把该包下面的类注册为对应的 TypeHandler
  #typeHandlersPackage: null
  # 如果配置了该属性,会将路径下的枚举类进行注入,让实体类字段能够简单快捷的使用枚举属性
  #typeEnumsPackage: null
  # 启动时是否检查 MyBatis XML 文件的存在,默认不检查
  checkConfigLocation: false
  # 通过该属性可指定 MyBatis 的执行器,MyBatis 的执行器总共有三种:
  # SIMPLE:该执行器类型不做特殊的事情,为每个语句的执行创建一个新的预处理语句(PreparedStatement)
  # REUSE:该执行器类型会复用预处理语句(PreparedStatement)
  # BATCH:该执行器类型会批量执行所有的更新语句
  executorType: SIMPLE
  # 指定外部化 MyBatis Properties 配置,通过该配置可以抽离配置,实现不同环境的配置部署
  configurationProperties: null
  configuration:
    # 自动驼峰命名规则(camel case)映射
    # 如果您的数据库命名符合规则无需使用 @TableField 注解指定数据库字段名
    mapUnderscoreToCamelCase: true
    # 默认枚举处理类,如果配置了该属性,枚举将统一使用指定处理器进行处理
    # org.apache.ibatis.type.EnumTypeHandler : 存储枚举的名称
    # org.apache.ibatis.type.EnumOrdinalTypeHandler : 存储枚举的索引
    # com.baomidou.mybatisplus.extension.handlers.MybatisEnumTypeHandler : 枚举类需要实现IEnum接口或字段标记@EnumValue注解.
    defaultEnumTypeHandler: org.apache.ibatis.type.EnumTypeHandler
    # 当设置为 true 的时候,懒加载的对象可能被任何懒属性全部加载,否则,每个属性都按需加载。需要和 lazyLoadingEnabled 一起使用。
    aggressiveLazyLoading: true
    # MyBatis 自动映射策略
    # NONE:不启用自动映射
    # PARTIAL:只对非嵌套的 resultMap 进行自动映射
    # FULL:对所有的 resultMap 都进行自动映射
    autoMappingBehavior: PARTIAL
    # MyBatis 自动映射时未知列或未知属性处理策
    # NONE:不做任何处理 (默认值)
    # WARNING:以日志的形式打印相关警告信息
    # FAILING:当作映射失败处理,并抛出异常和详细信息
    autoMappingUnknownColumnBehavior: NONE
    # Mybatis一级缓存,默认为 SESSION
    # SESSION session级别缓存,同一个session相同查询语句不会再次查询数据库
    # STATEMENT 关闭一级缓存
    localCacheScope: SESSION
    # 开启Mybatis二级缓存,默认为 true
    cacheEnabled: true
  global-config:
    # 是否打印 Logo banner
    banner: true
    # 是否初始化 SqlRunner
    enableSqlRunner: false
    dbConfig:
      # 主键类型
      # AUTO 数据库ID自增
      # NONE 空
      # INPUT 用户输入ID
      # ASSIGN_ID 全局唯一ID
      # ASSIGN_UUID 全局唯一ID UUID
      idType: AUTO
      # 表名前缀
      tablePrefix: null
      # 字段 format,例: %s,(对主键无效)
      columnFormat: null
      # 表名是否使用驼峰转下划线命名,只对表名生效
      tableUnderline: true
      # 大写命名,对表名和字段名均生效
      capitalMode: false
      # 全局的entity的逻辑删除字段属性名
      logicDeleteField: null
      # 逻辑已删除值
      logicDeleteValue: 2
      # 逻辑未删除值
      logicNotDeleteValue: 0
      # 字段验证策略之 insert,在 insert 的时候的字段验证策略
      # IGNORED 忽略判断
      # NOT_NULL 非NULL判断
      # NOT_EMPTY 非空判断(只对字符串类型字段,其他类型字段依然为非NULL判断)
      # DEFAULT 默认的,一般只用于注解里
      # NEVER 不加入 SQL
      insertStrategy: NOT_NULL
      # 字段验证策略之 update,在 update 的时候的字段验证策略
      updateStrategy: NOT_NULL
      # 字段验证策略之 select,在 select 的时候的字段验证策略既 wrapper 根据内部 entity 生成的 where 条件
      selectStrategy: NOT_NULL

Guess you like

Origin blog.csdn.net/qq_27480007/article/details/130522139