• mybatis-plus 插件


    mybatis-plus 插件

    #DO

    @Data
    @Accessors(chain = true)
    @TableName("tbl_employee")
    public class Employee implements Serializable {
    
        private static final long serialVersionUID = 1L;
    
        @TableId(value = "id", type = IdType.AUTO)
        private Integer id;
    
        private String lastName;
    
        private String email;
    
        private String gender;
    
        private Integer age;
        //标明该字段是乐观锁的字段
        @Version
        private Integer version;
        //标明该字段是逻辑删除字段
        @TableLogic
        private Integer deleted;
        //自动插入字段
        @TableField(fill = FieldFill.INSERT)
        @JsonDeserialize(using = LocalDateTimeDeserializer.class)
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
        private LocalDateTime date;
    }
    
    

    #配置类

    @Configuration
    public class MybatisPlusConf {
        //分页配置
        @Bean
        public PaginationInterceptor paginationInterceptor() {
            PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
            //防止恶意sql注入
            ArrayList<ISqlParser> sqlParsers = new ArrayList<>();
            sqlParsers.add(new BlockAttackSqlParser());
            paginationInterceptor.setSqlParserList(sqlParsers);
            return paginationInterceptor;
        }
    
        @Bean//乐观锁插件
        public OptimisticLockerInterceptor optimisticLockerInterceptor() {
            return new OptimisticLockerInterceptor();
        }
    }
    
    @Slf4j
    @Component
    public class MyMetaObjectHandler implements MetaObjectHandler {
        /*
        insert时自动填充指定字段
         */
        @Override
        public void insertFill(MetaObject metaObject) {
            log.info("start insert fill ....");
            //二者选一即可
            this.strictUpdateFill(metaObject, "date", LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐使用)
            this.fillStrategy(metaObject, "date", LocalDateTime.now());
            //如果是FieldFill.INSERT_UPDATE的字段同样要在该方法中声明
        }
    
        /*
        update时自动填充指定字段
         */
        @Override
        public void updateFill(MetaObject metaObject) {
    
        }
    }
    

    #sql性能分析插件

    • yaml
    spring:
      datasource:
        #    driver-class-name: com.mysql.cj.jdbc.Driver
        #性能分析插件,将driverClass修改为P6SpyDriver
        driver-class-name: com.p6spy.engine.spy.P6SpyDriver
        #    url: jdbc:mysql://localhost:3306/mp?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
        #    修改url为jdbc:p6sy:数据库://地址
        url: jdbc:p6spy:mysql://localhost:3306/mp?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
    
    • 添加spy.properties
    #3.2.1以上使用
    modulelist=com.baomidou.mybatisplus.extension.p6spy.MybatisPlusLogFactory,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,commit,resultset
    # 日期格式
    dateformat=yyyy-MM-dd HH:mm:ss
    # 实际驱动可多个
    #driverlist=com.mysql.cj.jdbc.Driver
    # 是否开启慢SQL记录
    outagedetection=true
    # 慢SQL记录标准 2 秒
    outagedetectioninterval=2
    
  • 相关阅读:
    使用自定义注解动态绑定多实现类实例
    使用策略模式和工厂模式动态绑定多实现类实例
    使用责任链模式动态绑定多实现类实例
    使用模板方法模式动态绑定多实现类实例
    IDEA 调试Java代码的两个技巧
    Maven中dependencyManagement标签的正确使用方法
    Spring注解之获取自定义注解信息
    Spring注解之自定义注解入门
    Spring 动态绑定多实现类实例综述
    数据迁移测试
  • 原文地址:https://www.cnblogs.com/kikochz/p/12913318.html
Copyright © 2020-2023  润新知