今天在寫一個專案的時候遇到了一個問題:如何寫三級分類的介面?
經過多方查詢資料 我解決了該問題
前端頁面如下:

資料庫:

在物體類中添加children
package com.example.humanresources.entity;
import java.util.Date;
import java.io.Serializable;
import java.util.List;
import lombok.Data;
@Data
public class BizProductCategory implements Serializable {
private static final long serialVersionUID = 892831452437776529L;
/**
* 類別id
*/
private Long id;
/**
* 類別名稱
*/
private String name;
/**
* 備注
*/
private String remark;
/**
* 排序
*/
private Integer sort;
private Date createTime;
private Date modifiedTime;
/**
* 父級分類id
*/
private Long pid;
// @TableField
private List<BizProductCategory> children;
}
在mybatis-plus中需要用到@TableField注解
由于我直接用的mybatis 所以直接將children添加為屬性即可
因為只對一級分類進行分頁,所以我又寫了一個物體類:
package com.example.humanresources.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class BizProductCategoryPlus {
private BizProductCategory bizProductCategory;
private List<BizProductCategory> rows;
private int total;
}
service層代碼:
@Override
public BizProductCategoryPlus listWithTree(int pageNum, int pageSize) {
// 1.查出所有分類
List<BizProductCategory> bizProductCategories = bizProductCategoryDao.selectAll();
// 2.組裝成父子樹形結構
// 2.1找出所有的一級分類
List<BizProductCategory> level1Menus = bizProductCategories.stream().filter(bizProductCategory ->
bizProductCategory.getPid() == 0
).map((menu)->{
menu.setChildren(getChildrens(menu,bizProductCategories));
return menu;
}).sorted((menu1,menu2)->{
// 選單的排序
return (menu1.getSort()==null?0:menu1.getSort())-(menu2.getSort()==null?0:menu2.getSort());
}).collect(Collectors.toList());
// List<BizProductCategory> level1Menus = bizProductCategories.stream().filter((bizProductCategory) -> {
// return bizProductCategory.getPid() == 0;
// }).collect(Collectors.toList());
List<BizProductCategory> page = new ArrayList<>();
int start = (pageNum-1)*pageSize;
for (int i = start; i < (start+pageSize > level1Menus.size() ? level1Menus.size() : start+pageSize); i++) {
page.add(level1Menus.get(i));
}
BizProductCategoryPlus bizProductCategoryPlus=new BizProductCategoryPlus();
bizProductCategoryPlus.setRows(page);
bizProductCategoryPlus.setTotal(level1Menus.size());
return bizProductCategoryPlus;
}
上面呼叫的遞回方法:
// 遞回查找所有選單的子選單
// root為當前選單 all是所有選單
private List<BizProductCategory> getChildrens(BizProductCategory root, List<BizProductCategory> all){
List<BizProductCategory> children = all.stream().filter(bizProductCategory -> {
return bizProductCategory.getPid() == root.getId();
}).map(bizProductCategory->{
// 找到子選單
bizProductCategory.setChildren(getChildrens(bizProductCategory,all));
return bizProductCategory;
}).sorted((menu1,menu2)->{
// 選單的排序
return (menu1.getSort()==null?0:menu1.getSort())-(menu2.getSort()==null?0:menu2.getSort());
}).collect(Collectors.toList());
return children;
}
controller層:
/**
* 分類樹形結構
* @param pageNum
* @param pageSize
* @return
* @author lt
*/
@GetMapping("categoryTree")
public R findAll(int pageNum,int pageSize){
BizProductCategoryPlus bizProductCategoryPlus = bizProductCategoryService.listWithTree(pageNum, pageSize);
// BizProductCategoryPlus bizProductCategoryPlus=new BizProductCategoryPlus();
return R.ok().setData(bizProductCategoryPlus);
}
這樣就實作了對一級分類的分頁查詢
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/377150.html
標籤:其他
