2.02 分类功能实现后台实现

三级分类效果图:2.02 分类功能实现后台实现_第1张图片

一 实现一级分类查询
步骤1:使用逆向工程生成创建实体类和对应的mapper

 实体类:Category
   mapper: CategoryMapper和CategoryMapper.xml

步骤2:

import com.one.pojo.Category;
import java.util.List;
public interface CategoryService {
    /**
     * 查询所有一级分类
     * @return
     */
    public List<Category> queryAllRootLevelCat();
}
import com.one.mapper.CategoryMapper;
import com.one.pojo.Category;
import com.one.service.category.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import java.util.List;
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<Category> queryAllRootLevelCat() {
        Example example = new Example(Category.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("type", 1);
        List<Category> result =  categoryMapper.selectByExample(example);
        return result;
    }
}

步骤3:

/**
 * 首页分类展示需求:
 * 1. 第一次刷新主页查询大分类,渲染展示到首页
 * 2. 如果鼠标上移到大分类,则加载其子分类的内容,如果已经存在子分类,则不需要加载(懒加载)
 */
@ApiOperation(value = "获取商品分类(一级分类)", notes = "获取商品分类(一级分类)", httpMethod = "GET")
@GetMapping("/cats")
public JSONResult cats() {
	List<Category> list = categoryService.queryAllRootLevelCat();
	return JSONResult.ok(list);
}

二 根据一级分类查询对应的二级和三级分类
步骤1:定义查询结果封装vo实体类

public class CategoryVO {
    private Integer id;
    private String name;
    private String type;
    private Integer fatherId;
    // 三级分类vo list
    private List<SubCategoryVO> subCatList;
}

public class SubCategoryVO {
    private Integer subId;
    private String subName;
    private String subType;
    private Integer subFatherId;
}

步骤2:定义对应查询二级和三级分类mapper

import com.one.vo.CategoryVO;
import java.util.List;
public interface CategoryMapperCustom {
    public List<CategoryVO> getSubCatList(Integer rootCatId);
}

CategoryMapperCustom.xml:

<?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="com.one.mapper.CategoryMapperCustom" >
  <resultMap id="myCategoryVO" type="com.one.vo.CategoryVO">
    <id column="id" property="id"/>
    <result column="name" property="name"/>
    <result column="type" property="type"/>
    <result column="fatherId" property="fatherId"/>
    <!--
      collection 标签:用于定义关联的list集合类型的封装规则
      property:对应三级分类的list属性名
      ofType:集合的类型,三级分类的vo
    -->
    <collection property="subCatList" ofType="com.one.vo.SubCategoryVO">
      <id column="subId" property="subId"/>
      <result column="subName" property="subName"/>
      <result column="subType" property="subType"/>
      <result column="subFatherId" property="subFatherId"/>
    </collection>
  </resultMap>

  <select id="getSubCatList" resultMap="myCategoryVO" parameterType="int">
    SELECT
        f.id as id,
        f.`name` as `name`,
        f.type as type,
        f.father_id as fatherId,
        c.id as subId,
        c.`name` as subName,
        c.type as subType,
        c.father_id as subFatherId
    FROM
        category f
    LEFT JOIN
        category c
    on
        f.id = c.father_id
    WHERE
        f.father_id = #{rootCatId}
  </select>

</mapper>

步骤3:实现类调用mapper对应的方法

/**
 * 根据一级分类id查询子分类信息
 * @param rootCatId
 * @return
 */
public List<CategoryVO> getSubCatList(Integer rootCatId);
@Autowired
private CategoryMapperCustom categoryMapperCustom;
@Transactional(propagation = Propagation.SUPPORTS)
@Override
public List<CategoryVO> getSubCatList(Integer rootCatId) {
	return categoryMapperCustom.getSubCatList(rootCatId);
}

步骤4:写接口调用对应的方法实现

@ApiOperation(value = "获取商品子分类", notes = "获取商品子分类", httpMethod = "GET")
@GetMapping("/subCat/{rootCatId}")
public JSONResult subCat(
		@ApiParam(name = "rootCatId", value = "一级分类id", required = true)
		@PathVariable Integer rootCatId) {
	if (rootCatId == null) {
		return JSONResult.errorMsg("分类不存在");
	}
	List<CategoryVO> list = categoryService.getSubCatList(rootCatId);
	return JSONResult.ok(list);
}

你可能感兴趣的:(java架构笔记,spring,mybatis,三级分类实现)