在用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>