Use mybatis-collection cascade function for recursive query

Table structure (three tables)

Car brand table

 

The car model table parent is the primary key of the car brand table

The parent of the car model table is the primary key id of the car model table

 

The business layer is not written, mainly the xml layer

Entity class accepts the format one level set one level  

 

public class ZqCarBrand implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * Car brand id
     */
    private Long id;

    /**
     * Car brand name
     */
    private String name;

    /**
     * Car brand letter
     */
    private String brandLetter;

    /**
     * 车型list
     */
    private List<ZqCarModel> children;

}

 

 

 

public class ZqCarModel implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * Model primary key id
     */
    private Long id;

    /**
     * Car brand id
     */
    private Long parentId;

    /**
     * model_name
     */
    private String name;

    /**
     * Car year list
     */
    private List<ZqCarYear> children;

}

 

 

public class ZqCarYear implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * Car year id
     */
    private Long id;

    /**
     * Car model id
     */
    private Long parentId;

    /**
     * Car model name
     */
    private String name;

}

 

The following are the main ones:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.zq.mapper.ZqCarBrandMapper">
   <!-- 汽车年份实体 -->
    <resultMap id="CarYearResultMap" type="com.ruoyi.zq.domain.base.ZqCarYear" >
                <result column="year_id" property="id" />
                <result column="model_id" property="parentId" />
                <result column="name" property="name" />
    </resultMap>
    
     <!-- 汽车型号 -->
    <resultMap id="CarModelResultMap" type="com.ruoyi.zq.domain.base.ZqCarModel" >
                <result column="model_id" property="id" />
                <result column="brand_id" property="parentId" />
                <result column="model_name" property="name" />
                <collection property="children" ofType="CarYearResultMap" javaType="java.util.List" column="model_id" select="queryCarYearListByModelId">
                
                </collection>
    </resultMap>
    
    <!-- 汽车品牌 -->
    <resultMap id="CarBrandtResultMap" type="com.ruoyi.zq.domain.base.ZqCarBrand" >
                <result column="brand_id" property="id" />
                <result column="brand_name" property="name" />
                <result column="brand_letter" property="brandLetter" />
                
                <collection property="children" ofType="CarModelResultMap" javaType="java.util.List"  column="brand_id" select="queryCarModelByBrandId">
               
                </collection>
                
    </resultMap>
    
    
 
    <!-- 查询汽车品牌实体 -->
    <select id="selectCarList"  resultMap="CarBrandtResultMap">
        select * from zq_car_brand where is_del=0 order by brand_letter asc
    </select>
    
    <!--通过品牌id查询对应的车型 -->
    <select id="queryCarModelByBrandId" parameterType="java.lang.Long" resultMap="CarModelResultMap">
       select * from zq_car_model where is_del=0 and brand_id=#{brandId}
    </select>
    
     <!--Query the corresponding year by model id-->
    <select id="queryCarYearListByModelId" parameterType="java.lang.Long" resultMap="CarYearResultMap">
       select year_id,model_id,CONCAT_WS('款',car_year,name) as name from zq_car_year where is_del=0 and model_id=#{modelId} order by car_year desc
    </select>
</mapper>

 

The format of the query is:

{     "id": 2,     "name": "ARCFOX"     "brandLetter": "A",     "children": [{         "id": 3,         "name": "ARCFOX LITE",         "parentId": 2         "children": [{             "id": 7,             "name": "2019款R300 原力版",             "parentId": 3         }, {             "id": 8,             "name": "2019款R300 引力版",             "parentId": 3         }, {             "id": 9,             "name": "2019 R300 Magic Edition",             "name": "2017 Original Force Edition",             "id": 10,         }, {             "parentId": 3






















            "parentId": 3
        }, {
            "id": 11,
            "name": "2017款引力版",
            "parentId": 3
        }],
    }, {
        "id": 4,
        "name": "ARCFOX αT",
        "parentId": 2
        "children": [{
            "id": 12,
            "name": "2021款480S",
            "parentId": 4
        }, {
            "id": 13,
            "name": "2021款480S+",
            "parentId": 4
        }, {
            "id": 14,
            "name": "2021款653S",
            "parentId": 4
        }, {
            "id": 15,
            "name": "2021款653S+",
            "parentId": 4
        }, {
            "id": 16,
            "name": "2021款四驱性能版 H",
            "parentId": 4
        }],
    }],
}

 

2. If you need to query that the three-level linkage is in the same table, the same 

The entity is the same as the upper level

 <!-- 区实体 -->
    <resultMap id="areaResultMap" type="com.ruoyi.zq.domain.base.District" >
                <result column="area_id" property="id" />
                <result column="pid" property="parentId" />
                <result column="name" property="name" />
    </resultMap>
    
     <!-- 市实体 -->
    <resultMap id="cityModelResultMap" type="com.ruoyi.zq.domain.base.City" >
                <result column="area_id" property="id" />
                <result column="pid" property="parentId" />
                <result column="name" property="name" />
                <collection property="children" ofType="areaResultMap" javaType="java.util.List" column="area_id" select="queryAreaListByPid">
                
                </collection>
    </resultMap>
    
    <!-- 省实体 -->
    <resultMap id="provinceResultMap" type="com.ruoyi.zq.domain.base.Province" >
                <result column="area_id" property="id" />
                <result column="name" property="name" />
                
                <collection property="children" ofType="cityModelResultMap" javaType="java.util.List"  column="area_id" select="queryCityListByPid">
               
                </collection>
                
    </resultMap>
    
    
 
    <!-- 查询省列表 -->
    <select id="selectProvincesList"  resultMap="provinceResultMap">     <!--Query the corresponding city by province id-->     </select>
        select area_id,name from zq_area where level=1

    

    <select id="queryCityListByPid" parameterType="java.lang.Long" resultMap="cityModelResultMap">
       select area_id,pid,name from zq_area where level=2 and pid=#{pid}
    </select>
    
    <!--通过市id查询对应的区域 -->
    <select id="queryAreaListByPid" parameterType="java.lang.Long" resultMap="areaResultMap">
        select area_id,name,pid from zq_area where level=3 and pid=#{pid}
    </select>

 

The speed of this kind of query is not excellent. Generally, like the three-level linkage or the three-level linkage of the provinces and municipalities, you can use nested queries for the first time and store them in redis. Next time, you can fetch data directly from redis. The efficiency is still very good.

Guess you like

Origin blog.csdn.net/qq_37557563/article/details/109196733