This is the 12th day of my participation in Gwen Challenge

First, take a look at the table structure.

Let’s say our content has

And that’s what’s shown here

  • The back-end language
    • java
      • spring
    • python
  • Operational technology
    • linux
    • docker

Next we based on SpringBoot + Mybatis to complete this amateur.

I’ll show you both XML and Java code.

Entity class

We are going to encapsulate it as the entity Vo with the next level

LevelCatalogVo> add a List

property to LevelCatalog

So let’s go ahead and write this APi

We went to the Service layer to write the concrete logic implementation.

This is the main method: query all the contents, and then through the code we encapsulate them into objects, so that we can query MySQL only once.

public List<LevelCatalogVo> info(a) {
    // Retrieve all contents
    List<LevelCatalog> levelCatalogs = baseMapper.selectList(null);
    // Create a VO display collection object to return
    List<LevelCatalogVo> levelCatalogVos = new ArrayList<>();
    // All content is traversed
    for (LevelCatalog levelCatalog : levelCatalogs) {
        // Find the first class, assign to the vo class, add to the set
        if (levelCatalog.getParentId() == 0){
            LevelCatalogVo levelCatalogVo = new LevelCatalogVo();
            BeanUtils.copyProperties(levelCatalog,levelCatalogVo);
            // Set subcategories for the first level categorylevelCatalogVo.setChildren(getChildrens(levelCatalogVo,levelCatalogs)); levelCatalogVos.add(levelCatalogVo); }}return levelCatalogVos;
}
Copy the code
private List<LevelCatalogVo> getChildrens(LevelCatalogVo root, List<LevelCatalog> levelCatalogs) {
    // Since there may be subcategories under subcategories, we still need to create a VO display collection object to return
    List<LevelCatalogVo> levelCatalogVos = new ArrayList<>();
    // Continue to iterate over everything
    for (LevelCatalog levelCatalog : levelCatalogs) {
        // Take the corresponding subclass, add it to the collection and return it
        if (root.getId().equals(levelCatalog.getParentId())){
            LevelCatalogVo levelCatalogVo = new LevelCatalogVo();
            BeanUtils.copyProperties(levelCatalog,levelCatalogVo);
            // Use recursion to step by step set up the classification of each size level
            levelCatalogVo.setChildren(getChildrens(levelCatalogVo,levelCatalogs));
            // Always join the setlevelCatalogVos.add(levelCatalogVo); }}return levelCatalogVos;
}
Copy the code

After completing the Controller layer call, the access results in exactly what we need.


Mybatis XML method

This is much simpler, Mybatis will help us do everything, but its query to Mysql is multiple times, rather than a check, so the performance will be greatly reduced in the case of a large amount of data.

And the answer is exactly the same as above.

<resultMap id="result" type="com.xn2001.entity.LevelCatalogVo">
    <id column="id" property="id"/>
    <result column="name" property="name"/>
    <collection property="children" column="id"
                ofType="com.xn2001.entity.LevelCatalogVo"
                select="info2"/>
</resultMap>

<select id="info2" resultMap="result">
    select id, name, parent_id from level_catalog where parent_id = #{parentId}
</select>
Copy the code