在用mybatis多对多查询时报了一个错误,java.lang.UnsupportedOperationException;后来仔细看了一下日志,提示“处理结果集错误”。sql文件如下:
<resultMap id="dtoList" type="ResourceDTO" autoMapping="true">
<id column="id1" property="id" ></id>
<result column="name1" property="name" ></result>
<result column="pid1" property="pid" ></result>
<collection property="resourceList" javaType="ResourceDTO" ofType="List" autoMapping="true">
<id column="id2" property="id" ></id>
<result column="name2" property="name" ></result>
<result column="pid2" property="pid" ></result>
<collection property="resourceList" javaType="ResourceDTO" ofType="List"
autoMapping="true">
<id column="id3" property="id" ></id>
<result column="name3" property="name" ></result>
<result column="pid3" property="pid" ></result>
</collection>
</collection>
</resultMap>
<select id="selectlist" resultMap="dtoList">
SELECT
v1.id as id1,
v1.name as name1,
v1.pid as pid1,
v2.id as id2 ,
v2.name as name2,
v2.pid as pid2,
v3.id as id3,
v3.name as name3,
v3.pid as pid3
FROM
resource as v1
left join resource as v2 on v2.pid = v1.id
left join resource as v3 on v3.pid = v2.id
where v1.pid = 0
</select>
于是立马想到应该是mysql数据库的字段类型与java字段类型不匹配,于是做了如下修改:
<resultMap id="dtoList" type="ResourceDTO" autoMapping="true">
<id column="id1" property="id" jdbcType="BIGINT"></id>
<result column="name1" property="name" jdbcType="VARCHAR"></result>
<result column="pid1" property="pid" jdbcType="BIGINT"></result>
<collection property="resourceList" javaType="ResourceDTO" ofType="List" autoMapping="true">
<id column="id2" property="id" jdbcType="BIGINT"></id>
<result column="name2" property="name" jdbcType="VARCHAR"></result>
<result column="pid2" property="pid" jdbcType="BIGINT"></result>
<collection property="resourceList" javaType="ResourceDTO" ofType="List"
autoMapping="true">
<id column="id3" property="id" jdbcType="BIGINT"></id>
<result column="name3" property="name" jdbcType="VARCHAR"></result>
<result column="pid3" property="pid" jdbcType="BIGINT"></result>
</collection>
</collection>
</resultMap>
<select id="selectlist" resultMap="dtoList">
SELECT
v1.id as id1,
v1.name as name1,
v1.pid as pid1,
v2.id as id2 ,
v2.name as name2,
v2.pid as pid2,
v3.id as id3,
v3.name as name3,
v3.pid as pid3
FROM
resource as v1
left join resource as v2 on v2.pid = v1.id
left join resource as v3 on v3.pid = v2.id
where v1.pid = 0
</select>
执行代码,结果还是同样的错误,后来又仔细检查了sql,原来是把javaType 和 ofType的类型搞混了,修改之后,执行成功:
<resultMap id="dtoList" type="ResourceDTO" autoMapping="true">
<id column="id1" property="id" jdbcType="BIGINT"></id>
<result column="name1" property="name" jdbcType="VARCHAR"></result>
<result column="pid1" property="pid" jdbcType="BIGINT"></result>
<collection property="ResourceDTOList" javaType="java.util.List" ofType="ResourceDTO" autoMapping="true">
<id column="id2" property="id" jdbcType="BIGINT"></id>
<result column="name2" property="name" jdbcType="VARCHAR"></result>
<result column="pid2" property="pid" jdbcType="BIGINT"></result>
<collection property="ResourceDTOList" javaType="java.util.List" ofType="ResourceDTO"
autoMapping="true">
<id column="id3" property="id" jdbcType="BIGINT"></id>
<result column="name3" property="name" jdbcType="VARCHAR"></result>
<result column="pid3" property="pid" jdbcType="BIGINT"></result>
</collection>
</collection>
</resultMap>
<select id="selectlist" resultMap="dtoList">
SELECT
v1.id as id1,
v1.name as name1,
v1.pid as pid1,
v2.id as id2 ,
v2.name as name2,
v2.pid as pid2,
v3.id as id3,
v3.name as name3,
v3.pid as pid3
FROM
resource as v1
left join resource as v2 on v2.pid = v1.id
left join resource as v3 on v3.pid = v2.id
where v1.pid = 0
</select>