有拿到一个树级结构的需求,有子集的状态下,就往下无限添加层级然后一起返回,直接用myabtis的resultMap处理会简单很多,就比方说下面这个结构
@Data
public class ZzztKhpfVo {
private String name;
private String unit;
private String year1;
private String year2;
private String year3;
private String year4;
private List<ZzztKhpfVo> zzztKhpfVoList;
}
在前面处理好业务逻辑,在dao层传入需要匹配的参数,根据自己的业务来
List<ZzztKhpfVo> getZzstpfInfoDetailNew(@Param("areaName") String areaName, @Param("childAreaName") String childAreaName, @Param("code") String code);
接下去在xml中定义一个resultMap,
column为你想要往子集中传递的一个参数,例如你需要一个父级的id作为子集的pid去查找则为{id=id},在子集的sql中就可以写上pid=#{id},就等于完成了一个id的传递
property为实体类中子集的参数名,select为你需要往子集中做的一个sql操作,你也可以使用dao层的同一个sql完成递归,也可以自定义一个新的子集sql
<resultMap id="zzstKhpfBg" type="org.jeecg.modules.basis.childscenario.zzst.vo.ZzztKhpfVo">
<collection column="{code=code,unit2=unit2,indicatorsCode=indicatorsCode,areaName=areaName}" property="zzztKhpfVoList" select="getLevelTwoList"
ofType="org.jeecg.modules.basis.childscenario.zzst.vo.ZzztKhpfVo"/>
</resultMap>
最后写自己需要的sql语句,我就随便写个sql好了,例子是我写的父级sql,如果想要往下面的子集sql中传递dao层的参数,可以使用#{code} as code,
然后在resultMap的中column定义{code=code},那么子集中直接可以使用#{code}来做一个参数,其他的基本上也就没什么问题了,逻辑都大致一样
<select id="getZzstpfInfoDetailNew" resultMap="zzstKhpfBg" parameterType="String">
SELECT #{childAreaName} as unit2,
azd.indicators_code as indicatorsCode,
azd.indicators_name as indicatorsName,
round(avg(azd.year1), 2) year1,
round(avg(azd.year2), 2) year2,
round(avg(azd.year3), 2) year3,
round(avg(azd.year4), 2) year4,
#{code} as code,
#{areaName} as areaName,
azd.indicators_score indicatorsScore
FROM ads_zzst_detail AS azd
LEFT JOIN ads_zzst_index AS azi
ON azd.UNIT_TYPEID = azi.UNIT_TYPEID
where azi.UNIT_TYPENAME = #{areaName}
<if test="code != null and code != ''">
and azi.unit_typeid like CONCAT(#{code},'%')
</if>
<if test="childAreaName != null and childAreaName != ''">
and azi.unit = #{childAreaName}
</if>
and length(indicators_code) = 3
group by unit2,indicators_code,indicators_name,code,areaName,indicatorsScore;
</select>
标签:code,String,azd,collection,添加,子集,private,sql,循环 From: https://www.cnblogs.com/Crush123/p/17152892.html