昨天 、一个好久没联系的小学弟找到我,说是最近做项目的时候遇到了一个难题,希望我能帮帮他们。本来想草草的推脱、这……我一听是难题,很快啊,我直接来了兴趣……
平常最大的喜好就是被难题摁在地上摩擦的我,瞬间这嘴角根本就控制不住的上扬
小学弟:老哥…控制一下……控制一下…
我:咳咳……你且把这难题说给我听听
小学弟:最近做项目的时候,需求是要把分类的结构以树形的样式显示在页面上
我:树形?!嗯……用一下循环嵌套,把二级分类列表嵌套进与其对应的一级分类里面
小学弟:嗯………
我:害、看老哥给你操作一波
小学弟:geigie~ 你好勇喔~
其实以树形的样式显示在页面,这个需求并不是很难,简单来说就是将一对多的逻辑关系再加上循环嵌套的问题,复杂的说,就是将一对多的逻辑关系……
首先,拿到需求先对其进行逻辑分析,分类以树形 ?那么问题来了,何为树形?前端的树形样式排版、拿到后端,用Java如何实现其中的业务逻辑关系 ?来画个图分析一下:
根据平时对分类等的业务逻辑进行分析,首先这二者是一对多的关系,在一级分类中添加一个盛装二级分类的集合,可以达到一级分类中可以有多个同属性的二级分类,然后在其基础上在套上一层循环,基本上就可以达成树形的结果。理论结束,接下来用代码实现:
第一步:先创建实体类
public class Category implements Serializable {
private static final long serialVersionUID = 1L;
//分类id
private String id;
//分类名称
private String title;
//父ID 一级分类的父ID为:0 , 二级分类父ID = 一级分类的id值
private String parentId;
.......get、set 方法 省略.......
}
为了分析简单,分别再创建出一级和二级各自的实体类
public class OneCategory {
private String id;
private String title;
//一级分类中有多个二级分类
private List<TwoCategory> children = new ArrayList<>();
get set 方法省略........
}
public class TwoCategory{
private String id;
private String title;
get set 方法省略........
}
第二步:编写Controller控制层
//分类列表(树形)
@GetMapping("/getAllSubject")
public R getAllSubject(){
//List集合中的泛型,因为在一级分类中含有一级分类和二级分类
List<OneCategory> list = subjectService.getAllOneTwoSubject();
return R.ok().data("list",list);
}
第三步:编写具体的业务逻辑(为了方便这里选择使用Mybatis-plus)
@Override
public List<OneCategory> getAllOneTwoSubject() {
//查询出所有的一级分类 parent_id = 0
QueryWrapper<Category> oneWrapper = new QueryWrapper<>();
oneWrapper.eq("parent_id","0");
List<Category> oneCategoryList = baseMapper.selectList(oneWrapper);
//查询出所有的二级分类 parent_id != 0
QueryWrapper<Category> twoWrapper = new QueryWrapper<>();
twoWrapper.ne("parent_id","0");
List<Category> twoCategoryList = baseMapper.selectList(twoWrapper);
//创建一个封装一级分类的集合
List<OneCategory> finalCategoryList = new ArrayList<>();
//封装所有的一级分类
//将查询出来的所有一级分类进行遍历,得到每一个一级分类的对象
//封装到要求的List集合中
for (int i = 0; i < oneCategoryList .size(); i++) {
//得到遍历出来的每一个一级分类的值
Category oCategory= oneSubjectList.get(i);
//最终是要将所有的遍历的一级分类的值填充到OneSubject中
OneCategory oneCategory = new OneCategory ();
//oCategory值复制到对应的oneCategory 对象里面
//对于BeanUtils.copyProperties()下面会具体介绍
BeanUtils.copyProperties(oCategory,oneCategory);
finalCategoryList.add(oneCategory);
//创建一个盛装二级分类的集合
List<TwoCategory> twoFinalCateList = new ArrayList<>();
//封装所有的二级分类
//将所有的二级分类遍历出来,得到的每一个二级分类的值封装到要求的List中
for (int m = 0; m < twoCategoryList.size(); m++) {
//遍历获取每一个二级分类
Category tCategory = twoCategoryList.get(m);
//判断二级分类的parentId是否与一级分类的id相等
if (tCategory .getParentId().equals(oneSubject.getId())){
//把tSubject中的值复制到twoSubject中去,然后在放到twoFinalSubject中
TwoSubject twoCategory = new TwoSubject();
BeanUtils.copyProperties(tCategory,twoCategory );
twoFinalSubjectList.add(twoSubject);
}
}
//最后再把相应一级分类下所有的二级分类放到一级分类中
OneCategory.setChildren(twoFinalCategoryList);
}
return finalCategoryList;
}
以上就是如何以树形样式显示分类的业务逻辑代码!接下来介绍一下BeanUtils.copyProperties()这个方法,简单来说:就是一个get+set的过程;复杂点说:直接点进去看源码——>
private static void copyProperties(Object source, Object target, Class<?> editable, String... ignoreProperties) throws BeansException {
Assert.notNull(source, "Source must not be null");
Assert.notNull(target, "Target must not be null");
//获取目标类型对象
Class<?> actualEditable = target.getClass();
if (editable != null) { //null
if (!editable.isInstance(target)) {
throw new IllegalArgumentException("Target class [" + target.getClass().getName() + "] not assignable to Editable class [" + editable.getName() + "]");
}
actualEditable = editable;
}
//获取目标对象所有字段属性
PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);
//需要过滤的字段
List<String> ignoreList = ignoreProperties != null ? Arrays.asList(ignoreProperties) : null;
PropertyDescriptor[] var7 = targetPds;
int var8 = targetPds.length;
for(int var9 = 0; var9 < var8; ++var9) {
//获取目标字段
PropertyDescriptor targetPd = var7[var9];
//获取set方法
Method writeMethod = targetPd.getWriteMethod();
//set方法不为空,过滤字段为空或过滤字段不包括当前遍历字段
if (writeMethod != null && (ignoreList == null || !ignoreList.contains(targetPd.getName()))) {
//获取源字段
PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName());
if (sourcePd != null) {
//获取get方法
Method readMethod = sourcePd.getReadMethod();
//ClassUtils.isAssignable判断是否可转(目标类型是否为源类型父类,或相同)
if (readMethod != null && ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) {
try {
//判断是否为public
if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
//打破封装
readMethod.setAccessible(true);
}
//value赋值
Object value = readMethod.invoke(source);
if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
writeMethod.setAccessible(true);
}
//值拷贝
writeMethod.invoke(target, value);
} catch (Throwable var15) {
throw new FatalBeanException("Could not copy property '" + targetPd.getName() + "' from source to target", var15);
}
}
}
}
}
完结!
我:小弟弟,懂了不
小学弟:懂了懂了!!geigei~ 你好帅啊~
我 :害