1.数据格式
2.对象模型
public class ItemCat {
//转换成json数据时使用u作为key
@JsonProperty("u")
private String url;
@JsonProperty("n")
private String name;
@JsonProperty("i")
private List<?> item;
}
public class ItemCatResult {
private List<?> data;
public List<?> getData() {
return data;
}
public void setData(List<?> data) {
this.data = data;
}
}
3.service逻辑处理
@Service
public class ItemCatServiceImpl implements ItemCatService {
@Autowired
private TbItemCatMapper itemCatMapper;
@Override
public ItemCatResult queryAllCategory() throws Exception {
ItemCatResult result = new ItemCatResult();
result.setData(getItemCatList(0l));
return result;
}
/**
* 查询分类列表
* <p>Title: getItemCatList</p>
* <p>Description: </p>
* @param parentid
* @return
*/
private List<?> getItemCatList(long parentid) {
TbItemCatExample example = new TbItemCatExample();
Criteria criteria = example.createCriteria();
//查询parentid为0的分类信息
criteria.andParentIdEqualTo(parentid);
List<TbItemCat> list = itemCatMapper.selectByExample(example);
List dataList = new ArrayList();
for (TbItemCat tbItemCat : list) {
//判断是否为父节点
if (tbItemCat.getIsParent()) {
ItemCat itemCat = new ItemCat();
itemCat.setUrl("/category/" + tbItemCat.getId() + ".html");
itemCat.setName(tbItemCat.getName());
//递归调用
itemCat.setItem(getItemCatList(tbItemCat.getId()));
//添加到列表
dataList.add(itemCat);
} else {
String catItem = "/item/" + tbItemCat.getId() + ".html|" + tbItemCat.getName();
dataList.add(catItem);
}
}
return dataList;
}
}
注:代码可能对大家帮助不大,因为毕竟数据库设计的不一样,但查询思路差别不大。