在Mybatis中实现递归查询,父类嵌套子类
xml:
这里注意
<collection column="{id=id,robotIp=robot_ip}" property="children" ofType="com.yuneasy.robort.vo.showEntity.StringTree" select="selectByAreaPidTwo2">
</collection>
column=“{prop1=col1,prop2=col2}”,这个是条件,会在selectByAreaPidTwo2方法中用到。
<resultMap id="areaListResult" type="com.yuneasy.robort.vo.showEntity.StringTree" >
<id column="key_id" property="keyId"></id>
<result column="id" property="id" jdbcType="VARCHAR" />
<result column="pid" property="pid" jdbcType="VARCHAR" />
<result column="robot_ip" property="robotIp" jdbcType="VARCHAR" />
<result column="name" property="name" jdbcType="VARCHAR" />
<collection column="{id=id,robotIp=robot_ip}" property="children" ofType="com.yuneasy.robort.vo.showEntity.StringTree" select="selectByAreaPidTwo2">
</collection>
</resultMap>
<select id="selectByAreaPidTwo2" resultMap="areaListResult" parameterType="java.util.Map">
SELECT a.id as key_id, a.area_id as id, a.area_pid as pid, a.robot_ip, b.name
from device_config a
LEFT JOIN device_config_base b on a.base_id=b.id
<where>
<if test="id != null">
a.area_pid = #{id}
</if>
<if test="robotIp != null">
and a.robot_ip = #{robotIp}
</if>
</where>
</select>
vo:
@Getter
@Setter
public class StringTree implements Serializable {
private static final long serialVersionUID = 1L;
private String keyId;
private String id;
private String pid;
private String name;
private String pointId;
private List<StringTree> children;
// private String type;
private String robotIp;
}
dao:
List<StringTree> selectByAreaPidTwo2(Map<String, String> map);
swagger测试结果类似下面这个:
{
"data": {
"list": [
{
"goodscateid": 1,
"name": "食品类",
"parentid": 0,
"description": "11111111111111111",
"displayorder": 1,
"commissionrate": null,
"enabled": 1,
"catelist": [
{
"goodscateid": 2,
"name": "生鲜类",
"parentid": 1,
"description": "222222222222222",
"displayorder": 2,
"commissionrate": null,
"enabled": 1,
"catelist": [
{
"goodscateid": 3,
"name": "海鲜类",
"parentid": 2,
"description": "3333333",
"displayorder": 3,
"commissionrate": null,
"enabled": 1,
"catelist": []
}
]
}
]
},
{
"goodscateid": 4,
"name": "服装类",
"parentid": 0,
"description": "服装生成商",
"displayorder": 2,
"commissionrate": null,
"enabled": 1,
"catelist": []
}
],
"prePage": 0,
"nextPage": 0,
"isFirstPage": true,
"isLastPage": true,
"hasPreviousPage": false,
"hasNextPage": false,
"navigatePages": 8,
"navigatepageNums": [
1
],
"navigateFirstPage": 1,
"navigateLastPage": 1,
"firstPage": 1,
"lastPage": 1
}
}