栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

mybaits-plus常见配置

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

mybaits-plus常见配置

文章目录

mybaitsplus常见配置

configLocationsmapperLocations

mapper.xml代码案例 typeAliasesPackagemapUnderscoreToCamelCase

mybaitsplus常见配置

在MP中有大量的配置,其中有一部分是Mybatis原生的配置,另一部分是MP的配置,详情请看官网的文档: https://baomidou.com/pages/56bac0/

下面我们对常用的配置做讲解。

configLocations

configLocations即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.xml

mapperLocations
#指定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 文件)

typeAliasesPackage

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

mybatis‐plus.type‐aliases‐package = cn.itcast.mp.pojo
mapUnderscoreToCamelCase

类型: 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。

 
	 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/777611.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号