一、基本配置:
- configLocation配置:MyBatis 配置文件位置,如果您有单独的 MyBatis 配置,请将其路径配置到 configLocation 中。
第一步:在application.yml中配置config-location
mybatis-plus:
config-location: classpath:mybatis-config.xml
mapper-locations: classpath*:com/mp/first/mapper/*.xml
# 全局策略配置为UUID
global-config:
db-config:
id-type: uuid
第二步:在对应的resource目录下建立一个与配置对应的mybatis-config.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd" >
<configuration>
...自己的配置
</configuration>
- mapperLocations配置:MyBatis Mapper 所对应的 XML 文件位置,如果您在 Mapper 中有自定义方法(XML 中有自定义实现),需要进行该配置,告诉 Mapper 所对应的 XML 文件位置(使用基本同上)
第一步:在application.yml中配置mapper-location
mybatis-plus:
mapper-locations: classpath*:com/mp/first/mapper/*.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.mp.first.dao.UserMapper">
<select id="selectAll" resultType="com.mp.first.entity.User">
select * from user ${ew.customSqlSegment}
</select>
<select id="selectUserPage" resultType="com.mp.first.entity.User">
select * from user ${ew.customSqlSegment}
</select>
</mapper>
- typeAliasesPackage配置:MyBaits 别名包扫描路径,通过该属性可以给包中的类注册别名,注册后在 Mapper 对应的 XML 文件中可以直接使用类名,而不用使用全限定的类名(即 XML 中调用的时候不用包含包名)
第一步:在application.yml中配置type-aliases-package
mybatis-plus:
mapper-locations: classpath*:com/mp/first/mapper/*.xml
type-aliases-package: com.mp.first.entity
第二步:在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.mp.first.dao.UserMapper">
<select id="selectAll" resultType="User">
select * from user ${ew.customSqlSegment}
</select>
<select id="selectUserPage" resultType="User">
select * from user ${ew.customSqlSegment}
</select>
</mapper>
二、进阶配置(本部分(Configuration)的配置大都为 MyBatis 原生支持的配置,这意味着您可以通过 MyBatis XML 配置文件的形式进行配置。
- mapUnderscoreToCamelCase配置:是否开启自动驼峰命名规则(camel case)映射,即从经典数据库列名 A_COLUMN(下划线命名) 到经典 Java 属性名 aColumn(驼峰命名) 的类似映射。
类型:boolean
默认值:true
注意:此属性在 MyBatis 中原默认值为 false,在 MyBatis-Plus 中,此属性也将用于生成最终的 SQL 的 select body;如果您的数据库命名符合规则无需使用 @TableField 注解指定数据库字段名
mybatis-plus:
configuration:
map-underscore-to-camel-case: true
这里要注意的是,configuration和config-location这两个配置不能同时出现,如果同时出现,会报错,比如下面这种情况就会报错
mybatis-plus:
config-location: classpath:mybatis-config.xml
configuration:
map-underscore-to-camel-case: true
三、DB策略配置
- insertStrategy/updateStrategy/selectStrategy配置:
值ignored代表不管有没有设置属性,所有的字段都会列到sql语句中;
值not_null为默认值,代表值为null的,会在sql中忽略掉,但不会忽略空字符串;
值not_empty代表值为null或者空字符串的,都会在sql中忽略掉
mybatis-plus:
# 全局策略配置为UUID
global-config:
db-config:
id-type: uuid
#代表不管有没有设置属性,所有字段都会列到sql语句中,没有设置的会插入null,一般不这样设置;
insert-strategy: ignored
#因为如果是update的话,你只想修改其中一个字段时,会把其他所有字段误设置为null
update-strategy: ignored
#如果是select的话,where条件会把所有没设置的字段也加入进去,并让等于null,如:where email = null
select-strategy: ignored
上面yml中配置的是全局策略,也可以再实体类的字段上给单独字段设置局部的字段验证策略,并且局部策略是要优于全局策略的
@TableField(insertStrategy = FieldStrategy.NOT_EMPTY)
private String email;
- tablePrefix配置:表名前缀
mybatis-plus:
# 全局策略配置为UUID
global-config:
db-config:
id-type: uuid
insert-strategy: ignored
update-strategy: ignored
select-strategy: ignored
table-prefix: mp_
这样表名如果是mp_user时,上面配置表名前缀后,sql生成的语句中也就能找到mp_user数据表了select * from mp_user where ...
- tableUnderline配置:表名是否使用下划线命名,默认是true