最近写的关于分布式的项目涉及到三级联动,需要根据后端通过一个方法向前端返回一个树形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;
/**
*目录的编号,用于parentid
*/
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;
@ApiOperation(value = "售后商品主数据一览-获取类别树结构数据" , notes="售后商品主数据一览-获取类别树结构数据")
@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>