MyBatis-Plus YAML 配置教程

一、基础配置示例

bash 复制代码
# application.yml
mybatis-plus:
  # 全局配置
  global-config:
    # 是否打印banner
    banner: true
    # 数据库相关配置
    db-config:
      # 主键类型 (AUTO:数据库自增, INPUT:手动输入, ASSIGN_ID:雪花算法, ASSIGN_UUID:UUID)
      id-type: auto
      # 表名是否使用下划线命名 (默认:true)
      table-underline: true
      # 逻辑删除字段名
      logic-delete-field: deleted
      # 逻辑删除值(默认:1)
      logic-delete-value: 1
      # 逻辑未删除值(默认:0)
      logic-not-delete-value: 0
      # 插入时是否自动填充字段
      insert-strategy: not_null
      # 更新时是否自动填充字段
      update-strategy: not_null
      # 查询时是否自动填充字段
      where-strategy: not_null
      # 数据库类型
      db-type: mysql
    
    # 刷新mapper
    refresh-mapper: true
    
  # 配置mapper.xml文件位置
  mapper-locations: 
    - classpath*:/mapper/**/*.xml
    - classpath*:/mybatis/mapper/*.xml
  
  # 实体类包路径(用于别名)
  type-aliases-package: com.example.demo.entity
  
  # TypeHandler包路径
  type-handlers-package: com.example.demo.handler
  
  # 配置MyBatis基础配置
  configuration:
    # 开启驼峰命名转换
    map-underscore-to-camel-case: true
    # 开启二级缓存
    cache-enabled: true
    # 开启延迟加载
    lazy-loading-enabled: true
    # 积极懒加载(false表示按需加载)
    aggressive-lazy-loading: false
    # 开启sql日志
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    # 默认执行器(REUSE:重用预处理语句)
    default-executor-type: reuse
    # 是否开启自动映射
    auto-mapping-behavior: partial
    # 是否允许JDBC生成主键
    use-generated-keys: true
    
  # 配置类型处理器包
  type-enums-package: com.example.demo.enums

二、分页插件配置

bash 复制代码
mybatis-plus:
  # 分页插件配置
  configuration:
    # 分页方言
    call-setters-on-nulls: true
  # 全局分页配置
  global-config:
    db-config:
      # 分页大小
      pagination-interceptor:
        # 默认每页条数
        default-page-size: 10
        # 最大每页条数
        max-limit: 1000

三、多环境配置示例

bash 复制代码
# application-dev.yml (开发环境)
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  global-config:
    banner: true
    db-config:
      logic-delete-value: 1
      logic-not-delete-value: 0

# application-prod.yml (生产环境)
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.nologging.NoLoggingImpl
  global-config:
    banner: false
    db-config:
      logic-delete-value: 1
      logic-not-delete-value: 0

四、完整配置参数说明表

配置项 说明 默认值 可选值
global-config.banner 是否打印banner true true/false
global-config.refresh-mapper 是否刷新mapper false true/false
global-config.db-config.id-type 全局主键类型 ASSIGN_ID AUTO, INPUT, ASSIGN_ID, ASSIGN_UUID, NONE
global-config.db-config.table-underline 表名、字段名下划线转驼峰 true true/false
global-config.db-config.logic-delete-field 逻辑删除字段名 deleted 自定义字段名
global-config.db-config.logic-delete-value 逻辑删除值 1 任意值
global-config.db-config.logic-not-delete-value 逻辑未删除值 0 任意值
global-config.db-config.db-type 数据库类型 UNKNOWN MYSQL, ORACLE, SQLSERVER, POSTGRE_SQL等
mapper-locations Mapper XML文件位置 classpath*:/mapper/**/*.xml 自定义路径
type-aliases-package 实体类包路径 null 包路径
type-handlers-package TypeHandler包路径 null 包路径
configuration.map-underscore-to-camel-case 驼峰命名转换 true true/false
configuration.cache-enabled 二级缓存开关 true true/false
configuration.lazy-loading-enabled 延迟加载开关 false true/false
configuration.log-impl 日志实现 StdOutImpl, Slf4jImpl等
configuration.default-executor-type 默认执行器类型 SIMPLE SIMPLE, REUSE, BATCH
configuration.auto-mapping-behavior 自动映射行为 PARTIAL NONE, PARTIAL, FULL

五、常用组合配置示例

1. MySQL开发环境配置
bash 复制代码
mybatis-plus:
  mapper-locations: classpath*:/mapper/**/*.xml
  type-aliases-package: com.example.entity
  global-config:
    db-config:
      id-type: auto
      logic-delete-field: deleted
      logic-delete-value: 1
      logic-not-delete-value: 0
  configuration:
    map-underscore-to-camel-case: true
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    cache-enabled: false
2. Oracle生产环境配置
bash 复制代码
mybatis-plus:
  mapper-locations: classpath*:/mapper/oracle/**/*.xml
  type-aliases-package: com.example.entity
  global-config:
    db-config:
      id-type: input
      db-type: oracle
      logic-delete-field: is_deleted
      logic-delete-value: Y
      logic-not-delete-value: N
  configuration:
    map-underscore-to-camel-case: true
    log-impl: org.apache.ibatis.logging.nologging.NoLoggingImpl
    cache-enabled: true

六、注意事项

  1. 配置优先级:XML配置 > YAML配置 > 默认配置

  2. 逻辑删除 :需要在实体类对应字段添加 @TableLogic 注解

  3. 分页插件 :需要单独配置 PaginationInterceptor Bean

  4. 性能优化:生产环境关闭SQL日志输出

  5. 版本兼容:不同版本配置项可能有差异,建议查阅对应版本文档

这个配置涵盖了MyBatis-Plus的大部分常用配置,可以根据实际项目需求进行调整。

相关推荐
北风toto1 天前
通过Entity 创建数据库中的表,目前只支持mysql,A.CTable使用mybatis/mybatis-plus自动创建表
数据库·mysql·mybatis
No8g攻城狮2 天前
【异常解决】SpringBoot3 + 人大金仓 V8+MyBatis-Plus 获取新增自增 ID
数据库·mybatis·人大金仓·国产信创
ElevenS_it1882 天前
Redis监控实战:内存使用+命中率+连接数三类核心指标接入Zabbix+分级告警完整配置方案
运维·网络·redis·mybatis·zabbix
JAVA社区2 天前
Java进阶全套教程(三)—— Spring框架核心精讲
java·开发语言·spring·面试·职场和发展·mybatis
谷哥的小弟2 天前
图文详解Spring Boot整合MyBatis(附源码)
spring boot·mysql数据库·mybatis·java框架
斯特凡今天也很帅2 天前
Spring Boot+mybatis项目切换sql为传参成无参
spring boot·sql·mybatis
JAVA社区2 天前
Java进阶全套教程(一)—— 数据框架Mybatis详解
java·开发语言·面试·职场和发展·mybatis
YOU OU3 天前
MyBatis 操作数据库(入门)
数据库·mybatis
wand codemonkey3 天前
SpringbootWeb【入门】+MySQL【安装】+【DataDrip安装 】+【连接MySQL】
java·mysql·mybatis
写了20年代码的老程序员3 天前
写了 20 年 Java,我受够了 MyBatis 的 4 个瞬间
mybatis·orm