一、定义返回对象ItemCatReult
public class ItemCatResult {
@JsonProperty("data")
private List<ItemCatData> itemCats = new ArrayList<ItemCatData>();
public List<ItemCatData> getItemCats() {
return itemCats;
}
public void setItemCats(List<ItemCatData> itemCats) {
this.itemCats = itemCats;
}
}
二、定义目录对象ItemCatData
public class ItemCatData {
@JsonProperty("u")// 序列化成json数据时为 u
private String url;
@JsonProperty("n")
private String name;
@JsonProperty("i")
private List<?> items;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<?> getItems() {
return items;
}
public void setItems(List<?> items) {
this.items = items;
}
}
三、写查询接口ApiItemCatController
@RequestMapping("api/item/cat")
@Controller
public class ApiItemCatController {
@Autowired
private ItemCatService itemCatService;
private static final ObjectMapper MAPPER = new ObjectMapper();
/**
* 对外提供接口服务,查询所有类目数据
*
* @return
*/
@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<ItemCatResult> queryItemCatList() {
try {
ItemCatResult itemCatResult = this.itemCatService.queryAllToTree();
return ResponseEntity.ok(itemCatResult);
} catch (Exception e) {
e.printStackTrace();
}
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
}
四、写Service
@Service
public class ItemCatService extends BaseService<ItemCat> {
/**
* 全部查询,并且生成树状结构
*
* @return
*/
public ItemCatResult queryAllToTree() {
ItemCatResult result = new ItemCatResult();
// 全部查出,并且在内存中生成树形结构
List<ItemCat> cats = super.queryAll();
// 转为map存储,key为父节点ID,value为数据集合
Map<Long, List<ItemCat>> itemCatMap = new HashMap<Long, List<ItemCat>>();
for (ItemCat itemCat : cats) {
if (!itemCatMap.containsKey(itemCat.getParentId())) {
itemCatMap.put(itemCat.getParentId(), new ArrayList<ItemCat>());
}
itemCatMap.get(itemCat.getParentId()).add(itemCat);
}
// 封装一级对象
List<ItemCat> itemCatList1 = itemCatMap.get(0L);
for (ItemCat itemCat : itemCatList1) {
ItemCatData itemCatData = new ItemCatData();
itemCatData.setUrl("/products/" + itemCat.getId() + ".html");
itemCatData.setName("<a href='" + itemCatData.getUrl() + "'>" + itemCat.getName() + "</a>");
result.getItemCats().add(itemCatData);
if (!itemCat.getIsParent()) {
continue;
}
// 封装二级对象
List<ItemCat> itemCatList2 = itemCatMap.get(itemCat.getId());
List<ItemCatData> itemCatData2 = new ArrayList<ItemCatData>();
itemCatData.setItems(itemCatData2);
for (ItemCat itemCat2 : itemCatList2) {
ItemCatData id2 = new ItemCatData();
id2.setName(itemCat2.getName());
id2.setUrl("/products/" + itemCat2.getId() + ".html");
itemCatData2.add(id2);
if (itemCat2.getIsParent()) {
// 封装三级对象
List<ItemCat> itemCatList3 = itemCatMap.get(itemCat2.getId());
List<String> itemCatData3 = new ArrayList<String>();
id2.setItems(itemCatData3);
for (ItemCat itemCat3 : itemCatList3) {
itemCatData3.add("/products/" + itemCat3.getId() + ".html|" + itemCat3.getName());
}
}
}
if (result.getItemCats().size() >= 14) {
break;
}
}
try {
// 将数据库查询结果集写入到缓存中
this.redisService.set(REDIS_KEY, MAPPER.writeValueAsString(result), REDIS_TIME);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}