MybatisPlus学习整理(二)

看这篇内容以前建议先看一下 MyBatisPlus学习整理(一)
很少bibi,直接建表
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`  (
  `id` bigint(20) NULL DEFAULT NULL COMMENT '主键',
  `name` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '姓名',
  `age` int(11) NULL DEFAULT NULL COMMENT '年龄',
  `email` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '邮箱',
  `manager_id` bigint(20) NULL DEFAULT NULL COMMENT '直属上级id',
  `create_time` datetime(0) NULL DEFAULT NULL COMMENT '建立时间',
  `update_time` datetime(0) NULL DEFAULT NULL COMMENT '修改时间',
  `version` int(11) NULL DEFAULT 1 COMMENT '版本',
  `deleted` int(1) NULL DEFAULT 0 COMMENT '逻辑删除标识(0,未删除;1,已删除)'
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
INSERT INTO `user` VALUES (1234, '大boss', 40, 'boss@163.com', NULL, '2019-10-02 10:08:02', '2019-10-02 10:08:05', 1, 0);
INSERT INTO `user` VALUES (2345, '王天风', 25, 'wtf@163.com', 1234, '2019-10-02 10:09:07', '2019-10-02 10:09:10', 1, 0);
INSERT INTO `user` VALUES (2346, '李艺伟', 28, 'lyw@163.com', 2345, '2019-10-02 10:10:09', '2019-10-02 10:10:12', 1, 0);
INSERT INTO `user` VALUES (3456, '张雨绮', 31, 'zyq@163.com', 2345, '2019-10-02 10:10:54', '2019-10-02 10:10:58', 1, 0);
INSERT INTO `user` VALUES (4566, '刘雨红', 32, 'lyh@163.com', 2345, '2019-10-02 10:11:51', '2019-10-02 10:11:55', 1, 0);
SET FOREIGN_KEY_CHECKS = 1;

项目延用MyBatisPlus学习整理(一)https://github.com/xiao-ren-wu/notebook/tree/master/mybatis-plus-demohtml

逻辑删除

  1. 设定逻辑删除规则

在配置文件中配置逻辑删除和逻辑未删除的值mysql

mybatis-plus:
  global-config:
      logic-not-delete-value: 0
      logic-delete-value: 1
  1. 在pojo类中在逻辑删除的字段加注解 @TableLogic
@Data
@EqualsAndHashCode(callSuper = false)
public class User extends Model<User> {
    @TableId(type = IdType.AUTO)
    private Long id;
    @TableField(condition = SqlCondition.LIKE)
    private String name;
    private Integer age;
    private String email;
    private Long managerId;
    private LocalDateTime createTime;
    private LocalDateTime updateTime;
    private Integer version;
    @TableLogic
    private Integer deleted;
}
  1. 经过id逻辑删除
@Test
    public void deleteById(){
        userMapper.deleteById(4566L);
    }

  1. 查询中排除删除标识字段及注意事项

逻辑删除字段只是为了标识数据是否被逻辑删除,在查询的时候,并不想也将该字段查询出来。
咱们只须要在delete字段上增长@TableField(select = false)mybatisplus在查询的时候就会自动忽略该字段。git

@Test
    public void selectIgnoreDeleteTest(){
        userMapper.selectById(3456L);
    }


自定义sql,MybatisPlus不会忽略deleted属性,须要咱们手动忽略github

自动填充

MybaitsPlus在咱们插入数据或者更新数据的时候,为咱们提供了自动填充功能。相似MySQL提供的默认值同样。
若是咱们须要使用自动填充功能,咱们须要在实体类的相应属性上加@TableField注解,并指定何时进行自动填充。mybatisPlus为咱们提供了三种填充时机,在FieldFill枚举中spring

public enum FieldFill {
    /**
     * 默认不处理
     */
    DEFAULT,
    /**
     * 插入时填充字段
     */
    INSERT,
    /**
     * 更新时填充字段
     */
    UPDATE,
    /**
     * 插入和更新时填充字段
     */
    INSERT_UPDATE
}

设置好以后,咱们还须要编写具体的填充规则,具体是编写一个填充类并交给Spring管理,而后实现MetaObjectHandler接口中的insertFillupdateFill方法。
eg:sql

  1. 插入User对象的时候自动填充插入时间,更新User对象的时候自动填充更新时间。
  • 指定实体类中须要自动填充的字段,并设置填充时机
@Data
@EqualsAndHashCode(callSuper = false)
public class User extends Model<User> {
    ...
    @TableField(fill = INSERT)
    private LocalDateTime createTime;
    @TableField(fill = UPDATE)
    private LocalDateTime updateTime;
    ...
}
  • 编写填充规则
@Component
public class MyMetaObjHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        if(metaObject.hasSetter("createTime")){
            setInsertFieldValByName("createTime", LocalDateTime.now(),metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        if(metaObject.hasSetter("updateTime")){
            setUpdateFieldValByName("updateTime",LocalDateTime.now(),metaObject);
        }
    }
}
解释一下为何要用if判断是否有对应的属性
mybatisPlus在执行插入或者更新操做的时候,每次都会执行该方法,有些表中是没有设置自动填充字段的,并且有些自动填充字段的值的获取比较消耗系统性能,因此为了避免必要的消耗,进行if判断,决定是否须要填充。

有些时候咱们已经设置了属性的值。不想让mybatisPlus再自动填充,也就是说咱们没有设置属性的值,mybatisPlus进行填充,若是设置了那么就用咱们设置的值。这种状况咱们只须要在填充类中提早获取默认值,而后使用该默认值就能够了。数据库

@Override
    public void updateFill(MetaObject metaObject) {
        if(metaObject.hasSetter("updateTime")){
            Object updateTime = getFieldValByName("updateTime", metaObject);
            if(Objects.nonNull(updateTime)){
                setUpdateFieldValByName("updateTime",updateTime,metaObject);
            }else{
                setUpdateFieldValByName("updateTime",LocalDateTime.now(),metaObject);
            }
        }
    }

乐观锁

乐观锁适用于读多写少的状况,更新数据的时候不使用“锁“而是使用版本号来判断是否能够更新数据。经过不加锁来减少数据更新时间和系统的性能消耗,进而提升数据库的吞吐量。CAS机制就是一种典型的乐观锁的形式。
乐观锁是逻辑存在的一种概念,咱们若是使用乐观锁须要手动在表的加上version字段。mybatis

  1. mysql使用乐观锁伪代码示例:
update user 
set balabala....
where balabala... and version = xxx
乐观锁

1.配置类中注入乐观锁插件app

@Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor(){
        return new OptimisticLockerInterceptor();
    }
  1. 实体类中的版本字段增长@version注解
@Data
@EqualsAndHashCode(callSuper = false)
public class User extends Model<User> {
    ...
    @Version
    private Integer version;
    ...
}
  1. test

更新王天风的年龄ide

@Test
    public void testLock(){
        int version = 1;
        User user = new User();
        user.setEmail("wtf@163.com");
        user.setAge(34);
        user.setId(2345L);
        user.setManagerId(1234L);
        user.setVersion(1);
        userMapper.updateById(user);

    }

image.png
数据库中的version已经变成2
image.png

注意事项:
  1. 支持的类型只有:int,Integer,long,Long,Date,Timestamp,LocalDateTime
  2. 整数类型下newVerison = oldVersion+1
  3. newVersion会写到entity中
  4. 仅支持updateById(id)与update(entity,wrapper)方法
  5. 在update(entiry,wrapper)方法下,wrapper不能复用

性能分析

  1. 配置类中注入性能分析插件
@Bean
   // @Profile({"dev,test"})
    public PerformanceInterceptor performanceInterceptor() {
        PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
        // 格式化sql输出
        performanceInterceptor.setFormat(true);
        // 设置sql执行最大时间,单位(ms)
        performanceInterceptor.setMaxTime(5L);

        return performanceInterceptor;
    }

执行sql就能够打印sql执行的信息了
image.png

依靠第三方插件美化sql输出

https://mp.baomidou.com/guide/p6spy.html

  1. 第三方依赖
<dependency>
            <groupId>p6spy</groupId>
            <artifactId>p6spy</artifactId>
            <version>3.8.5</version>
        </dependency>
  1. 更改配置文件中的dirver和url
spring:
  datasource:
#    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: root
#    url: jdbc:mysql://localhost:3306/test?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
    driver-class-name: com.p6spy.engine.spy.P6SpyDriver
    url: jdbc:p6spy:mysql://localhost:3306/test?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
  1. 增长spy.properties配置文件
module.log=com.p6spy.engine.logging.P6LogFactory,com.p6spy.engine.outage.P6OutageFactory
# 自定义日志打印
logMessageFormat=com.baomidou.mybatisplus.extension.p6spy.P6SpyLogger
#日志输出到控制台
appender=com.baomidou.mybatisplus.extension.p6spy.StdoutLogger
# 使用日志系统记录 sql
#appender=com.p6spy.engine.spy.appender.Slf4JLogger
# 设置 p6spy driver 代理
deregisterdrivers=true
# 取消JDBC URL前缀
useprefix=true
# 配置记录 Log 例外,可去掉的结果集有error,info,batch,debug,statement,commit,rollback,result,resultset.
excludecategories=info,debug,result,batch,resultset
# 日期格式
dateformat=yyyy-MM-dd HH:mm:ss
# 实际驱动可多个
#driverlist=org.h2.Driver
# 是否开启慢SQL记录
outagedetection=true
# 慢SQL记录标准 2 秒
outagedetectioninterval=2
  1. test

注意

开启性能分析会消耗系统的性能,因此性能分析插件要配合@Profile注解执行使用的环境。

SQL注入器 ->_-> 封装自定义通用SQL

实现步骤:

  1. 建立定义方法的类
  2. 建立注入器
  3. 在mapper中加入自定义方法

eg: 编写一个删除表全部数据的方法

  1. 建立定义方法的类
public class DeleteAllMethod extends AbstractMethod {
    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        // 执行的sql
        String sql = "delete from " + tableInfo.getTableName();
        // mapper接口方法名
        String method = "deleteAll";
        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, mapperClass);
        return addDeleteMappedStatement(mapperClass, method, sqlSource);
    }
}
  1. 建立注入器。添加本身的方法
@Component
public class MySqlInject extends DefaultSqlInjector {
    @Override
    public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
        List<AbstractMethod> methodList = super.getMethodList(mapperClass);
        methodList.add(new DeleteAllMethod());
        return methodList;
    }
}
  1. 在mapper中加入自定义方法
public interface UserMapper extends BaseMapper<User> {

    /**
     * 删除全部表数据
     *
     * @return 影响行数
     */
    int deleteAll();

}
  1. test
@Test
    public void deleteAll(){
        userMapper.deleteAll();
    }

image.png

  • 附录
  1. 参考源码https://github.com/xiao-ren-wu/notebook/blob/master/mybatis-plus-demo-2.zip