有好久一段时间没写mybatis的xml了,出现这样的问题,写一下加深印象。
错误分析:
这个错误是因为在从数据库查出多条数据,但是接收的时候只显示一个。
解决方案:
举个例子说明问题
<resultMap id="BaseResultMap" type="net.yang.apiV1.entity.ShopFetchRange">
<id column="id" property="id" jdbcType="BIGINT" />
<result column="member_id" property="memberId" jdbcType="BIGINT" />
<result column="fetch_range" property="fetchRange" jdbcType="VARCHAR" />
<result column="longitude" property="longitude" jdbcType="DECIMAL" />
<result column="latitude" property="latitude" jdbcType="DECIMAL" />
</resultMap>
<select id="rangesIsExist" resultMap="BaseResultMap" parameterType="java.util.Map">
SELECT IFNULL(id,1) FROM shop_fetch_range where longitude=#{longitude,jdbcType=BIGINT} and member_id=#{memberId}
and latitude=#{latitude,jdbcType=BIGINT}
</select>
在这里需要说一个bug
attempt to return null from a method with a primittive return tyep(long)
这个bug的意思返回的值是null,这个时候就需要使用 ifnull(a,1)如果a是null的话,就返回1,这样在java后台进行接收的时候就使用List接收,这样就行了。
接着说第一个bug,那个bug使用不能更改 resultType="java.util.List"因为在resultType中放的是盛放的元素,而不应该直接写容器,所以这个时候要使用自定的resultMap来写,就是上面的那种形式。
resultMap:适合使用返回值是自定义实体类的情况。
resultType:适合使用返回值得数据类型是非自定义的,即jdk的提供的类型。