【解决问题】mybatis plus 读取数据库没有返回值问题 返回值都为null

JAVA项目中使用mybatis plus 读取MySQL数据库,其中有个字段没有返回值问题,查看SQL都正常有值,但其最后返回值都为null。

排查过程:

可以先在配置文件加上如下语句,可以在控制台打印出mybatis执行时的具体sql、查询条件、返回值等等
.yml

mybatis-plus:
    configuration:
        log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

.properties

mybatis-plus.configuration.log-impl = org.apache.ibatis.logging.stdout.StdOutImpl

在控制台中看到SQL出来都正常,且有相应字段返回,其他字段都正常返回,只有一个带下划线的字段都为null。
可以看类似这样实体类字段名称有下划线的情况,会导致读取不到值

例如:

@TableName(value = "table")
public class Info {
    
    
    @TableField(value = "book_name")
    private String book_name;
}

可以看到官方相应配置中,有开启下划线转驼峰的功能。
把下划线形式的转成驼峰格式了,无法匹配到对应值就返回null了

MybatisConfiguration configuration = new MybatisConfiguration();
configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
configuration.setJdbcTypeForNull(JdbcType.NULL);
//下划线转为驼峰
configuration.setMapUnderscoreToCamelCase(true);
sqlSessionFactory.setConfiguration(configuration);

解决方案

1、可以选择在配置文件中把这个关闭
.yml

mybatis-plus:
  configuration:
    map-underscore-to-camel-case: false # 数据库下划线自动转驼峰标示关闭

.properties

mybatis.configuration.map-underscore-to-camel-case = false

2、可以直接选择写sql语句来获取这个值
遵循着能不改全局就不动的原则,毕竟也是前人先码下的语句,,改一处而动全身。。还是省事选择了这个。。

3、修改实体类,改成驼峰形式的就可以正常匹配了

猜你喜欢

转载自blog.csdn.net/weixin_44436677/article/details/127483445