当前位置:网站首页>Stream stream is used for classification display.

Stream stream is used for classification display.

2022-07-23 14:13:00 Ozawa can't Java

1: First , First, let's look at my database .

  You can see in my database , The first level classification is parent_id yes 0, The secondary classification is parent_id It's his parent id

2: The goal is

My goal , That is to integrate the primary classification and secondary classification through flow programming , That is to put the secondary classification into the primary classification children Properties of the , And then it goes back to the front end .

 3: terms of settlement

(1) The first one is

      The idea is , Create two separate classes , One First level classification , One Secondary classification ,, And then use MP, , respectively, Get all First level classification And all Secondary classification , ad locum , Because all the fields in the table are returned in the background , Instead of the fields we want , So you need to assign values separately .

// First level classification 
@Data
@Accessors(chain = true)
public class OneSubject {
    private String id;
    private String title;

    // A primary classification has multiple secondary classifications 
    private List<TwoSubject> children=new ArrayList<>();
}




// Secondary classification 
@Data
@AllArgsConstructor
@NoArgsConstructor
public class TwoSubject {
    private String id;
    private String title;
}

Java Code implementation

@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    // Put the primary classification and secondary classification together 
    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        // Query all first level classifications  parentid=0
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id","0");
        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapperOne);
        // Query all secondary classifications  parentid!=0

        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperOne.ne("parent_id","0");
        List<EduSubject> twoSubjectList = baseMapper.selectList(wrapperTwo);
        // establish list aggregate , Used to store the final encapsulated data 
        List<OneSubject> finalSubjectList=new ArrayList<>();
        // Package level 1 Classification 
        // Find all the first level classifications list A collection of traverse , Get the classification object of each level , Get the value of each primary classification object 
        // Package to the required list In the assembly 
        for (int i = 0; i < oneSubjectList.size(); i++) {
            EduSubject eduSubject = oneSubjectList.get(i);
            OneSubject oneSubject = new OneSubject();
//            oneSubject.setId(eduSubject.getId());
//            oneSubject.setTitle(eduSubject.getTitle());
            // hold eduSubject Copy the value to the corresponding oneSubject Inside the object , The properties in the two objects are the same, and the corresponding automatic assignment 
            BeanUtils.copyProperties(eduSubject,oneSubject);

            // Loop through and query all the secondary classifications in the primary classification 
            // establish list The set encapsulates the secondary classification of each primary classification 
            List<TwoSubject> twoFinalSubjectList=new ArrayList<>();
            // Traverse the secondary classification list aggregate 
            for (int j = 0; j < twoSubjectList.size(); j++) {
                EduSubject tSubject = twoSubjectList.get(j);
                // If the secondary classification parentid And primary classification id equally , Just add it to the primary classification 
                if(tSubject.getParentId().equals(eduSubject.getId())){
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(tSubject,twoSubject);
                    twoFinalSubjectList.add(twoSubject);
                }
            }

            // Put all the secondary classifications below the first level into the first level 
            oneSubject.setChildren(twoFinalSubjectList);
            finalSubjectList.add(oneSubject);

        }

        return finalSubjectList;
    }

}

(2) The second kind

The second method uses streaming programming , That is our theme today .

Ideas : There is no need to create a type here , In the receive field returned by the database   Add a new one   Field . Create a Stream flow , And then in map Method to set its secondary classification , That is to say children.  Define a method here , It is to obtain all the secondary classifications under a primary classification , Then return to all secondary classifications , Then add it to the first level of classification to get children Properties of the .

 @TableField(exist = false)// This field does not exist in the database 
    private List<EduSubject> children;

Java Code implementation

    // Put the primary classification and secondary classification together 
    @Override
    public List<EduSubject> getAllOneTwoSubject() {

        // Query all first level classifications  parentid=0
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id", "0");
        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapperOne);

        // Query all secondary classifications  parentid!=0
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperOne.ne("parent_id", "0");
        List<EduSubject> twoSubjectList = baseMapper.selectList(wrapperTwo);

        // At this time, we have got the first level classification (oneSubjectList) And secondary classification (twoSubjectList),
        //1: Adopt flow programming to encapsulate the data in the secondary classification into the primary classification children in 

        oneSubjectList.stream()
                .map(sub ->{
                    return sub.setChildren(getChildren(sub.getId(),twoSubjectList));
                })
               .collect(Collectors.toList());
        return oneSubjectList;
    }

    // // Judge the secondary classification in the primary parent class 
    @Override
    public List<EduSubject> getChildren(String oneSubject, List<EduSubject> eduSubject) {
     return  eduSubject.stream().filter(res -> {return res.getParentId().equals(oneSubject);}).collect(Collectors.toList());
    }

原网站

版权声明
本文为[Ozawa can't Java]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207230739046849.html