MyBatis-Plus 配置

xiaoxiao2025-08-03  95

配置

springBoot工程 配置 MapperScan 注解 @SpringBootApplication @MapperScan("mapper文件位置") public class Application { public static void main(String[] args) { SpringApplication.run(QuickStartApplication.class, args); } }

一般来说,简单工程通过以上配置就可以正常使用MyBatis-Plus了 时 MyBatis-Plus 提供了大量的个性化配置来满足不同复杂度的工程,可以按照项目需求使用

基本配置

configLocation

类型 string默认值 null MyBatis 配置文件位置,如果您有单独的 MyBatis 配置,请将其路径配置到 configLocation 中 mybatis-plus: mapper-locations: classpath*:mybatis/*.xml

typeAliasesPackage

类型 string默认值 null MyBaits 别名包扫描路径,通过该属性可以给包中的类注册别名,注册后在 Mapper 对应的 XML 文件中可以直接使用类名,而不用使用全限定的类名(即 XML 中调用的时候不用包含包名)。 mybatis-plus: type-aliases-package: com.baomidou.mybatisplus.samples.quickstart.entity

typeAliasesSuperType

类型 string默认值 null 该配置请和 typeAliasesPackage 一起使用,如果配置了该属性,则仅仅会扫描路径下以该类作为父类的域对象 。 mybatis-plus: type-aliases-package: com.baomidou.mybatisplus.samples.quickstart.entity type-aliases-super-type: java.lang.Object

typeHandlersPackage

类型 string默认值 null TypeHandler 扫描路径,如果配置了该属性,SqlSessionFactoryBean 会把该包下面的类注册为对应的 TypeHandler。 mybatis-plus: type-handlers-package: com.baomidou.mybatisplus.samples.quickstart.handler

typeEnumsPackage

类型 string默认值 null 枚举类 扫描路径,如果配置了该属性,会将路径下的枚举类进行注入,让实体类字段能够简单快捷的使用枚举属性,具体使用请结合 枚举注入 查看。 mybatis-plus: type-enums-package: com.baomidou.mybatisplus.samples.quickstart.enums

checkConfigLocation

类型 boolean默认值 false 启动时是否检查 MyBatis XML 文件的存在,默认不检查。 mybatis-plus: check-config-location: false

executorType

类型 ExecutorType

默认值 sample 通过该属性可指定 MyBatis 的执行器,MyBatis 的执行器总共有三种:

ExecutorType.SIMPLE:该执行器类型不做特殊的事情,为每个语句的执行创建一个新的预处理语句(PreparedStatement)

ExecutorType.REUSE:该执行器类型会复用预处理语句(PreparedStatement)

ExecutorType.BATCH:该执行器类型会批量执行所有的更新语句

mybatis-plus: executor-type: simple

configurationProperties

类型 Properties默认值 null 指定外部化 MyBatis Properties 配置,通过该配置可以抽离配置,实现不同环境的配置部署。 mybatis-plus: configuration-properties: classpath:mybatis/config.properties

进阶配置

本部分的配置大都为 MyBatis 原生支持的配置,这意味着您可以通过 MyBatis XML 配置文件的形式进行配置。

mapUnderscoreToCamelCase

类型 boolean默认值 true 是否开启自动驼峰命名规则(camel case)映射,即从经典数据库列名 A_COLUMN 到经典 Java 属性名 aColumn 的类似映射, 此属性在 mybatis 中原默认值为 false 在 mybatis-plus 中,此属性也将用于生成最终的 sql 的 select body 符合规则无需使用 @TableField 注解指定数据库字段名.

aggressiveLazyLoading

类型 boolean默认值 true 当设置为 true 的时候,懒加载的对象可能被任何懒属性全部加载,否则,每个属性都按需加载。需要和 lazyLoadingEnabled 一起使用。 mybatis-plus: configuration: aggressive-lazy-loading: true

autoMappingBehavior

类型 AutoMappingBehavior

默认值 partial

MyBatis 自动映射策略,通过该配置可指定 MyBatis 是否并且如何来自动映射数据表字段与对象的属性,总共有 3 种可选值:

AutoMappingBehavior.NONE:不启用自动映射AutoMappingBehavior.PARTIAL:只对非嵌套的 resultMap 进行自动映射AutoMappingBehavior.FULL:对所有的 resultMap 都进行自动映射 mybatis-plus: configuration: auto-mapping-behavior: partial

autoMappingUnknownColumnBehavior

类型 AutoMappingUnknownColumnBehavior

默认值 partial MyBatis 自动映射时未知列或未知属性处理策略,通过该配置可指定 MyBatis 在自动映射过程中遇到未知列或者未知属性时如何处理,总共有 3 种可选值:

AutoMappingUnknownColumnBehavior.NONE:不做任何处理 (默认值)AutoMappingUnknownColumnBehavior.WARNING:以日志的形式打印相关警告信息AutoMappingUnknownColumnBehavior.FAILING:当作映射失败处理,并抛出异常和详细信息 mybatis-plus: configuration: auto-mapping-unknown-column-behavior: none

cacheEnabled

类型:boolean默认值:true

全局地开启或关闭配置文件中的所有映射器已经配置的任何缓存,默认为 true。

mybatis-plus: configuration: cache-enabled: true

callSettersOnNulls

类型:boolean默认值:false 指定当前结果集为null的时候是否调用映射对象Setter(Map 对象时为 put)方法,通常运用于有 Map.keySet() 依赖或 null 值初始化的情况。 通俗的讲,即 MyBatis 在使用 resultMap 来映射查询结果中的列,如果查询结果中包含空值的列,则 MyBatis 在映射的时候,不会映射这个字段,这就导致在调用到该字段的时候由于没有映射,取不到而报空指针异常。 基本类型(int、boolean 等)是不能设置成 null 的。 mybatis-plus: configuration: call-setters-on-nulls: false

configurationFactory

类型:Class<?>默认值:null 指定一个提供 Configuration 实例的工厂类。该工厂生产的实例将用来加载已经被反序列化对象的懒加载属性值,其必须包含一个签名方法static Configuration getConfiguration()。(从 3.2.3 版本开始) mybatis-plus: configuration: configuration-factory: com.xxx.SampleConfigurationFactory

全局策略配置

refresh

类型:boolean默认值:false 是否自动刷新 Mapper 对应的 XML 文件,默认不自动刷新。如果配置了该属性,Mapper 对应的 XML 文件会自动刷新,更改 XML 文件后,无需再次重启工程,由此节省大量时间。 该配置不建议在生产环境打开! mybatis-plus: global-config: refresh: true

sqlParserCache

类型:boolean默认值:false 是否缓存 Sql 解析,默认不缓存。 mybatis-plus: global-config: sql-parser-cache: true

sqlSession

类型:SqlSession默认值:null 单例重用 SqlSession。 mybatis-plus: global-config: sql-session: com.xxx.SqlSession

sqlSessionFactory

类型:SqlSessionFactory默认值:null 缓存当前 Configuration 的 SqlSessionFactory。 mybatis-plus: global-config: sql-session-factory: com.xxx.SqlSessionFactory

DB 策略配置

capitalMode

类型:boolean默认值:false 是否开启大写命名,默认不开启。 mybatis-plus: global-config: db-config: capital-mode: false

columnLike

类型:boolean默认值:false 是否开启 LIKE 查询,即根据 entity 自动生成的 where 条件中 String 类型字段 是否使用 LIKE,默认不开启。 mybatis-plus: global-config: db-config: column-like: false

columnUnderline

此属性存在于 2.x 版本上,现同 mapUnderscoreToCamelCase 融合

dbType

类型:DbType默认值:OTHER 数据库类型,默认值为未知的数据库类型 如果值为OTHER,启动时会根据数据库连接 url 获取数据库类型;如果不是OTHER则不会自动获取数据库类型 mybatis-plus: global-config: db-config: db-type: mysql

idType

类型:IdType默认值:ID_WORKER 全局默认主键类型 mybatis-plus: global-config: db-config: id-type: id_worker

logicDeleteValue

类型:String默认值:1 逻辑已删除值,(逻辑删除下有效) mybatis-plus: global-config: db-config: logic-delete-value: 1

logicNotDeleteValue

类型:String默认值:0 逻辑未删除值,(逻辑删除下有效) mybatis-plus: global-config: db-config: logic-not-delete-value: 0

tablePrefix

类型:String默认值:null 表名前缀 mybatis-plus: global-config: db-config: table-prefix: xx_

tableUnderline

类型:boolean默认值:true 表名、是否使用下划线命名,默认数据库表使用下划线命名 mybatis-plus: global-config: db-config: table-underline: true
转载请注明原文地址: https://www.6miu.com/read-5034199.html

最新回复(0)