一、表设计
二、entity
import com.fasterxml.jackson.annotation.JsonIgnore; import com.joyoung.cloud.security.common.entity.BaseEntity; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import lombok.experimental.Accessors; import javax.persistence.Column; import javax.persistence.Table; import javax.persistence.Transient; import java.io.Serializable; import java.math.BigDecimal; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Map; @ApiModel(value = "自助售卖产品销售区域架构表") @Table(name = "sys_sellarea") @Data @Accessors(chain = true) public class SysSellarea extends BaseEntity implements Serializable { private static final long serialVersionUID = 1L; /** * 销售区域编码 */ @Column(name = "sellarea_code") @ApiModelProperty(value = "销售区域编码") private String sellareaCode; /** * 销售区域名称 */ @Column(name = "sellarea_name") @ApiModelProperty(value = "销售区域名称") private String sellareaName; /** * 上级ID */ @Column(name = "parent_id") @ApiModelProperty(value = "上级ID") private String parentId; /** * 地址 */ @Column(name = "path") @ApiModelProperty(value = "地址") private String path; /** * 等级 */ @Column(name = "path_level") @ApiModelProperty(value = "等级") private String pathLevel; /** * 排序 */ @Column(name = "seq") @ApiModelProperty(value = "排序") private BigDecimal seq; /** * 描述 */ @Column(name = "remark") @ApiModelProperty(value = "描述") private String remark; @Transient private boolean hasChecked; @Transient private boolean hasChildren; @Transient private List<SysSellarea> children = new LinkedList<SysSellarea>(); @JsonIgnore public static List<SysSellarea> sortList(Map<String, SysSellarea> map) { List<SysSellarea> SysSellareaTree = new ArrayList<>(); map.forEach((id, obj) -> { if (map.containsKey(obj.getParentId())) { map.get(obj.getParentId()).getChildren().add(obj); } else { SysSellareaTree.add(obj); } }); return SysSellareaTree; } }
三、controller
/** * 删除销售区域 * @param id * @return */ @Override @ApiOperation(value = "删除销售区域及下面所有销售区域") @DeleteMapping("/{id}") public Result del(@PathVariable String id) { service.del(id); return ResultGenerator.genSuccessResult(); }
四、service
public interface SysSellareaService extends BaseService<SysSellarea> { void del(String id); }
五、serviceimpl
@Service @Transactional public class SysSellareaServiceImpl extends BaseServiceImpl<SysSellareaDao, SysSellarea> implements SysSellareaService { /** * 删除销售区域 * @param id */ @Override public void del(String id) { //删除该节点以及下面节点 delete(id); } private void delete(String id){ //删除该节点 try { dao.deleteSellareaById(id); List<SysSellarea> sysSellareas = dao.selectListByParentId(id); //递归删除该节点下所有节点 if (sysSellareas==null||sysSellareas.size()==0){ return; }else { for (SysSellarea sysSellarea1 : sysSellareas){ delete(sysSellarea1.getId()); } } } catch (Exception e) { e.printStackTrace(); throw new BaseSystemException(SELLAREA_DEL_FAIL.getCode(),SELLAREA_DEL_FAIL.getMsg()); } } }
六、dao
@Repository public interface SysSellareaDao extends TKMapper<SysSellarea> { /** * 根据parentId查询 * @param parentId * @return */ List<SysSellarea> selectListByParentId(String parentId); }
七、mapper
<select id="selectListByParentId" resultType="com.joyoung.cloud.security.common.entity.system.SysSellarea"> SELECT <include refid="Sellarea_param"/>, EXISTS (SELECT id FROM sys_sellarea WHERE parent_id = ssa.id AND del_flag = 0) AS hasChildren FROM sys_sellarea ssa WHERE ssa.del_flag = 0 AND ssa.parent_id = #{parentId} </select>
标签:java,String,递归,private,id,value,import,节点,name From: https://www.cnblogs.com/wffzk/p/17525921.html