Java递归生成树形菜单结构

it2024-08-13  39

生成菜单如图所示

创建数据库
CREATE TABLE organization (organizationID VARCHAR(10),organizationName VARCHAR(10),parentID VARCHAR(10)); INSERT INTO organization VALUES('1' , 'school' , '0'); INSERT INTO organization VALUES('2.1' , 'grade1' , '1'); INSERT INTO organization VALUES('2.1.1' , 'class1' , '2.1'); INSERT INTO organization VALUES('2.2' , 'grade2' , '0'); INSERT INTO organization VALUES('2.3' , 'grade3' , '0'); INSERT INTO organization VALUES('2.1.2' , 'class2' , '2.1'); INSERT INTO organization VALUES('2.2.1' , 'class-1' , '2.2'); INSERT INTO organization VALUES('2.3.1' , 'class.1' , '2.3'); INSERT INTO organization VALUES('2.3.2' , 'cladd.2' , '2.3');
sql映射
<?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> <!--列表查询--> <select id="sysOrgGetAll"> SELECT organizationID, organizationName, parentID FROM sys_organization where state = 1 </select> </mapper>
创建实体类
/** * * 组织机构实体类 * */ public class SysOrg { private String organizationID; //主键 private String organizationName; //组织机构名称 private String parentID; //上层组织机构 private List<SysOrg> children; //子集 public String getOrganizationID() { return organizationID; } public void setOrganizationID(String organizationID) { this.organizationID = organizationID; } public String getOrganizationName() { return organizationName; } public void setOrganizationName(String organizationName) { this.organizationName = organizationName; } public String getParentID() { return parentID; } public void setParentID(String parentID) { this.parentID = parentID; } public List<SysOrg> getChildren() { return children; } public void setChildren(List<SysOrg> children) { this.children = children; } }
定义mapper
@Mapper public interface SysOrgMapper { //列表查询 public List<SysOrg> sysOrgGetAll(SysOrg sysOrg); }
控制层实现
public class SysOrgController extends BaseController{ @Autowired private SysOrgService sysOrganization; /** * 列表查询 */ @PostMapping(value = "/getsysorg") public ResultRsp getSysOrg(SysOrg sysorg){ return sysOrganization.getSysOrg(sysorg); } }
定义service业务层实现
public ResultTRsp getSysOrg(SysOrg sysorg){ //1.查出所有条目,放在一个集合sysAllOrgList中 List<SysOrg> sysAllOrgList = sysOrgMapper.sysOrgGetAll(sysorg); //2. 在集合sysAllOrgList里面找出parentId是0的,放在一个集合2里 //方法1 Lambda 表达式写法 //List<SysOrg> getParentOryList = sysAllOrgList.stream().filter(a-> "0".equals(a.getParentID())).collect(Collectors.toList()); //方法2 for循环方式 List<SysOrg> getParentOryList = sysOrgGetParent(sysAllOrgList); //3. 把集合1和集合2传入方法 List<SysOrg> res = getSysOrgChild(sysAllOrgList,getParentOryList); } public List<SysOrg> sysOrgGetParent(List<SysOrg> sysAllOrglist){ List<SysOrg> parentList = new ArrayList<SysOrg>(); for(int i = 0; i < sysAllOrglist.size(); i++) { if ("0".equals(sysAllOrglist.get(i).getParentID())) { parentList.add(sysAllOrglist.get(i)); } } return parentList; } //递归查找子菜单 public List<SysOrg> getSysOrgChildren(List<SysOrg> sysOrgList, List<SysOrg> allParentList) { //1 循环最顶级组织机构 for(int i = 0 ;i < allParentList.size() ; i++){ //获取全部组织机构 SysOrg sysParentOrg = allParentList.get(i); //保存子集 List<SysOrg> childrenList = new ArrayList(); //2 循环所有的组织机构 for(int j = 0 ;j < sysOrgList.size() ; j++){ //按顺序获取所有组织机构里面的数据 SysOrg sysChildrenOrg = sysOrgList.get(j); //如果一级的ID是其他组织机构的父级ID的话,那么这条数据就是一级的子集 if(sysParentOrg.getOrganizationID().equals(sysChildrenOrg.getParentID())){ childrenList.add(sysChildrenOrg); } } if(null != childrenList && childrenList.size() > 0){ allParentList.get(i).setChidren(getSysOrgChildren(sysOrgList,childrenList)); } allParentList.get(i).setChidren(childrenList); return allParentList; } }
最新回复(0)