查询分类类别

This commit is contained in:
Bunny 2023-12-15 09:33:34 +08:00
parent bc29b1f58e
commit c6abd66b51
4 changed files with 62 additions and 2 deletions

View File

@ -5,6 +5,8 @@ import cn.bunny.common.spzx.model.entity.product.Category;
import cn.bunny.common.spzx.model.vo.common.Result;
import cn.bunny.common.spzx.model.vo.common.ResultCodeEnum;
import cn.bunny.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@ -13,12 +15,14 @@ import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@Tag(name = "查询分类")
@RestController
@RequestMapping("/admin/product/category")
public class CategoryController {
@Autowired
CategoryService categoryService;
@Operation(summary = "查询分类类别", description = "使用懒加载")
@GetMapping("/findCategoryList/{id}")
public Result findCategoryList(@PathVariable("id") Long id) {
List<Category> categoryList = categoryService.findCategoryList(id);

View File

@ -0,0 +1,15 @@
package cn.bunny.mapper;
import cn.bunny.common.spzx.model.entity.product.Category;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface CategoryMapper {
// 查询分类类别
List<Category> selectCategoryByParentId(Long id);
// 查询是否有下级菜单
int selectCountByParentId(Long parentId);
}

View File

@ -1,15 +1,31 @@
package cn.bunny.service.impl;
import cn.bunny.common.spzx.model.entity.product.Category;
import cn.bunny.mapper.CategoryMapper;
import cn.bunny.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.List;
@Service
public class CategoryServiceImpl implements CategoryService {
@Autowired
private CategoryMapper categoryMapper;
// 根据id查询分类列表
@Override
public List<Category> findCategoryList(Long id) {
return null;
List<Category> categoryList = categoryMapper.selectCategoryByParentId(id);
if (!CollectionUtils.isEmpty(categoryList)) {
categoryList.forEach(category -> {
int count = categoryMapper.selectCountByParentId(category.getId());
category.setHasChildren(count > 0);
});
}
return categoryList;
}
}
}

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.bunny.mapper.CategoryMapper">
<resultMap id="categoryMap" type="cn.bunny.common.spzx.model.entity.product.Category" autoMapping="true"/>
<!-- 用于select查询公用抽取的列 -->
<sql id="columns">
id,name,image_url,parent_id,status,order_num,create_time,update_time,is_deleted
</sql>
<!-- 查询下层分类 -->
<select id="selectCategoryByParentId" resultType="cn.bunny.common.spzx.model.entity.product.Category">
select
<include refid="columns"/>
from category where parent_id=#{id} and is_deleted=0 order by id desc;
</select>
<!-- 查询是否有下层分类 -->
<select id="selectCountByParentId" resultType="java.lang.Integer">
select count(*)
from category
where parent_id = #{id}
and is_deleted = 0
</select>
</mapper>