mybatis 根据父节点查询所有子节点(三级联动+resultMap三级映射)

最近写的关于分布式的项目涉及到三级联动,需要根据后端通过一个方法向前端返回一个树形list,如:
在这里插入图片描述
根据大佬的思路:
数据库sql:自查询,查询出所有的字段
mybatis:需要使用resultMap嵌套collection,然后在connection中嵌套resultMap,然后再在resultMap中嵌套collection,最后在collection中嵌套最后一个resultMap
关于映射问题:因为从sql语句返回的就是具有三层关系的数据,所以需要用resultMap嵌套collection完成,至于这三个entity会在代码上贴出
剩下的service层和controller就和普通的一样就可以了

下面贴代码:
首先是vo对象,这三个vo需要通过resultMap和collection关联:

CatalogTreeVo 

@Data
@NoArgsConstructor
public class CatalogTreeVo implements Serializable {
    /*
     *编号
     * */
    private String categoryId;

    /**
     *产品类型:分为原厂配件,生活精品
     */
    private String categoryType;

    /**
     *目录的编号,用于parentid
     */
    private String categoryCode;

    /**
     *产品名称
     */
    private String name;

    /**
     *通过catalogNo找到父项
     */
    private String parentId;



    //用于返回children
    private List<CatalogTreeChildVo> child;
}

CatalogTreeChildVo 


@Data
@NoArgsConstructor
public class CatalogTreeChildVo implements Serializable {
    /*
     *编号
     * */
    private String categoryId;

    /**
     *产品类型:分为原厂配件,生活精品
     */
    private String categoryType;

    /**
     *目录的编号,用于parentid
     */
    private String categoryCode;

    /**
     *产品名称
     */
    private String name;

    /**
     *通过catalogNo找到父项
     */
    private String parentId;



    //用于返回children
    private List<CatalogTmVo> child;
}
CatalogTmVo 

@Data
@NoArgsConstructor
public class CatalogTmVo implements Serializable {
    /*
     *编号
     * */
    private String categoryId;

    private String categoryType;

    private String categoryCode;

    /**
     *产品名称
     */
    private String name;

    /**
     *通过catalogNo找到父项
     */
    private String parentId;

}

然后是controller:

ProductController 


@Api(value = "Product Controller", description = "Operation and management of Product ")
@RestController
@RequestMapping(value = "/mdm/product/")
public class ProductController {
    @Autowired
    ITreeService iTreeService;

    @ApiImplicitParam(name = "tree", value = "类目VO", required = true, dataType = "CatalogTreeVo")
    @GetMapping("tree")
    public AjaxResult treetest() {
        List<CatalogTreeVo> responseCategoryVoList= iTreeService.tree();
        return AjaxResult.getInstance().successT(responseCategoryVoList);
    }
}

service:

ITreeService 

public interface ITreeService extends BaseService<TmCatalog> {
    List<CatalogTreeVo> tree();
}

TreeServiceImpl 

@Service
public class TreeServiceImpl extends BaseServiceImpl<TreeMapper, TmCatalog> implements ITreeService {
    @Autowired
    private TreeMapper treeMapper;
    @Override
    public List<CatalogTreeVo> tree() {
        List<CatalogTreeVo> catalogTreeVos = treeMapper.test();
        return catalogTreeVos;
    }
}

mapper:

TreeMapper 

public interface TreeMapper extends BaseMapper<TmCatalog> {
    List<CatalogTreeVo> test();
}
<?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.capge.cn.mdm.mapper.TreeMapper">

    <resultMap id="BaseResultMap" type="com.capge.cn.mdm.api.response.CatalogTreeVo">
        <id column="catalog_code" property="categoryId" jdbcType="VARCHAR"/>
        <result column="catalog_type" property="categoryType" jdbcType="VARCHAR"/>
        <result column="catalog_no" property="categoryCode" jdbcType="VARCHAR"/>
        <result column="name_cn" property="name" jdbcType="VARCHAR"/>
        <result column="parent_code" property="parentId" jdbcType="VARCHAR"/>
        <collection property="child"
                    javaType="ArrayList"
                    ofType="com.capge.cn.mdm.api.response.CatalogTreeChildVo"
                    resultMap="ChildMap" columnPrefix="c_">
        </collection>

    </resultMap>

    <resultMap id="ChildMap" type="com.capge.cn.mdm.api.response.CatalogTreeChildVo">
        <id column="catalog_code" property="categoryId" jdbcType="VARCHAR"/>
        <result column="catalog_type" property="categoryType" jdbcType="VARCHAR"/>
        <result column="catalog_no" property="categoryCode" jdbcType="VARCHAR"/>
        <result column="name_cn" property="name" jdbcType="VARCHAR"/>
        <result column="parent_code" property="parentId" jdbcType="VARCHAR"/>

        <collection property="child"
                    javaType="ArrayList" ofType="com.capge.cn.mdm.api.response.CatalogTmVo"
                    resultMap="TmMap" columnPrefix="t_"/>

    </resultMap>

    <resultMap id="TmMap" type="com.capge.cn.mdm.api.response.CatalogTmVo">
        <id column="catalog_code" property="categoryId" jdbcType="VARCHAR"/>
        <result column="catalog_type" property="categoryType" jdbcType="VARCHAR"/>
        <result column="catalog_no" property="categoryCode" jdbcType="VARCHAR"/>
        <result column="name_cn" property="name" jdbcType="VARCHAR"/>
        <result column="parent_code" property="parentId" jdbcType="VARCHAR"/>
    </resultMap>



    <select id="test" resultMap="BaseResultMap">
        SELECT  c1.catalog_code,
                c1.catalog_type,
                c1.catalog_no,
                c1.name_cn,
                c1.parent_code,
                c2.catalog_code AS c_catalog_code,
                c2.catalog_type AS c_catalog_type,
                c2.catalog_no AS c_catalog_no,
                c2.name_cn AS c_name_cn,
                c2.parent_code AS c_parent_code,
                c3.catalog_code AS c_t_catalog_code,
                c3.catalog_type AS c_t_catalog_type,
                c3.catalog_no AS c_t_catalog_no,
                c3.name_cn AS c_t_name_cn,
                c3.parent_code AS c_t_parent_code
                FROM tm_afs_catalog c1
        LEFT JOIN tm_afs_catalog c2 ON c1.catalog_code=c2.parent_code
        LEFT JOIN tm_afs_catalog c3 ON c2.catalog_code=c3.parent_code WHERE c1.parent_code='-1'
    </select>

</mapper>
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值