前提
Java代码中定义的ID名为 userId
MySql中的定义的ID名为 id
这样在查询时,就会出现查不到id的值
方法一 加别名
select id as userid , username, password as userPassword from user
方法二 使用resultMap
@Select("select * from user")
@Results(id = "resultMap" , value = {
@Result(property = "userId",column = "id")
})
其中定义的 id = "resultMap" 使得其他方法可以通过
@ResultMap("resultMap") 这种方式来调用这个属性
比如:
@ResultMap("resultMap")
@Select("select * from user where username like #{name}")
本文探讨了Java代码中userId与MySql数据库中id字段不一致导致的问题,并提供了两种解决方案:通过SQL语句添加别名和使用MyBatis的resultMap特性进行字段映射。
375

被折叠的 条评论
为什么被折叠?



