Java学习——springboot中采用注解方式使用myBaits报错,关联对象错误

错误描述

数据库表名使用的是'_'命名,但是实体类中,我们使用的是驼峰式命名。常理来说,mybatis是可以自动关联两者,不需要属性名和字段一模一样,不需要写resultmap,但是在使用的过程中,就发现获取类型的值为空,这明显是没有关联到。

解决方法

xml文件配置:

在spring boot项目中没有mybatis.xml文件,配置文件application.properties中,加入配置项:

 mybatis.configuration.mapUnderscoreToCamelCase=true 或mybatis.configuration.map-underscore-to-camel-case=true

设为true表示开启驼峰转换。经过试验,两种配置方法都可以。但如果同时配置,前者mybatis.configuration.mapUnderscoreToCamelCase的优先级更高。

代码设置

由于我们是使用的多数据源,所以选择用代码:

sqlsessionFactory.getConfiguration.setMapUnderscoreToCamelCase(true)

这里是SqlsessionFactory多数据源配置的SqlsessionFactory,请自动装备SqlsessionFactory,或者从springboot中获取。

猜你喜欢

转载自blog.csdn.net/qq_23418043/article/details/82153855