树形结构
行吧行吧,又做回JAVA了
在springboot中,撰写接口,返回树形图无限分级
一、为了最快做出来,直接三级固定死,在MySql中写死
1.1 在entity中建立子类存放
package *.dataresource.entity.dataobject; @Data @Getter @Setter @AllArgsConstructor @NoArgsConstructor public class MasterBusiness extends BaseDO implements Serializable {//实现树状结构 private List<MasterBusiness> children; }
1.2在dal中建立数据库接口
package *.dataresource.dal; @Repository public interface IMasterBusinessRepository {//树状查询 List<MasterBusiness> selectTree(); }
1.3在mapping中配置映射
<resultMap id="TreeResultMap" type="*.dataresource.entity.dataobject.MasterBusiness"> <id column="businessId1" property="businessId" jdbcType="INTEGER"/> <result column="businessCode1" property="businessCode" jdbcType="VARCHAR"/> <result column="businessName1" property="businessName" jdbcType="VARCHAR"/> <result column="businessLevel1" property="businessLevel" jdbcType="VARCHAR"/> <result column="parentId1" property="parentId" jdbcType="VARCHAR"/> <result column="sort1" property="sort" jdbcType="VARCHAR"/> <result column="explain1" property="explain" jdbcType="VARCHAR"/> <result column="status1" property="status" jdbcType="INTEGER"/> <result column="createUser1" property="createUser" jdbcType="VARCHAR"/> <result column="createTime1" property="createTime" jdbcType="TIMESTAMP"/> <result column="modifyUser1" property="modifyUser" jdbcType="VARCHAR"/> <result column="modifyTime1" property="modifyTime" jdbcType="TIMESTAMP"/> <collection property="children" ofType="*.dataresource.entity.dataobject.MasterBusiness"> <id column="businessId2" property="businessId" jdbcType="INTEGER"/> <result column="businessCode2" property="businessCode" jdbcType="VARCHAR"/> <result column="businessName2" property="businessName" jdbcType="VARCHAR"/> <result column="businessLevel2" property="businessLevel" jdbcType="VARCHAR"/> <result column="parentId2" property="parentId" jdbcType="VARCHAR"/> <result column="sort2" property="sort" jdbcType="VARCHAR"/> <result column="explain2" property="explain" jdbcType="VARCHAR"/> <result column="status2" property="status" jdbcType="INTEGER"/> <result column="createUser2" property="createUser" jdbcType="VARCHAR"/> <result column="createTime2" property="createTime" jdbcType="TIMESTAMP"/> <result column="modifyUser2" property="modifyUser" jdbcType="VARCHAR"/> <result column="modifyTime2" property="modifyTime" jdbcType="TIMESTAMP"/> <collection property="children" ofType="*.dataresource.entity.dataobject.MasterBusiness"> <id column="businessId3" property="businessId" jdbcType="INTEGER"/> <result column="businessCode3" property="businessCode" jdbcType="VARCHAR"/> <result column="businessName3" property="businessName" jdbcType="VARCHAR"/> <result column="businessLevel3" property="businessLevel" jdbcType="VARCHAR"/> <result column="parentId3" property="parentId" jdbcType="VARCHAR"/> <result column="sort3" property="sort" jdbcType="VARCHAR"/> <result column="explain3" property="explain" jdbcType="VARCHAR"/> <result column="status3" property="status" jdbcType="INTEGER"/> <result column="createUser3" property="createUser" jdbcType="VARCHAR"/> <result column="createTime3" property="createTime" jdbcType="TIMESTAMP"/> <result column="modifyUser3" property="modifyUser" jdbcType="VARCHAR"/> <result column="modifyTime3" property="modifyTime" jdbcType="TIMESTAMP"/> </collection> </collection> </resultMap>
<select id="selectTree" resultMap="TreeResultMap"> SELECT c1.businessId 'businessId1', c1.businessCode 'businessCode1', c1.businessName 'businessName1', c1.businessLevel 'businessLevel1', c1.parentId 'parentId1', c1.sort 'sort1', c1.`explain` 'explain1', c1.`status` 'status1', c1.createUser 'createUser1', c1.createTime 'createTime1', c1.modifyUser 'modifyUser1', c1.modifyTime 'modifyTime1', c2.businessId 'businessId2', c2.businessCode 'businessCode2', c2.businessName 'businessName2', c2.businessLevel 'businessLevel2', c2.parentId 'parentId2', c2.sort 'sort2', c2.`explain` 'explain2', c2.`status` 'status2', c2.createUser 'createUser2', c2.createTime 'createTime2', c2.modifyUser 'modifyUser2', c2.modifyTime 'modifyTime2', c3.businessId 'businessId3', c3.businessCode 'businessCode3', c3.businessName 'businessName3', c3.businessLevel 'businessLevel3', c3.parentId 'parentId3', c3.sort 'sort3', c3.`explain` 'explain3', c3.`status` 'status', c3.createUser 'createUser3', c3.createTime 'createTime3', c3.modifyUser 'modifyUser3', c3.modifyTime 'modifyTime3' FROM ( SELECT * FROM master_business WHERE `status` = 1 ) c1 LEFT JOIN ( SELECT * FROM master_business WHERE `status` = 1 ) c2 ON c2.parentId = c1.businessId LEFT JOIN ( SELECT * FROM master_business WHERE `status` = 1 ) c3 ON c3.parentId = c2.businessId WHERE c1.businessLevel = 1 </select>
1.4在bll中建立业务接口
package *.dataresource.bll; public interface IMasterBusinessService {//查询树 List<MasterBusiness> selectTree(); }
1.5在impl实现业务接口
package *.dataresource.bll.impl; @Service public class MasterBusinessService extends BaseService implements IMasterBusinessService { @Autowired private IMasterBusinessRepository masterBusinessRepository; //查询树 @Override public List<MasterBusiness> selectTree() { return masterBusinessRepository.selectTree(); }
}
1.6在controller中获取路由与设置数据
package *.dataresource.controller; @RestController @RequestMapping("masterBusiness") public class MasterBusinessController extends BaseController { @Autowired private IMasterBusinessService masterBusinessService; //查询树 @GetMapping("/selectAll") public List<MasterBusiness> selectMasterBusiness()throws Exception{ return masterBusinessService.selectTree(); }
}
1.7路由访问:
{{host}}/masterBusiness/selectAll
二、后续更新无限级分类
2.1 entity不变
2.2 不变
2.3 直接select所有列表
2.4 业务接口不变
2.5 业务实现使用递归
//树状结构递归查询全部 @Override public List<MasterBusiness> selectTree() { //查询总集 List<MasterBusiness> list = masterBusinessRepository.select(new MasterBusiness()); if (list==null || list.size()<=0){ return null; } LinkedList<MasterBusiness> linkedList = new LinkedList<>(); //筛选出父级 list.forEach(data->{ if(Integer.parseInt(data.getParentId()) == 0){ linkedList.add(data); } }); //对每个父级进行遍历 linkedList.forEach(data->{ data.setChildren(this.getChildren(data.getBusinessId(), list)); }); return linkedList; } //递归获取children节点 private List<MasterBusiness> getChildren(Integer parentId,List<MasterBusiness> list) { //子集集合 LinkedList<MasterBusiness> linkedList = new LinkedList<>(); list.forEach(data->{ if (parentId == Integer.parseInt(data.getParentId())) { linkedList.add(data); } }); // 递归退出条件 if (linkedList.size() == 0){ return null; } // 把子集的子集再递归循环一遍 linkedList.forEach(data->{ data.setChildren(getChildren(data.getBusinessId(),list)); }); return linkedList; }
2.6 直接在controller中获取列表
2.7 并在前台路由中访问
一懒众衫小