Mybatis-Plus:配置

Mybatis-Plus:配置

在MP中有⼤量的配置,其中有⼀部分是Mybatis原⽣的配置,另⼀部分是MP的配置,详情见官网:https://baomidou.com/

MyBatis-Plus_19

1、基本配置

1.1、configLocation

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

Spring Boot:

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"></property>
</bean>

1.2、mapperLocations

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

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"></property>
</bean>

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

测试:

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.tao.mapper.UserMapper">
    <select id="findById" resultType="com.tao.pojo.User">
        select * from user where id = #{id}
    </select>
</mapper>

在UserMapper接口中添加findById方法:

/*
    自定义findById方法
 */
public User findById(Long id);

测试⽤例:

/*
    测试自定义方法findById
 */
@Test
public void findById(){
    User user = userMapper.findById(12L);
    System.out.println(user);
}

结果:

User(id=12, name=小七, age=20, mail=null, address=null)

1.3、typeAliasesPackage

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

Spring Boot:

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

Spring MVC:

<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
    <property name="typeAliasesPackage" value="com.baomidou.mybatisplus.samples.quickstart.entity"></property>
</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。

示例:

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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值