1、基本配置

1.1、configLocation(加载Mybatis的核心配置类)

MyBatis 配置⽂件位置,如果有单独的 MyBatis 配置,请将其路径配置到 configLocation 中。
MyBatis Configuration 的具体内容请参考MyBatis 官⽅⽂档

Spring Boot:

mybatis-plus.config-location = classpath:mybatis-config.xml

Spring MVC:
<bean id="sqlSessionFactory"
class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-config.xml"/> </bean>
1.2、mapperLocations(加载Mybatis的映射配置类)

MyBatis Mapper 所对应的 XML ⽂件位置,如果您在 Mapper 中有⾃定义方法(XML 中有⾃定义实现),需要进行该配置,告诉
Mapper 所对应的 XML ⽂件位置。

Spring Boot:

mybatis-plus.mapper-locations = classpath*:mybatis/*.xml

Spring MVC: 
<bean id="sqlSessionFactory"
class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
<property name="mapperLocations" value="classpath*:mybatis/*.xml"/> </bean>
Maven 多模块项⽬的扫描路径需以开头 classpath*:(即加载多个 jar 包下的 XML ⽂件)

1.3、typeAliasesPackage

MyBaits 别名包扫描路径,通过该属性可以给包中的类注册别名,注册后在 Mapper 对应的 XML ⽂件中可以直接使⽤类名,⽽不⽤使⽤全限定的类名(即
XML 中调⽤的时候不⽤包含包名)。

Spring Boot: 

mybatis-plus.type-aliases-package = com.lagou.mp.pojo

Spring MVC: 
<bean id="sqlSessionFactory"
class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
<property name="typeAliasesPackage" value="com.lagou.mp.pojo"/> </bean>

2、进阶配置 

本部分(Configuration)的配置⼤都为 MyBatis 原⽣⽀持的配置,这意味着您可以通过 MyBatis XML配置⽂件的形式进⾏配置。

2.1、mapUnderscoreToCamelCase

类型: boolean
默认值: true

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

注意:
此属性在 MyBatis 中原默认值为 false,在 MyBatis-Plus 中,此属性也将⽤于⽣成最终的 SQL 的select body
如果您的数据库命名符合规则⽆需使⽤ @TableField 注解指定数据库字段名 

示例(SpringBoot): 

#关闭⾃动驼峰映射,该参数不能和mybatis-plus.config-location同时存在
mybatis-plus.configuration.map-underscore-to-camel-case=false

2.2、cacheEnabled 

类型: boolean
默认值: true

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

 SpringBoot: 

mybatis-plus.configuration.cache-enabled=false

3、DB 策略配置

3.1、idType

类型: com.baomidou.mybatisplus.annotation.IdType
默认值: ID_WORKER 

全局默认主键类型,设置后,即可省略实体对象中的@TableId(type = IdType.AUTO)配置。 

SpringBoot: 

mybatis-plus.global-config.db-config.id-type=auto

SpringMVC: 
<!--这⾥使⽤MP提供的sqlSessionFactory,完成了Spring与MP的整合--> <bean
id="sqlSessionFactory"
class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/> <property name="globalConfig">
<bean class="com.baomidou.mybatisplus.core.config.GlobalConfig"> <property
name="dbConfig"> <bean
class="com.baomidou.mybatisplus.core.config.GlobalConfig$DbConfig"> <property
name="idType" value="AUTO"/> </bean> </property> </bean> </property> </bean>
3.2、tablePrefix

类型: String
默认值: null

表名前缀,全局配置后可省略@TableName()配置。 

SpringBoot:

mybatis-plus.global-config.db-config.table-prefix=tb_

SpringMVC: 
<bean id="sqlSessionFactory"
class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/> <property name="globalConfig">
<bean class="com.baomidou.mybatisplus.core.config.GlobalConfig"> <property
name="dbConfig"> <bean
class="com.baomidou.mybatisplus.core.config.GlobalConfig$DbConfig"> <property
name="idType" value="AUTO"/> <property name="tablePrefix" value="tb_"/> </bean>
</property> </bean> </property> </bean>

技术
今日推荐
PPT
阅读数 126
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信