1.先看一下分类管理是哪个页面
2.我们先加载一些本地数据看一下效果,在这个页面里导入数据
3.查看后台页面效果
4.我们来远程加载数据
先把本地加载的关掉
看一下分类管理的请求
我们去数据库看一下表结构:
回到后台里,编写controller方法:
首先引入依赖,
然后加入实体类:
@Table(name="tb_category")
public class Category {
    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private Long id;
    private String name;
    private Long parentId;
    private Boolean isParent; // 注意isParent生成的getter和setter方法需要手动加上Is
    private Integer sort;
    //生成getter和setter方法 
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Long getParentId() {
        return parentId;
    }
    public void setParentId(Long parentId) {
        this.parentId = parentId;
    }
    public Boolean getIsParent() {
        return isParent;
    }
    public void setIsParent(Boolean parent) {
        isParent = parent;
    }
    public Integer getSort() {
        return sort;
    }
    public void setSort(Integer sort) {
        this.sort = sort;
    }
}  
编写对应的mapper
启用通用Mapper扫描
编写controller
@Controller
@RequestMapping("category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @GetMapping("list")
    public ResponseEntity<List<Category>> queryCategoriesByPid(@RequestParam(value = "pid",defaultValue = "0")Long pid){
            if (pid == null || pid < 0){
                //400: 参数不合法
                //return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
                //return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
                return ResponseEntity.badRequest().build();
            }
            List<Category> categories = this.categoryService.queryCategoriesByPid(pid);
            if (CollectionUtils.isEmpty(categories)){
                //404:资源服务器未找到
                //return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
                return ResponseEntity.notFound().build();
            }
            //200:查询成功
            return ResponseEntity.ok(categories);
    }
}  完成queryCategoriesByPid方法
启动微服务:
设置网关的域名转发
访问 http://api.leyou.com/api/item/category/list?pid=0 看看能不能查询到
可以查询到,但是,但我们点击后台页面的 分类管理时,却报错了
这是因为跨域了,下一节解决跨域问题。

京公网安备 11010502036488号