在自己写的mapper.xml文件时,查询数据库一些字段正常、一些字段为null。

经过对比发现为null的都是采用了 “_” 的命名字段,比如last_login_time。

因为我的pojo和mapper文件都是采用了逆向工程生成的,对于_命名的数据库字段,逆向工程生成的pojo这些字段变成了驼峰命名,对于逆向工程生成的mapper文件,查询结果返回的resultMap已经有映射起来了。而对于自己写的mapper.xml与生成的pojo没有映射到,所以查询的结果才为null。

解决方案

方案一:使用别名的方式

<select id=”findUserByOpenid” parameterType=”String” resultType=”userCustom”>
select
id,
nickname,
openid,
first_login_time as firstLoginTime,
last_login_time as lastLoginTime
from user
where openid = #{openId}
</select>
方案二(推荐):在springboot的application.properties配置文件开启mybatis的驼峰命名转换

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

————————————————
版权声明:本文为CSDN博主「温城(Anson)」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_37451395/article/details/123601028