«

springboot 2.4集成mybatis-plus

时间:2024-5-15 16:23     作者:紫琪软件工作室     分类: mybatis-plus


1、pom版本号引入

1.2.62 3.4.0 ## 2、pom.xml引入 ```xml com.alibaba fastjson ${fastjson.version} org.projectlombok lombok true mysql mysql-connector-java runtime com.baomidou mybatis-plus-boot-starter ${mybatis-plus.version} com.baomidou mybatis-plus-extension ${mybatis-plus.version} com.baomidou mybatis-plus ${mybatis-plus.version} ``` ## 3、application.yml 配置 ```yaml spring: # jackson 格式 jackson: date-format: yyyy-MM-dd HH:mm:ss time-zone: GMT+8 datasource: url: jdbc:mysql://localhost:3306/databaseName?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true&allowMultiQueries=true&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true username: user password: pwd driver-class-name: com.mysql.cj.jdbc.Driver #mybatis-plus mybatis-plus: mapper-locations: classpath:/mapper/**/*Mapper.xml type-aliases-package: cn.*.model # 实体基类 type-aliases-super-type: cn.base.model.BaseBean # 实体内部使用枚举类包路径 type-enums-package: cn.*.defaultEnum configuration: local-cache-scope: statement default-enum-type-handler: com.baomidou.mybatisplus.extension.handlers.MybatisEnumTypeHandler log-impl: org.apache.ibatis.logging.stdout.StdOutImpl global-config: # 自定义Mapper顶级接口 super-mapper-class: cn.base.mybatis.plus.mapper.IDefaultBaseMapper db-config: id-type: AUTO ``` ## 4、分页插件支持 ```java /** * mybatis-plus 配置类 * * @version V1.0 * @author jj-tech * @date 2020-10-15 11:17 **/ @EnableTransactionManagement @Configuration @MapperScan(basePackages = "mapper接口包") public class MybatisPlusConfig { /** * 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除) */ @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); return interceptor; } } ``` ## 5、如配置了自定义Mapper基类,所有Mapper需要集成BaseMapper ## 6、自定义枚举类,存入数据库字段的值,需添加注解@EnumValue:例如 ```java import com.baomidou.mybatisplus.annotation.EnumValue; /** * 文件类型枚举类 * @author ZhangJi */ public enum AffixType { /** * 文件 */ FILE(1, "文件"), /** * 文件夹 */ FOLDER(2, "文件夹"); @EnumValue private final Integer value; private final String desc; AffixType(Integer value, String desc) { this.value = value; this.desc = desc; } @Override public String toString() { return "FileType{" + "value=" + value + ", desc='" + desc + '\'' + '}'; } } ``` ### #####疑问留言:1770202799@qq.com#####