使用MyBatis 查询数据库查出有数据 但返回对象为null |
您所在的位置:网站首页 › mybatis查出来的字段为空 › 使用MyBatis 查询数据库查出有数据 但返回对象为null |
select a.user_id as userId, a.user_code as userCode, a.user_name as userName, a.status status, a.finish_time as finishTime, a.create_time as createTime from t_exployee a where a.user_id = #{userId,jdbctype=varchar} select a.user_id as userId, a.user_code as userCode, a.user_name as userName, a.status status, a.finish_time as finishTime, a.create_time as createTime from t_exployee a where a.user_code = #{userCode,jdbctype=varchar} public class Exployee { private static final long serialVersionUID = 1L; private String userId; private String userCode; private String userName; private String status; private Date createTime; private Date finishTime; //自动生成set、get方法 }
两个查询,参数和返回都是一样的,getExployeeByUserId 百试不爽,必定查询出结果;但是getExployeeByUserCode查询,时不时的查询结果返回为null,明明是有数据的;愁死我了,你要是一直查不出来也行啊,一会有,一会没有。百度的时候,有说是因为数据库表的字段和java的实体类字段对不上,建议配置ResultMap;查询返回的时候,用resultMap="setExployee" 而不是resultType
数据库表的字段和java的实体类字段对不上,这是原因之一,可以先核实一下,排除一下是不是这个原因;我反正不是这个原因 我的情况是这样的user_id 是主键,这个查询百分百;但是user_code不是主键,在mybatis里面,非主键查询可能不会返回唯一值,所以返回结果不能用单实体,必须要用List类型接收,我后来把接口方法改成返回List,就能查询出结果了。 也是绕了好久,各种排查
|
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |