在MP中有大量的配置,其中有一部分是Mybatis原生的配置,另一部分是MP的配置,详情:https://mybatis.plus/config/
下面我们对常用的配置做讲解。

一、基本配置

1 configLocation

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

Spring Boot:

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

Spring MVC:

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

2 mapperLocations

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

2.1 配置

Spring Boot:

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

Spring MVC:

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

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

2.2 测试

UserMapper.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hua.mapper.UserMapper">

    <select id="findById" resultType="com.hua.pojo.User">
     select * from user where id = #{id}
   </select>

</mapper>

UserMapper接口

package com.hua.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hua.pojo.User;

public interface UserMapper extends BaseMapper<User> {

    User findById(Long id);
}

测试用例:

@RunWith(SpringRunner.class) 
@SpringBootTest
public class UserMapperTest {

    @Autowired
    private UserMapper userMapper;


    @Test
    public void testSelectPage() {
        User user = userMapper.findById(1L); 
        System.out.println(user);
    }

}

运行结果:
配置信息 - 图1

3 typeAliasesPackage

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

Spring Boot:

mybatis-plus.type-aliases-package = com.hua.pojo

Spring MVC:

<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
    <property name="typeAliasesPackage"  value="com.hua.pojo"/>
</bean>

二、进阶配置

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

1 mapUnderscoreToCamelCase

  • 类型: boolean
  • 默认值: true

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

注意:

  1. 此属性在 MyBatis 中原默认值为 false,在 MyBatis-Plus 中,此属性也将用于生成最终的 SQL 的

select body

  1. 如果您的数据库命名符合规则无需使用 @TableField 注解指定数据库字段名

示例(SpringBoot):

#关闭自动驼峰映射,该参数不能和mybatis-plus.config-location同时存在
# 因为其可以在原生mybatis核心配置文件中配置
mybatis-plus.configuration.map-underscore-to-camel-case=false

2 cacheEnabled

  • 类型: boolean
  • 默认值: true

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

示例springboot:

mybatis-plus.configuration.cache-enabled=false

三、数据库策略配置

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>

需要库表已经设置开启自动递增

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>