← 返回首页
MybatisPlus基础教程(十七)
发表时间:2022-06-21 09:36:32
下划线驼峰转换

在mybatis-plus中,默认开启了下滑线-驼峰转换。例如:

String create_time;  //默认转换为 createTime;

String createTime; //默认转换为create_time;

在一个实体类中如果某个属性使用了一个带下划线的字段,这样查询出来的结果为null。例如:


//字段名字为create_time;
String create_time;  //默认转换为 createTime; 这样查询出来结果为null;

即使用了@TableField()注解指定了映射关系,也为null。

//字段名字为create_time;
@TableField("create_time")
String create_time;  //默认转换为 createTime; 这样查询出来结果为null;

解决方案

1)修改实例类属性的名字.


//字段名字为create_time;
String createTime;  //默认转换为 createTime; 这样查询出来结果为null;因此属性名字只能是createTime

2)把下划驼峰转换配置关掉即可。

在application.yml添加

mybatis-plus:
  configuration:
    map-underscore-to-camel-case: false

数据库字段支持驼峰标识解决方案

在application.yml添加以下配置:

spring:
  jpa:
    hibernate:
      naming:
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

把mybatis-plus下划驼峰转换配置关掉。在application.yml添加:

mybatis-plus:
  configuration:
    map-underscore-to-camel-case: false