1、查
POJO
package com.leyou.item.pojo; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Table(name = "tb_category") public class Category { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private Long parentId; private Boolean isParent; private Integer sort; 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
package com.leyou.item.mapper; import com.leyou.item.pojo.Category; import tk.mybatis.mapper.common.Mapper; public interface CategoryMapper extends Mapper<Category> { }
Service
package com.leyou.item.service; import com.leyou.item.mapper.CategoryMapper; import com.leyou.item.pojo.Category; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class CategoryService { @Autowired private CategoryMapper categoryMapper; /** * 根据父节点查询子节点 * @param pid * @return */ public List<Category> queryCategoriesByPid(Long pid) { Category record = new Category(); record.setParentId(pid); return this.categoryMapper.select(record); } }
Controller
package com.leyou.item.controller; import com.leyou.item.pojo.Category; import com.leyou.item.service.CategoryService; import com.netflix.ribbon.proxy.annotation.Http; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.util.CollectionUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import java.util.List; @Controller @RequestMapping("/category") public class CategoryController { @Autowired private CategoryService categoryService; /** * 根据父节点的id查询子节点 * @param pid * @return */ @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); } }