首页 >> 知识 >> 使用配置

使用配置

MyBatis-Plus 提供了丰富的配置选项,以满足不同用户的需求。这些配置中,一部分继承自 MyBatis 原生支持的配置,另一部分则是 MyBatis-Plus 特有的扩展配置。

使用方式Spring Boot 配置

在 Spring Boot 项目中,可以通过 application.yml 或 application.properties 文件来配置 MyBatis-Plus。

mybatis-plus: configuration: # MyBatis 配置 map-underscore-to-camel-case: true global-config: # 全局配置 db-config: # 数据库配置 id-type: autoSpring MVC 配置

在传统的 Spring MVC 项目中,可以通过 XML 配置文件来配置 MyBatis-Plus。

Baseconfiglocations类型:String默认值:null

指定 MyBatis 配置文件的位置。如果有单独的 MyBatis 配置文件,应将其路径配置到 configlocations。

配置示例:

mybatis-plus: config-locations: classpath:/mybatis-config.xmlmapperlocationss类型:String[]默认值:["classpath*:/mapper/**/*.xml"]

指定 MyBatis Mapper 对应的 XML 文件位置。如果在 Mapper 中有自定义方法,需要配置此项。

配置示例:

mybatis-plus: mapper-locationss: classpath:/mapper/**.xml

对于 Maven 多模块项目,扫描路径应以 classpath*: 开头,以加载多个 JAR 包中的 XML 文件。

typeAliasesPackage类型:String默认值:null

指定 MyBatis 别名包扫描路径,用于给包中的类注册别名。注册后,在 Mapper 对应的 XML 文件中可以直接使用类名,无需使用全限定类名。

配置示例:

mybatis-plus: type-aliases-package: com.your.domaintypeAliasesSuperType类型:Class默认值:null

与 typeAliasesPackage 一起使用,仅扫描指定父类的子类。

配置示例:

mybatis-plus: type-aliases-super-type: com.your.domain.BaseEntitytypeHandlersPackage类型:String默认值:null

指定 TypeHandler 扫描路径,用于注册自定义类型转换器。

配置示例:

mybatis-plus: type-handlers-package: com.your.typehandlers

TypeHandler 用于自定义类型转换。

typeEnumsPackage类型:String默认值:null

从 3.5.2 版本开始,该配置无效,通用枚举功能无需配置即可使用。

checkConfiglocations Spring Boot Only 类型:boolean默认值:false

指定启动时是否检查 MyBatis XML 文件的存在,默认不检查。

配置示例:

mybatis-plus: check-config-locations: trueexecutorType Spring Boot Only 类型:ExecutorType默认值:simple

指定 MyBatis 的执行器类型,包括 SIMPLE、REUSE 和 BATCH。

配置示例:

mybatis-plus: executor-type: reuseconfigurationProperties类型:Properties默认值:null

指定外部化 MyBatis Properties 配置,用于抽离配置,实现不同环境的配置部署。

配置示例:

mybatis-plus: configuration-properties: classpath:/mybatis-properties.propertiesconfiguration类型:Configuration默认值:null

原生 MyBatis 所支持的配置,具体请查看 具体请查看 Configuration。

globalConfig类型:com.baomidou.mybatisplus.core.config.GlobalConfig默认值:GlobalConfig::new

MyBatis-Plus 全局策略配置,具体请查看 GlobalConfig。

配置示例:

mybatis-plus: global-config: db-config: table-prefix: tbl_ id-type: autoConfiguration

MyBatis-Plus 的 Configuration 配置继承自 MyBatis 原生支持的配置,这意味着您可以通过 MyBatis XML 配置文件的形式进行配置,也可以通过 Spring Boot 或 Spring MVC 的配置文件进行设置。

mapUnderscoreToCamelCase类型:boolean默认值:true

开启自动驼峰命名规则(camel case)映射,即从经典数据库列名 A_COLUMN(下划线命名) 到经典 Java 属性名 aColumn(驼峰命名) 的类似映射。

配置示例:

mybatis-plus: configuration: map-underscore-to-camel-case: true

在 MyBatis-Plus 中,此属性也将用于生成最终的 SQL 的 select body。如果您的数据库命名符合规则,无需使用 @TableField 注解指定数据库字段名。

defaultEnumTypeHandler类型:Class默认值:null

指定一个提供 Configuration 实例的工厂类。该工厂生产的实例将用来加载已被反序列化对象的懒加载属性值。工厂类必须包含一个签名方法 static Configuration getConfiguration()。

配置示例:

mybatis-plus: configuration: configuration-factory: com.your.config.MyConfigurationFactoryGlobalConfig

GlobalConfig 是 MyBatis-Plus 提供的全局策略配置,它允许开发者对 MyBatis-Plus 的行为进行全局性的定制。

banner类型:boolean默认值:true

控制是否在控制台打印 MyBatis-Plus 的 LOGO。

配置示例:

mybatis-plus: global-config: banner: falseenableSqlRunner类型:boolean默认值:false

控制是否初始化 SqlRunner(com.baomidou.mybatisplus.extension.toolkit.SqlRunner)。

配置示例:

mybatis-plus: global-config: enable-sql-runner: truesqlInjector类型:com.baomidou.mybatisplus.core.injector.ISqlInjector默认值:com.baomidou.mybatisplus.core.injector.DefaultSqlInjector

SQL 注入器,用于注入 MyBatis-Plus 提供的通用方法。Starter 下支持@Bean注入。

配置示例:

mybatis-plus: global-config: sql-injector: com.baomidou.mybatisplus.core.injector.DefaultSqlInjector@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor()); return interceptor;}superMapperClass类型:Class默认值:com.baomidou.mybatisplus.core.mapper.Mapper.class

通用 Mapper 父类,只有该父类的子类 Mapper 才会注入 sqlInjector 内的方法。

metaObjectHandler类型:com.baomidou.mybatisplus.core.handlers.MetaObjectHandler默认值:null

元对象字段填充控制器,用于自动填充实体类的字段。Starter 下支持@Bean注入。

配置示例:

mybatis-plus: global-config: meta-object-handler: com.example.MyMetaObjectHandler@Beanpublic MetaObjectHandler metaObjectHandler() { return new MyMetaObjectHandler();}identifierGenerator Since 3.3.0 类型:com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator默认值:com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator

Id 生成器,用于生成实体类的唯一标识。Starter 下支持@Bean注入。

配置示例:

mybatis-plus: global-config: identifier-generator: com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator@Beanpublic IdentifierGenerator identifierGenerator() { return new CustomIdentifierGenerator();}dbConfig类型:com.baomidou.mybatisplus.core.config.GlobalConfig$DbConfig默认值:null

MyBatis-Plus 全局策略中的 DB 策略配置,具体请查看 DbConfig。

配置示例:

mybatis-plus: global-config: db-config: table-prefix: tbl_ id-type: ASSIGN_IDDbConfigidType类型:com.baomidou.mybatisplus.annotation.IdType默认值:ASSIGN_ID

全局默认主键类型。

IdType.AUTO:使用数据库自增 ID 作为主键。IdType.NONE:无特定生成策略,如果全局配置中有 IdType 相关的配置,则会跟随全局配置。IdType.INPUT:在插入数据前,由用户自行设置主键值。IdType.ASSIGN_ID:自动分配 ID,适用于 Long、Integer、String 类型的主键。默认使用雪花算法通过 IdentifierGenerator 的 nextId 实现。 @since 3.3.0 IdType.ASSIGN_UUID:自动分配 UUID,适用于 String 类型的主键。默认实现为 IdentifierGenerator 的 nextUUID 方法。 @since 3.3.0

配置示例:

mybatis-plus: global-config: db-config: id-type: ASSIGN_IDtablePrefix类型:String默认值:null

表名前缀

配置示例:

mybatis-plus: global-config: db-config: table-prefix: tbl_schema类型:String默认值:null

指定数据库的 Schema 名称,通常不用设置。

配置示例:

mybatis-plus: global-config: db-config: schema: my_schemacolumnFormat类型:String默认值:null

用于在生成 SQL 时对字段名进行格式化,例如添加前缀或后缀,对主键无效,例: %s。

配置示例:

mybatis-plus: global-config: db-config: column-format: %s_fieldtableFormat Since 3.5.3.2 类型:String默认值:null

在生成 SQL 时对表名进行格式化,例: %s。

配置示例:

mybatis-plus: global-config: db-config: table-format: tbl_%spropertyFormat Since 3.3.0 类型:String默认值:null

用于在 Entity 的字段映射到数据库字段时进行格式化,只有在 column as property 这种情况下生效,对主键无效,例: %s。

配置示例:

mybatis-plus: global-config: db-config: property-format: %s_proptableUnderline类型:boolean默认值:true

控制表名是否使用驼峰转下划线命名。

配置示例:

mybatis-plus: global-config: db-config: table-underline: falsecapitalMode类型:boolean默认值:false

控制表名和字段名是否使用大写命名。

配置示例:

mybatis-plus: global-config: db-config: capital-mode: truekeyGenerator类型:com.baomidou.mybatisplus.core.incrementer.IKeyGenerator默认值:null

自定义表主键生成器。Starter 下支持@Bean注入。

配置示例:

mybatis-plus: global-config: db-config: key-generator: com.example.CustomKeyGenerator@Beanpublic IKeyGenerator keyGenerator() { return new CustomKeyGenerator();}logicDeleteField类型:String默认值:null

全局的 Entity 逻辑删除字段属性名,仅在逻辑删除功能打开时有效。

配置示例:

mybatis-plus: global-config: db-config: logic-delete-field: deletedlogicDeletevalsue类型:String默认值:1

逻辑已删除值,仅在逻辑删除功能打开时有效。

配置示例:

mybatis-plus: global-config: db-config: logic-delete-value: truelogicNotDeletevalsue类型:String默认值:0

逻辑未删除值,仅在逻辑删除功能打开时有效。

配置示例:

mybatis-plus: global-config: db-config: logic-not-delete-value: falseinsertStrategy类型:com.baomidou.mybatisplus.annotation.FieldStrategy默认值:NOT_NULL

控制字段在 Insert 时的字段验证策略。

FieldStrategy.DEFAULT:遵循全局配置的策略。如果全局配置未指定,默认行为是仅在字段值不为 NULL 时插入该字段。FieldStrategy.ALWAYS:总是插入该字段,无论字段值是否为 NULL。FieldStrategy.NOT_NULL:仅在字段值不为 NULL 时插入该字段。FieldStrategy.NOT_EMPTY:仅在字段值不为空(对于字符串类型)或不为 NULL(对于其他类型)时插入该字段。FieldStrategy.NEVER:从不插入该字段,即使字段值不为 NULL。FieldStrategy.IGNORED: 忽略判断,效果等同于”ALWAYS” @Deprecated

配置示例:

mybatis-plus: global-config: db-config: insert-strategy: NEVERupdateStrategy类型:com.baomidou.mybatisplus.annotation.FieldStrategy默认值:NOT_NULL

控制字段在 Update 时的字段验证策略。

配置示例:

mybatis-plus: global-config: db-config: update-strategy: IGNOREDwhereStrategy类型:com.baomidou.mybatisplus.annotation.FieldStrategy默认值:NOT_NULL

控制字段在 Update 时的字段验证策略。既 Wrapper 根据内部 Entity 生成的 Where 条件。

配置示例:

mybatis-plus: global-config: db-config: where-strategy: ALWAYS
网站地图