java.lang.IllegalArgumentException: Result Maps collection does not contain value for com.lxx.mapper.StudentMapper.int错误
在做小型的springboot项目时,遇到了一个百思不得其解的问题,明明进行的时登录或者注册,但是项目却报StudentMapper的错误
当时问题出来后,我还以为时登录注册部分的问题,但是在我找了一遍问题后,发现没有任何问题,我上网查查查查查查,网上各位大神们说的都是resultMap和resultType没有搞清楚,用错了 我还以为时我resultMap里的唯一标识在用的时候拼错了,我就又找呀找,一个小时后,我继续上网查这个问题,提示的还是resultMap和resultType的问题,所以我就又去看我的xml文件,终于我发现是我晕了,resultMap部分没有用错,只是有一个该用成resultType的地方也用成了resultMap了,晕了晕了
原本是这样的:
// An highlighted block
<select id="totalCounts" resultMap="int">
select count(distinct s.id)
from t_student s
left join t_city c on s.cityid = c.id
left join t_clazz cl on s.clazzid = cl.id
left join t_group g on s.groupid = g.id
left join t_student_tag st on s.id = st.studentid
left join t_tag t on st.tagid = t.id
<where>
<if test="col!=null and col!='' and val!=null and val!=''">
${col} like concat('%',#{val},'%')
</if>
</where>
</select>
应该改为:
// An highlighted block
<select id="totalCounts" resultType="int">
select count(distinct s.id)
from t_student s
left join t_city c on s.cityid = c.id
left join t_clazz cl on s.clazzid = cl.id
left join t_group g on s.groupid = g.id
left join t_student_tag st on s.id = st.studentid
left join t_tag t on st.tagid = t.id
<where>
<if test="col!=null and col!='' and val!=null and val!=''">
${col} like concat('%',#{val},'%')
</if>
</where>
</select>
这里我觉得我需要总结一下resultMap和resultType的区别:
resultType和resultMap功能类似 ,都是返回对象信息 ,但是resultMap要更强大一些 ,可自定义。
- 基本映射:resultType ,使用resultType进行输出映射,只有查询出来的列名和pojo中的属性名一致,该列才可以映射成功。(数据库,实体,查询字段,这些全部都得一一对应)…… resultType就比较鸡肋了,必须字段名一样;
2.高级映射:resultMap ,可自定义。因为resultMap要配置一下,表和类的一一对应关系,所以说就算你的字段名和你的实体类的属性名不一样也没关系,都会给你映射出来;