mybaitsplus常见配置
configLocationsmapperLocations
mapper.xml代码案例 typeAliasesPackagemapUnderscoreToCamelCase
mybaitsplus常见配置在MP中有大量的配置,其中有一部分是Mybatis原生的配置,另一部分是MP的配置,详情请看官网的文档: https://baomidou.com/pages/56bac0/
下面我们对常用的配置做讲解。
configLocationsconfigLocations即MyBatis 配置文件位置,如果您有单独的 MyBatis 配置,请将其路径配置到 configLocation 中。 MyBatis Configuration 的具体内容请参考MyBatis 官方文档
示例: 1 在resources下创建mybatis-config.xml
在application.properties下配置configLocations,如下:
#指定mybatis-config.xml的位置 mybatis-plus.config-location = classpath:mybatis/mybatis-config.xmlmapperLocations
#指定mapper文件位置 mybatis-plus.mapper-locations = classpath*:mybatis/mapper public interface UserMapper extends baseMapper{ public User findById(Long id); }
在测试类中添加查找id 的方法
代码如下:
@Test
public void testFindByid(){
//查询tb_user记录
User user = userMapper.findById(2L);
System.out.println(user.toString());
}
运行结果:
注意:Maven 多模块项目的扫描路径需以 classpath*: 开头 (即加载多个 jar 包下的 XML 文件)
设置MyBaits 别名包扫描路径,通过该属性可以给包中的类注册别名,注册后在 Mapper 对应的 XML 文件中可以 直接使用类名,而不用使用全限定的类名(即 XML 中调用的时候不用包含包名)
mybatis‐plus.type‐aliases‐package = cn.itcast.mp.pojomapUnderscoreToCamelCase
类型: boolean
默认值: true
是否开启自动驼峰命名规则(camel case)映射,即从经典数据库列名 A_COLUMN(下划线命名) 到经典 Java 属性名 aColumn(驼峰命名) 的类似映射。
注意: 在 MyBatis-Plus 中此属性默认值为true,用于生成最终的 SQL 语句 如果您的数据库命名符合规则无需使用 @TableField 注解指定数据库字段名
#开启自动驼峰映射 #mybatis-plus.configuration.map-underscore-to-camel-case=true
注意:配置configuration.map-underscore-to-camel-case则不能配置config-location
那么配置了config-location,在想配置mapUnderscoreToCamelCase 怎么办呢?
这里就用之前mybaits的配置就可以了,在mybatis/mybatis-config.xml中添加配置:
就可以了
开启了自动转驼峰, 我们就可以屏蔽@TableField了,
再进行测试,没有问题
如果项目中有符合驼峰规则的定义也有不符合的,此时建议统一使用@TableField。
如果使用mybatis-config.xml的同时在application.properties配置mybatis-plus.configuration则报错
Property 'configuration' and 'configLocation' can not specified with together
解决方法: 只使用一种配置方法。
本案例屏蔽mybatis-plus.configuration.map-underscore-to-camel-case=true,在mybatis-config.xml中配置 settings。



