本文共 679 字,大约阅读时间需要 2 分钟。
MyBatis无法查询出属性名和数据库字段名不完全相同的数据。
即:属性名和数据库字段名分别为驼峰命名和下划线命名时查出的数据为NULL。
MyBatis默认是属性名和数据库字段名一一对应的,即
数据库表列:user_name
实体类属性:user_name
但是java中一般使用驼峰命名
数据库表列:user_name
实体类属性:userName
在Spring Boot中,可以通过设置map-underscore-to-camel-case属性为true来开启驼峰功能。
MyBatis配置:
application.properties中:
#开启驼峰命名转换mybatis.configuration.map-underscore-to-camel-case=true
application.yml中:
mybatis: configuration: map-underscore-to-camel-case: true
使用该配置可以让MyBatis自动将SQL中查出来的带下划线命名的字段,自动转换为驼峰命名,再去匹配类中的属性。
例:
@Select("select phone_num,card_num from xxx where id=#{id}")public User getUserInfo(String id);查出来的结果,会被自动转换成phoneNum和cardNum,再和结果类(这里就是User )中的属性进行匹配。
DEBUG过程【大概40分钟以后】:
转载地址:http://gegaf.baihongyu.com/