基础配置
一、configLocation
MyBatis 配置文件位置,如果您有单独的 MyBatis 配置,请将其路径配置到 configLocation
中。
1、Spring Boot:
mybatis-plus:
......
configuration:
......
global-config:
......
db-config:
......
示例
2、springMVC
<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
<property name="configuration" ref="configuration"/> <!-- 非必须 -->
<property name="globalConfig" ref="globalConfig"/> <!-- 非必须 -->
......
</bean>
<bean id="configuration" class="com.baomidou.mybatisplus.core.MybatisConfiguration">
......
</bean>
<bean id="globalConfig" class="com.baomidou.mybatisplus.core.config.GlobalConfig">
<property name="dbConfig" ref="dbConfig"/> <!-- 非必须 -->
......
</bean>
<bean id="dbConfig" class="com.baomidou.mybatisplus.core.config.GlobalConfig.DbConfig">
......
</bean>
示例
二、mapperLocations
- 类型:
String[]
- 默认值:
["classpath*:/mapper/**/*.xml"]
MyBatis Mapper 所对应的 XML 文件位置,如果您在 Mapper 中有自定义方法(XML 中有自定义实现),需要进行该配置,告诉 Mapper 所对应的 XML 文件位置。
注意
Maven 多模块项目的扫描路径需以 classpath*:
开头 (即加载多个 jar 包下的 XML 文件)
测试自定义方法
三、 typeAliasesPackage
- 类型:
String
- 默认值:
null
MyBaits 别名包扫描路径,通过该属性可以给包中的类注册别名,注册后在 Mapper 对应的 XML 文件中可以直接使用类名,而不用使用全限定的类名(即 XML 中调用的时候不用包含包名)。
进阶配置
本部分(Configuration)的配置大都为 MyBatis 原生支持的配置,这意味着您可以通过 MyBatis XML 配置文件的形式进行配置。
四、mapUnderscoreToCamelCase
- 类型:
boolean
- 默认值:
true
是否开启自动驼峰命名规则(camel case)映射,即从经典数据库列名 A_COLUMN(下划线命名) 到经典 Java 属性名 aColumn(驼峰命名) 的类似映射。
注意
此属性在 MyBatis 中原默认值为 false,在 MyBatis-Plus 中,此属性也将用于生成最终的 SQL 的 select body
如果您的数据库命名符合规则无需使用 @TableField
注解指定数据库字段名
不能共存
五、cacheEnabled
- 类型:
boolean
- 默认值:
true
开启 Mybatis 二级缓存,默认为 true。
DB策略配置
六、idType
- 类型:
com.baomidou.mybatisplus.annotation.IdType
- 默认值:
ASSIGN_ID
1、全局默认主键类型。
2、单个主键类型。
七、tablePrefix
- 类型:
String
- 默认值:
null
1、表名前缀。
2、单个表名前缀。