1.映射
<!--app返回结果封装--> <resultMap id="AppMap" type="java.util.HashMap"> <!--不加这个映射 返回无id--> <id column="id" jdbcType="BIGINT" property="id" /> <collection property="childsList" column="id" select="getChildrenModues"> </collection> </resultMap>
2.sql
<select id="commentList" parameterType="java.lang.Long" resultMap="AppMap"> SELECT a.`id` as id,a.`username` AS username,a.`create_time` AS createTime,a.`goods_rank` AS goodsRank, a.`content`,a.`imgs` FROM t_comment a WHERE a.goods_id=#{goodsId} </select> <select id="getChildrenModues" parameterType="java.lang.Long" resultType="java.util.HashMap"> SELECT b.`id` as id,b.create_time AS createTime,b.`username` AS username,b.content AS content,b.imgs AS imgs FROM t_comment b WHERE b.p_id=#{pId} </select>
3.结果
"success":true,
"data":[
{
"imgs":"/2018/06/22/d681967ec2b8464e99206642b11a340a.jpg",
"createTime":"2018-06-25 01:56:52",
"childsList":[
{
"imgs":"b.jpg",
"createTime":"2018-06-22 03:09:29",
"id":2,
"content":"哪里不好",
"username":"商家"
},
{
"imgs":"c.jpg",
"createTime":"2018-06-22 03:09:30",
"id":3,
"content":"你看",
"username":"小明"
},
{
"imgs":"d.jpg",
"createTime":"2018-06-22 03:09:33",
"id":4,
"content":"哦",
"username":"商家"
}
],
"id":1,
"goodsRank":5,
"content":"商品不好",
"username":"小明"
},
{
"imgs":"/2018/06/22/d681967ec2b8464e99206642b11a340a2.jpg",
"createTime":"2018-06-25 01:56:52",
"childsList":[
],
"id":40,
"goodsRank":5,
"content":"商品不好2",
"username":"小明2"
}
],
"message":"success"
}