万隆的笔记 万隆的笔记
博文索引
笔试面试
  • 在线学站

    • 菜鸟教程 (opens new window)
    • 入门教程 (opens new window)
    • Coursera (opens new window)
  • 在线文档

    • w3school (opens new window)
    • Bootstrap (opens new window)
    • Vue (opens new window)
    • 阿里开发者藏经阁 (opens new window)
  • 在线工具

    • tool 工具集 (opens new window)
    • bejson 工具集 (opens new window)
    • 文档转换 (opens new window)
  • 更多在线资源
  • Changlog
  • Aboutme
GitHub (opens new window)
博文索引
笔试面试
  • 在线学站

    • 菜鸟教程 (opens new window)
    • 入门教程 (opens new window)
    • Coursera (opens new window)
  • 在线文档

    • w3school (opens new window)
    • Bootstrap (opens new window)
    • Vue (opens new window)
    • 阿里开发者藏经阁 (opens new window)
  • 在线工具

    • tool 工具集 (opens new window)
    • bejson 工具集 (opens new window)
    • 文档转换 (opens new window)
  • 更多在线资源
  • Changlog
  • Aboutme
GitHub (opens new window)
  • MyBatis

  • Spring-MyBatis

  • MyBatis-Plus

    • MyBaits Plus简介
    • MyBaits Plus快速入门
    • MyBaits Plus CURD
    • MyBaits Plus配置
      • 基本配置
      • 进阶配置
      • DB 策略配置
    • Wrapper条件构造器
    • ActiveRecord
    • MyBaits Plus插件
    • MyBaits Plus拓展
    • MyBaits Plus代码生成器
  • MyBatis
  • MyBatis-Plus
2022-12-17
目录

MyBaits Plus配置

# MyBatis Plus配置

在MP中有⼤量的配置,其中有⼀部分是Mybatis原⽣的配置,另⼀部分是MP的配置,详情可见官方文档 (opens new window):下⾯我们对常⽤的配置做讲解。

# 基本配置

# configLocation

  • 类型:String
  • 默认值:null。

MyBatis 配置文件位置,如果您有单独的 MyBatis 配置,请将其路径配置到 configLocation 中。MyBatis Configuration 的具体内容请参考MyBatis官方文档 (opens new window)。

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>

# mapperLocations

  • 类型:String[]
  • 默认值:["classpath*:/mapper/**/*.xml"]

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

Spring Boot:

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

Spring MVC:

<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
	<property name="mapperLocations" value="classpath*:mappers/*.xml"/>
</bean>

tips

Maven 多模块项目的扫描路径需以 classpath*: 开头 (即加载多个 jar 包下的 XML 文件)

# typeAliasesPackage

  • 类型:String
  • 默认值:null。

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

Spring Boot:

mybatis-plus.type-aliases-package = org.example.hello.mp.entity

Spring MVC:

<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
 <property name="typeAliasesPackage" value="org.example.hello.mp.entity"/>
</bean>

# 进阶配置

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

# mapUnderscoreToCamelCase

  • 类型:boolean
  • 默认值:true。

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

注意

此属性在 MyBatis 中原默认值为 false,在 MyBatis-Plus 中,此属性也将⽤于⽣成最终的 SQL 的 select body。

如果您的数据库命名符合规则⽆需使⽤ @TableField 注解指定数据库字段名

Spring Boot:

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

# cacheEnabled

  • 类型:boolean
  • 默认值:true。

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

Spring Boot:

mybatis-plus.configuration.cache-enabled = false

# DB 策略配置

# idType

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

全局默认主键类型,详细配置见官网 (opens new window)。自 3.3.0 开始,默认使用ASSIGN_ID:雪花算法+UUID(不含中划线)

Spring Boot:

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

Spring MVC:

<!--这⾥使⽤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="ASSIGN_ID"/>
          </bean>
        </property>
			</bean>
	</property>
</bean>

# tablePrefix

  • 类型: String

  • 默认值: null

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

Spring Boot:

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

Spring MVC:

<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="ASSIGN_ID"/>
          <property name="tablePrefix" value="tb_"/>
  			</bean>
			</property>
		</bean>
	</property>
</bean>
上次更新: 5/30/2023, 11:23:39 PM
Wrapper条件构造器

Wrapper条件构造器→

最近更新
01
2025
01-15
02
Elasticsearch面试题
07-17
03
Elasticsearch进阶
07-16
更多文章>
Theme by Vdoing
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式