使用MyBatis 查询数据库查出有数据 但返回对象为null

<!-- 根据主键id查询订单信息-->
<select id="getExployeeByUserId" parametertype="java.lang.string" resulttype="com.my.vo.Exployee">
    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>

<select id="getExployeeByUserCode" parametertype="java.lang.string" resulttype="com.my.vo.Exployee">
    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}
</select>

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

  <resultMap id="setExployee" type="com.my.vo.Exployee">
    <result column="user_id" property="userId" jdbcType="VARCHAR"/>
    <result column="user_code" property="userCode" jdbcType="VARCHAR"/>
    <result column="user_name" property="userName" jdbcType="VARCHAR"/>
 
    <result column="status" property="status" jdbcType="VARCHAR"/>
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
    <result column="finish_time" property="finishTime" jdbcType="TIMESTAMP"/>
</resultMap>

数据库表的字段和java的实体类字段对不上,这是原因之一,可以先核实一下,排除一下是不是这个原因;我反正不是这个原因

我的情况是这样的user_id 是主键,这个查询百分百;但是user_code不是主键,在mybatis里面,非主键查询可能不会返回唯一值,所以返回结果不能用单实体,必须要用List类型接收,我后来把接口方法改成返回List,就能查询出结果了。

也是绕了好久,各种排查

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章