Entity层
实体类:BCategoryDict.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| import com.fasterxml.jackson.annotation.JsonFormat; import lombok.Data; import java.util.Date;
@Data public class BCategory { private Long id; private Long parentId; private String ancestors; private String name; private Integer level; private Integer status; private Integer sort; private String description; }
|
Vo类:BCategoryDictVo.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| import com.fasterxml.jackson.annotation.JsonFormat; import lombok.Data; import java.util.Date; import java.util.ArrayList; import java.util.List;
@Data public class BCategoryVo { private Long id; private Long parentId; private String ancestors; private String name; private Integer level; private Integer status; private Integer sort; private String description; private List<BCategoryVo> children = new ArrayList<BCategoryVo>();; }
|
树形结构类:BCategoryTree.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| import com.fasterxml.jackson.annotation.JsonInclude; import lombok.Data; import java.io.Serializable; import java.util.List; import java.util.stream.Collectors;
@Data public class BCategoryTree implements Serializable { private static final long serialVersionUID = 1L;
private Long id;
private String label;
@JsonInclude(JsonInclude.Include.NON_EMPTY) private List<BCategoryTree> children; public BCategoryTree() { } public BCategoryTree(BCategoryVo item) { this.id = item.getId(); this.label = item.getName(); this.children = item.getChildren().stream().map(BCategoryTree::new).collect(Collectors.toList()); } }
|
Controller层
对外接口:BCategoryController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| import com.smart.common.core.domain.AjaxResult; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.Map;
@RestController @RequestMapping("/business/category") public class BCategoryController { @Autowired private IBCategoryService bCategoryService;
@RequestMapping(value = "/getTreeByParams",method = RequestMethod.GET) public List<BCategoryTree> getTreeByParams(@RequestParam Map params) { return bCategoryService.getTreeByParams(params); } }
|
Service层
业务逻辑层接口:IBCategoryService.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import com.smart.common.core.domain.AjaxResult; import java.util.Map;
public interface IBCategoryService {
public List<BCategoryTree> getTreeByParams(Map params); }
|
业务逻辑层接口实现类:BCategoryServiceImpl.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.*; import java.util.stream.Collectors;
@Service public class BCategoryServiceImpl implements IBCategoryService { @Autowired private BCategoryMapper bCategoryMapper;
@Override public List<BCategoryTree> getTreeByParams(Map params) { List<BCategoryVo> lists = bCategoryMapper.getBCategoryListByParams(params); List<BCategoryVo> categoryTrees = buildTree(lists); List<BCategoryTree> data = categoryTrees.stream().map(BCategoryTree::new).collect(Collectors.toList()); return data; }
public List<BCategoryVo> buildTree(List<BCategoryVo> list) { List<BCategoryVo> returnList = new ArrayList<BCategoryVo>(); List<Long> idList = new ArrayList<Long>(); for (BCategoryVo item : list) { idList.add(item.getId()); } for (Iterator<BCategoryVo> iterator = list.iterator(); iterator.hasNext();) { BCategoryVo vo = (BCategoryVo) iterator.next(); if (!idList.contains(vo.getParentId())) { recursionFn(list, vo); if (vo.getLevel() == 1){ returnList.add(vo); } } } if (returnList.isEmpty()) { returnList = list; } return returnList; }
private void recursionFn(List<BCategoryVo> list, BCategoryVo t) { List<BCategoryVo> childList = getChildList(list, t); t.setChildren(childList); for (BCategoryVo tChild : childList) { if(getChildList(list, tChild).size() > 0){ recursionFn(list, tChild); } } }
private List<BCategoryVo> getChildList(List<BCategoryVo> list, BCategoryVo t) { List<BCategoryVo> tlist = new ArrayList<BCategoryVo>(); Iterator<BCategoryVo> it = list.iterator(); while (it.hasNext()) { BCategoryVo n = (BCategoryVo) it.next(); if (null != n.getParentId() && n.getParentId().longValue() == t.getId().longValue()) { tlist.add(n); } } return tlist; } }
|
Mapper层
Mapper接口:BCategoryMapper.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; import java.util.List; import java.util.Map;
@Repository @Mapper public interface BCategoryMapper {
public List<BCategoryVo> getBCategoryListByParams(Map params); }
|
mapper.xml
resultType
需要自己指定上面的vo类路径
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <select id="getBCategoryListByParams" parameterType="map" resultType="com.smart.business.entity.vo.BCategoryVo"> SELECT id, parent_id, ancestors, name, level, status, sort, description FROM b_category <where> <if test="ancestors != null"> AND find_in_set( #{ancestors}, ancestors) </if> <if test="parentId != null"> AND parent_id = #{parentId} </if> <if test="level != null"> AND level = #{level} </if> <if test="status != null"> AND status = #{status} </if> </where> </select>
|
结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| [ { "id": 1, "label": "首页模块", "children": [ { "id": 6, "label": "A图标" }, { "id": 7, "label": "B图标" } ] }, { "id": 2, "label": "个人中心模块", "children": [ { "id": 8, "label": "A图标" }, ] }, { "id": 3, "label": "看视频模块", "children": [ { "id": 16, "label": "图标", "children": [ { "id": 17, "label": "A图标" }, { "id": 18, "label": "B图标" } ] } ] } ]
|
数据库表结构,mysql
1 2 3 4 5 6 7 8 9 10 11 12
| CREATE TABLE `b_category` ( `id` int(11) NOT NULL AUTO_INCREMENT, `parent_id` bigint(20) DEFAULT '0' COMMENT '父类别id', `ancestors` varchar(50) DEFAULT '' COMMENT '祖级列表', `name` varchar(255) DEFAULT NULL COMMENT '类型名', `level` int(11) DEFAULT NULL COMMENT '分类等级', `status` int(1) DEFAULT '1' COMMENT '状态', `sort` int(10) DEFAULT '0' COMMENT '排序', `description` varchar(255) DEFAULT NULL COMMENT '描述', `create_time` timestamp NULL DEFAULT NULL COMMENT '创建时间', PRIMARY KEY (`id`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COMMENT='分类';
|