项目场景:
Springboot项目中使用mybatis-plus进行mysql查询;
问题描述
在mapper.xml中新增了一个新的查询方法selectIp
<select id="selectIp" resultMap="java.lang.String"> SELECT ip FROM `xxxx` GROUP BY ip </select>
服务启动后,所有的mybatis查询都报错:java.lang.IllegalArgumentException: Result Maps collection does not contain value for xxx
原因分析:
主要是因为我查询语句返回的是String类型的结果,但是上面的resultType我写成了resultMap;
resultType:当使用resultType做SQL语句返回结果类型时,对于SQL语句查询出的字段在相应的pojo中必须有和它相同的字段对应
例如:java.lang.Long和java.lang.String等以及自定义的实体
resultMap:当使用resultMap做SQL语句返回结果类型时,通常需要在mapper.xml中定义resultMap进行pojo和相应表字段的对应。
例如:
<resultMap id="ipMap" type="com.xxx.entity.IpAddress">
<result column="collector_list" property="collectorList"
typeHandler="com.xxx.hander.CollectorInfoHander"/>
<result column="setting" property="setting"
typeHandler="com.xxx.handlers.FastjsonTypeHandler"/>
<result column="ip" property="ip"/>
<result column="address" property="address"/>
</resultMap>
解决方案:
将上面的查询返回类型resultMap改为resultType即可;