数据库中基本table表结构
id classname parent_id
1 服装 null
2 男装 1
3 女装 1
4 男裤 2
5 男上衣 2
…….
/**
* 商品分类实体类
*/
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@Entity
@Table(name = "wemall_goodsclass")
public class GoodsClass extends IdEntity {
private static final long serialVersionUID = 1L;
//分类名称
private String className;
//商品子分类
@OneToMany(mappedBy = "parent")
@OrderBy("sequence asc")
private List<GoodsClass> childs = new ArrayList<GoodsClass>();
//商品父类
@ManyToOne(fetch = FetchType.LAZY)
private GoodsClass parent;
private int sequence;
//等级等级
private int level;
.......省略其他字段以及get set方法
@Autowired
private IGoodsClassService goodsClassService;
@RequestMapping({ "/user/goodsclass.htm" })
private void getGoodsClass(HttpServletRequest request, HttpServletResponse response) {
Map<String, String> resultMap = new HashMap<>();
Map params = new HashMap();
params.put("display", Boolean.valueOf(true));
List<GoodsClass> gcs = this.goodsClassService.query(
"select obj from GoodsClass obj where obj.parent.id is null and obj.display=:display order by obj.sequence asc",
params, -1, -1);
if (gcs.size() < 0) {
resultMap.put("code", "102");
resultMap.put("msg", "查无数据");
writeResponse(response, resultMap);
} else {
stringify(response, gcs);
}
}
/**
* 把对象转换为json字符串 输出
* @param goodsClass 商品类型对象
*/
private void stringify(HttpServletResponse response,List<GoodsClass> goodsClass) {
// 商品
// ---> 商品子分类
// ---> 商品子分类
JSONArray list = new JSONArray();
for (GoodsClass firstGoodsClass : goodsClass) { // 迭代第一层商品类型
JSONObject firstMap = new JSONObject(); // 存储第一层商品类型
firstMap.put("id", firstGoodsClass.getId());
firstMap.put("className", firstGoodsClass.getClassName());
List<GoodsClass> sencondGoodsClassList = firstGoodsClass.getChilds(); // 获取第一层商品类型的子分类
JSONArray sencondList = new JSONArray();
for (GoodsClass sencondGoodsClass : sencondGoodsClassList) {// 迭代第一层子分类
JSONObject sencondMap = new JSONObject();
sencondMap.put("id", sencondGoodsClass.getId());
sencondMap.put("className", sencondGoodsClass.getClassName());
List<GoodsClass> thirdGoodsClassList = sencondGoodsClass.getChilds(); // 获取第二层子分类
JSONArray thirdList = new JSONArray();
for (GoodsClass thirdGoodsClass : thirdGoodsClassList) { // 迭代第二层子分类
JSONObject thirdMap = new JSONObject();
thirdMap.put("id", thirdGoodsClass.getId());
thirdMap.put("className", thirdGoodsClass.getClassName());
thirdList.add(thirdMap); // 将每个第二层的子分类存储到第二层子分类列表中
}
sencondMap.put("childGoodsClass", thirdList); // 将第二层子分类列表存储在第一层子分类中
sencondList.add(sencondMap); // 将第一层子分类存储在第一层子分类列表
}
firstMap.put("childGoodsClass", sencondList); // 将第二层子分类列表存储在第一层商品分类中
list.add(firstMap);
}
Gson gson = new Gson();
PrintWriter writer = getPrintWriter(response);
writer.print(gson.toJson(list));
}
/**
*
* @param response
* @param map
*/
private void writeResponse(HttpServletResponse response, Map<String, String> map) {
Gson gson = new Gson();
PrintWriter writer = getPrintWriter(response);
writer.print(gson.toJson(map));
}
private PrintWriter getPrintWriter(HttpServletResponse response) {
response.setContentType("text/plain");
response.setHeader("Cache-Control", "no-cache");
response.setCharacterEncoding("UTF-8");
try {
return response.getWriter();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
-----------------------------------------------分割线
递归循环获取方法
@RequestMapping(value = "/DagTree")
public void tree(HttpServletRequest request, HttpServletResponse response) throws Exception {
Map params = HtmlUtil.getParameterMap(request);
List<Map<String, Object>> lst = getCJtreeList(params);
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map kqgz_map = new HashMap();
kqgz_map.put("children", lst);
kqgz_map.put("id", "-1");
kqgz_map.put("text", "库房结构树");
kqgz_map.put("state", "open");
kqgz_map.put("sfkf", "0");
list.add(kqgz_map);
HtmlUtil.writerJson(response, list);
}
private List<Map<String, Object>> getCJtreeList(Map paramMap) throws Exception {
List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
result = baseService.selectList("com.kq.dagl.dakgl.DAGL_DagTree", paramMap);
List<Map<String, Object>> LIST_CJ = result;
result = this.iteration("", LIST_CJ);//上级ID为0 表示最高级别
LIST_CJ = result;
return LIST_CJ;
}