太简单的两篇递归循环
orcale 在项目里递归循环实战
先看资产表T_ATOM_ASSET结构
看业务类别表T_ATOM_BUSI_CATEGORY结构
问题出现
页面显示
实际对应的归属业务分类
涉及到oracle递归实战(这里不会如何直接在atomAsset的seelct里面处理递归回显)
直接在实现层看atomAssetMapper和atomBusiCategoryMapper处理(这里转来转去数据格式,不知道有啥简单方法不)
impl
@Override
public PageResult<AtomAsset> queryAll(AtomAssetQueryCriteria criteria, Page<Object> page){
IPage<AtomAsset> all = atomAssetMapper.findAll(criteria, page);
List<AtomAsset> records = all.getRecords();
// 根据每条数据类的busiSort递归向下查找归属业务分类
if(records.size() > 0 ){
for (int i = 0; i < records.size(); i++) {
String busiSort = records.get(i).getBusiSort();
// 逗号分隔字符串截取
String[] split = busiSort.split(",");
// string[] 转List<String>再转Set<String>-----为了去重
List<String> strings = new ArrayList<>(Arrays.asList(split));
Set<String> taskSet = new LinkedHashSet<>();
taskSet.addAll(strings);
// 记录最终对应的分类名字结果
Set<String> taskSetResult = new LinkedHashSet<>();
for(String item : taskSet){
// 根据分类id获取分类名称(递归向下查找)
List<String> subCategory = atomBusiCategoryMapper.findSubCategory(Long.parseLong(item));
// 根据分类id获取当前自己的分类名称
String currentCategoryName = atomBusiCategoryMapper.findCategoryNameByCateforyId(Long.parseLong(item));
taskSetResult.addAll(subCategory);
taskSetResult.add(currentCategoryName);
}
// set又转成List返回前端
records.get(i).setBusiSortName(new ArrayList<>(taskSetResult));
}
}
// System.out.println(all);
return PageUtil.toPage(all);
}
直接看AtomBusiCategoryMapper.xml的小小递归写法
<select id="findSubCategory" resultType="java.lang.String">
select distinct category_name
from T_ATOM_BUSI_CATEGORY
start with superior_id = #{categotyId}
connect by prior category_id = superior_id
</select>
<select id="findCategoryNameByCateforyId" resultType="java.lang.String">
select category_name
from T_ATOM_BUSI_CATEGORY
where category_id = #{categotyId}
</select>