/**
* 模板引擎
* 1)thymeleaf-starter 关闭缓存
* 2)静态资源放static按照路径直接访问
* 3)页面放在template下。直接访问
* springboot,项目访问自动访问index.html
* 4)不重启服务器的情况下更新资源
* 1)引入dev-tools
* 2)修改完页面,ctrl shift f9 重新自动编译下页面,代码配重启
*/
pom文件添加依赖
org.springframework.boot
spring-boot-starter-thymeleaf
2.1.8.RELEASE
org.springframework.boot
spring-boot-devtools
true
2.1.8.RELEASE
增加Catelog2Vo
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Catelog2Vo {
private String catelogId;
private List catalog3List;
private String id;
private String name;
/**
* 三级分类vo
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public static class Catelog3Vo{
private String catalog2Id;
private String name;
private String id;
}
}
首页跳转一级分类
@GetMapping({"/","index.html"})
public String indexPage(Model model){
//todo 1,查出所有的一级分类
List categoryEntities= categoryService.getLevel1Categorys();
model.addAttribute("categorys",categoryEntities);
return "index";
}
获取二三级json数据
@ResponseBody
@GetMapping("index/catalog.json")
public Map> getCatalogJson(){
Map> map= categoryService.getCatalogJson();
System.out.println(map);
return map;
}
一级 分类
List getLevel1Categorys();
@Override
public List getLevel1Categorys() {
QueryWrapper wrapper = new QueryWrapper<>();
wrapper.eq("parent_cid",0);
List categoryEntities = baseMapper.selectList(wrapper);
return categoryEntities;
}
二三级分类查询
Map> getCatalogJson();
@Override
public Map> getCatalogJson() {
//1.查出所有一级分类
List level1Categorys = getLevel1Categorys();
//2封装数据
Map> parent_cid = level1Categorys.stream().collect(Collectors.toMap(k -> k.getCatId().toString(), v -> {
//1.每一个的一级分类,查到这个一级分类的二级分类
List categoryEntities = baseMapper.selectList(new QueryWrapper()
.eq("parent_cid", v.getCatId()));
//2.封装上面的结果
List catelog2Vos = null;
if (categoryEntities != null) {
catelog2Vos = categoryEntities.stream().map(l2 -> {
Catelog2Vo catelog2Vo = new Catelog2Vo(
v.getParentCid().toString(), null, v.getName().toString(), v.getCatId().toString()
);
//找出当前二级分类的三级分类分装成vo
List categoryEntities1 = baseMapper.selectList(new QueryWrapper().eq("parent_cid", l2.getCatId()));
List collect=null;
if (categoryEntities1!=null){
collect = categoryEntities1.stream().map(l3 -> {
Catelog2Vo.Catelog3Vo catelog3Vo = new Catelog2Vo.Catelog3Vo(l2.getCatId().toString(), l3.getName(), l3.getCatId().toString());
return catelog3Vo;
}).collect(Collectors.toList());
}
catelog2Vo.setCatalog3List(collect);
return catelog2Vo;
}).collect(Collectors.toList());
}
return catelog2Vos;
}));
return parent_cid;
}
最后数据查看json格式部分
"11": [
{
"catelogId": "0",
"catalog3List": [
{
"catalog2Id": "82",
"name": "商务休闲鞋",
"id": "774"
},
{
"catalog2Id": "82",
"name": "正装鞋",
"id": "775"
},
{
"catalog2Id": "82",
"name": "休闲鞋",
"id": "776"
},
{
"catalog2Id": "82",
"name": "凉鞋/沙滩鞋",
"id": "777"
},
{
"catalog2Id": "82",
"name": "男靴",
"id": "778"
},
{
"catalog2Id": "82",
"name": "功能鞋",
"id": "779"
},
{
"catalog2Id": "82",
"name": "拖鞋/人字拖",
"id": "780"
},
{
"catalog2Id": "82",
"name": "雨鞋/雨靴",
"id": "781"
},
{
"catalog2Id": "82",
"name": "传统布鞋",
"id": "782"
},
{
"catalog2Id": "82",
"name": "鞋配件",
"id": "783"
},
{
"catalog2Id": "82",
"name": "帆布鞋",
"id": "784"
},
{
"catalog2Id": "82",
"name": "增高鞋",
"id": "785"
},
{
"catalog2Id": "82",
"name": "工装鞋",
"id": "786"
},
{
"catalog2Id": "82",
"name": "定制鞋",
"id": "787"
}
],
"id": "鞋靴",
"name": "11"
},
{
"catelogId": "0",
"catalog3List": [
{
"catalog2Id": "83",
"name": "高跟鞋",
"id": "788"
},
{
"catalog2Id": "83",
"name": "单鞋",
"id": "789"
},
{
"catalog2Id": "83",
"name": "休闲鞋",
"id": "790"
},
{
"catalog2Id": "83",
"name": "凉鞋",
"id": "791"
},
{
"catalog2Id": "83",
"name": "女靴",
"id": "792"
},
{
"catalog2Id": "83",
"name": "雪地靴",
"id": "793"
},
{
"catalog2Id": "83",
"name": "拖鞋/人字拖",
"id": "794"
},
{
"catalog2Id": "83",
"name": "踝靴",
"id": "795"
},
{
"catalog2Id": "83",
"name": "筒靴",
"id": "796"
},
{
"catalog2Id": "83",
"name": "帆布鞋",
"id": "797"
},
{
"catalog2Id": "83",
"name": "雨鞋/雨靴",
"id": "798"
},
{
"catalog2Id": "83",
"name": "妈妈鞋",
"id": "799"
},
{
"catalog2Id": "83",
"name": "鞋配件",
"id": "800"
},
{
"catalog2Id": "83",
"name": "特色鞋",
"id": "801"
},
{
"catalog2Id": "83",
"name": "鱼嘴鞋",
"id": "802"
},
{
"catalog2Id": "83",
"name": "布鞋/绣花鞋",
"id": "803"
},
{
"catalog2Id": "83",
"name": "马丁靴",
"id": "804"
},
{
"catalog2Id": "83",
"name": "坡跟鞋",
"id": "805"
},
{
"catalog2Id": "83",
"name": "松糕鞋",
"id": "806"
},
{
"catalog2Id": "83",
"name": "内增高",
"id": "807"
},
{
"catalog2Id": "83",
"name": "防水台",
"id": "808"
}
],
"id": "鞋靴",
"name": "11"
}
],