mirror of
https://gitee.com/bookshelfplus/bookshelfplus
synced 2025-09-11 19:21:39 +08:00
查询所有分类后端完成
This commit is contained in:
@@ -17,6 +17,9 @@ import plus.bookshelf.Service.Model.CategoryModel;
|
||||
import plus.bookshelf.Service.Service.BookService;
|
||||
import plus.bookshelf.Service.Service.CategoryService;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Api(tags = "书籍分类信息")
|
||||
@Controller("category")
|
||||
@RequestMapping("/category")
|
||||
@@ -25,7 +28,7 @@ public class CategoryController extends BaseController {
|
||||
@Autowired
|
||||
CategoryService categoryService;
|
||||
|
||||
@ApiOperation(value = "获取书籍分类", notes = "获取书籍分类")
|
||||
@ApiOperation(value = "获取指定分类", notes = "获取指定的书籍分类")
|
||||
@RequestMapping(value = "get", method = {RequestMethod.GET})
|
||||
@ResponseBody
|
||||
public CommonReturnType get(@RequestParam(value = "id") Integer id) {
|
||||
@@ -38,6 +41,19 @@ public class CategoryController extends BaseController {
|
||||
return CommonReturnType.create(categoryVO);
|
||||
}
|
||||
|
||||
@ApiOperation(value = "获取所有分类", notes = "获取所有的书籍分类")
|
||||
@RequestMapping(value = "list", method = {RequestMethod.GET})
|
||||
@ResponseBody
|
||||
public CommonReturnType getAll() {
|
||||
List<CategoryModel> categoryModels = categoryService.getAllCategorys();
|
||||
List<CategoryVO> categoryVOS = new ArrayList<>();
|
||||
for (CategoryModel categoryModel : categoryModels) {
|
||||
CategoryVO categoryVO = convertFromModel(categoryModel);
|
||||
categoryVOS.add(categoryVO);
|
||||
}
|
||||
return CommonReturnType.create(categoryVOS);
|
||||
}
|
||||
|
||||
private CategoryVO convertFromModel(CategoryModel categoryModel) {
|
||||
if (categoryModel == null) {
|
||||
return null;
|
||||
|
@@ -62,4 +62,6 @@ public interface CategoryDOMapper {
|
||||
int updateByPrimaryKey(CategoryDO record);
|
||||
|
||||
CategoryDO[] selectChildrenByCategoryId(Integer id);
|
||||
|
||||
CategoryDO[] selectAll();
|
||||
}
|
@@ -7,12 +7,21 @@ import plus.bookshelf.Dao.Mapper.CategoryDOMapper;
|
||||
import plus.bookshelf.Service.Model.CategoryModel;
|
||||
import plus.bookshelf.Service.Service.CategoryService;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class CategoryServiceImpl implements CategoryService {
|
||||
|
||||
@Autowired
|
||||
private CategoryDOMapper categoryDOMapper;
|
||||
|
||||
/**
|
||||
* 获取指定分类详情
|
||||
*
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public CategoryModel getCategoryById(Integer id) {
|
||||
CategoryDO categoryDO = categoryDOMapper.selectByPrimaryKey(id);
|
||||
@@ -31,6 +40,23 @@ public class CategoryServiceImpl implements CategoryService {
|
||||
return categoryModel;
|
||||
}
|
||||
|
||||
/**
|
||||
* 取得所有分类
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public List<CategoryModel> getAllCategorys() {
|
||||
CategoryDO[] categoryDOS = categoryDOMapper.selectAll();
|
||||
List<CategoryModel> categoryModels = new ArrayList<>();
|
||||
|
||||
for (CategoryDO categoryDO : categoryDOS) {
|
||||
CategoryModel categoryModel = convertFromDataObject(categoryDO);
|
||||
categoryModels.add(categoryModel);
|
||||
}
|
||||
return categoryModels;
|
||||
}
|
||||
|
||||
// 转换时不转换父亲与儿子,否则会陷入死循环
|
||||
private CategoryModel convertFromDataObject(CategoryDO categoryDO) {
|
||||
if (categoryDO == null) {
|
||||
|
@@ -2,6 +2,8 @@ package plus.bookshelf.Service.Service;
|
||||
|
||||
import plus.bookshelf.Service.Model.CategoryModel;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface CategoryService {
|
||||
/**
|
||||
* 通过分类Id获取分类
|
||||
@@ -10,4 +12,11 @@ public interface CategoryService {
|
||||
* @return
|
||||
*/
|
||||
CategoryModel getCategoryById(Integer id);
|
||||
|
||||
/**
|
||||
* 获取所有的分类列表
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
List<CategoryModel> getAllCategorys();
|
||||
}
|
||||
|
Reference in New Issue
Block a user