有个需求,就是在mybatis中获取2个不同类型的结果集,使用一个对象接收时,会报错:
Expected one result (or null) to be returned by selectOne()
查找网上是说:查询方法中得到两个两个结果集,然而你的返回用了一个实体去接收,程序这个时候就不知道如何去分配了,很果断的报这个错
后面解决的方案就是:
xml:
<select id="getTest" statementType="CALLABLE" resultMap="BaseOneMap,BaseTwoMap"
parameterType="map">
<![CDATA[
{ CALL xxx_xxx_doGetTest(
#{@p_ID,mode=IN,jdbcType=VARCHAR},
#{@p_NO,mode=IN,jdbcType=VARCHAR},
#{@p_Flag1,jdbcType=VARCHAR,mode=IN},
<span style="white-space:pre"> </span> #{@p_Flag2,mode=IN,jdbcType=VARCHAR}
)}
]]>
</select>
其中:
<resultMap type="com.fox.bean.TestTwoBean" id="BaseOneMap">
<result column="result1" jdbcType="VARCHAR" property="result1" />
</resultMap>
<resultMap type="com.fox.bean.TestTwoBean" id="BaseTwoMap">
<result column="result2" jdbcType="VARCHAR" property="result2" />
</resultMap>
Mapper:使用List<List<?>> 接收就好了:
public interface TestMapper {
public List<List<?>> getTest(@Param("@p_ID") String userName,
@Param("@p_NO") String banbie,
@Param("@p_Flag1") String flag1, @Param("@p_Flag2") String flag2);
}
最后在service里获取数据作转换:
List<List<?>> map = testMapper.getTest(userName, banbie, flag1, flag2);
Map<String, List<?>> maps = new HashMap<String, List<?>>();
if (map!=null && map.size() > 1) {//结果为2个结果集
maps.put("oneList", (List<TestOneBean>) map.get(0));
maps.put("twoList", (List<TestTwoBean>) map.get(1));
}
return maps;
最后调试OK啦~~~~~好久都没有写服务端的代码了,努力~!!
<resultMap type="com.fox.bean.TestTwoBean" id="BaseTwoMap">
<result column="result" jdbcType="VARCHAR" property="result" />
</resultMap>