定义一个实体
package com.sky.webpro.domain;
import com.alibaba.fastjson.JSONObject;
import java.util.List;
public class One2Many extends JSONObject {
private List<JSONObject> datas;
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sky.webpro.mapper.TestMapper">
<!--一对多查询-->
<resultMap id="oneToManyResult" type="com.sky.webpro.domain.One2Many">
<id column="id" property="id"/>
<result column="uname" property="uname"/>
<!-- 下面这句很重要:作用就是通过selectDeptListById查出list然后映射到属性mercSettleList中。其中,ofType为list的泛型,column为子select的入参(这里是id),select为子查询的mapperId-->
<collection property="datas" ofType="com.alibaba.fastjson.JSONObject" column="{userid=id}" select="selectDeptListById">
<id column="deptid" property="deptid"/>
<result column="dnames" property="deptname"/>
</collection>
</resultMap>
<!--先执行 一 对应的mapper接口只需声明此方法即可-->
<select id="selectUserList" resultMap="oneToManyResult">
select * from tb_user
</select>
<!--后执行 多 userid=#{userid} 保持一致即可-->
<select id="selectDeptListById" resultType="com.alibaba.fastjson.JSONObject">
select * from tb_depts where userid=#{userid}
</select>
</mapper>