mybatis使用fastjson接受一對多查詢

定義一個實體

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>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章