层级分类结构的实现

利用id进行层级分类:
    
    思路:设置父类id,一级id=0;二级id=1;以此类推:
    
    1.数据库表的设计:
        CREATE TABLE `tb_category` (
          `id` int(20) NOT NULL AUTO_INCREMENT COMMENT '分类ID',
          `name` varchar(50) DEFAULT NULL COMMENT '分类名称',
          `goods_num` int(11) DEFAULT '0' COMMENT '商品数量',
          `is_show` char(1) DEFAULT NULL COMMENT '是否显示',
          `is_menu` char(1) DEFAULT NULL COMMENT '是否导航',
          `seq` int(11) DEFAULT NULL COMMENT '排序',
          `parent_id` int(20) DEFAULT NULL COMMENT '上级ID',
          `template_id` int(11) DEFAULT NULL COMMENT '模板ID',
          PRIMARY KEY (`id`),
          KEY `parent_id` (`parent_id`)
        ) ENGINE=InnoDB AUTO_INCREMENT=11157 DEFAULT CHARSET=utf8 COMMENT='商品类目';
        
    2.根据id查询其下的所有子类:
        -- 一级分类列表查询
        SELECT * from tb_category WHERE parent_id=0

        -- 二级分类列表查询
        SELECT * from tb_category WHERE parent_id=1

        -- 三级分类列表查询
        SELECT * from tb_category WHERE parent_id=2
        
    3.代码思路:
        分析:
            根据页面传递过来的请求,传递parent_id的值
            后台接收到请求 执行查询
            返回List<TbCateogry>


             GET 请求:/category/list/{pid}
             参数: 父分类的ID(你点击到的分类的ID 作为父分类id)
             返回值:Result<List<Category>>

             controller service dao
             
    4.响应结果工具类:
        package entity;

        import java.io.Serializable;

        /**
         * 描述
         *
         * @author 三国的包子
         * @version 1.0
         * @package entity *
         * @since 1.0
         */
        public class Result<T> implements Serializable {
            private boolean flag;//是否成功
            private Integer code;//返回码
            private String message;//返回消息
            private T data;//返回数据

            public Result(boolean flag, Integer code, String message, Object data) {
                this.flag = flag;
                this.code = code;
                this.message = message;
                this.data = (T) data;
            }

            public Result(boolean flag, Integer code, String message) {
                this.flag = flag;
                this.code = code;
                this.message = message;
            }

            public Result() {
                this.flag = true;
                this.code = StatusCode.OK;
                this.message = "操作成功!";
            }

            public boolean isFlag() {
                return flag;
            }

            public void setFlag(boolean flag) {
                this.flag = flag;
            }

            public Integer getCode() {
                return code;
            }

            public void setCode(Integer code) {
                this.code = code;
            }

            public String getMessage() {
                return message;
            }

            public void setMessage(String message) {
                this.message = message;
            }

            public T getData() {
                return data;
            }

            public void setData(T data) {
                this.data = data;
            }
        }
        
    封装查询条件的工具类:
        

        import java.io.Serializable;

        /**
         * 封装查询条件
         */
        public class QueryPageBean implements Serializable{
            private Integer currentPage;//页码
            private Integer pageSize;//每页记录数
            private String queryString;//查询条件

            public Integer getCurrentPage() {
                return currentPage;
            }

            public void setCurrentPage(Integer currentPage) {
                this.currentPage = currentPage;
            }

            public Integer getPageSize() {
                return pageSize;
            }

            public void setPageSize(Integer pageSize) {
                this.pageSize = pageSize;
            }

            public String getQueryString() {
                return queryString;
            }

            public void setQueryString(String queryString) {
                this.queryString = queryString;
            }
        }
        
    分页结果封装工具类:
        

        import java.io.Serializable;
        import java.util.List;

        /**
         * 分页结果封装对象
         */
        public class PageResult implements Serializable{
            private Long total;//总记录数
            private List rows;//当前页结果
            public PageResult(Long total, List rows) {
                super();
                this.total = total;
                this.rows = rows;
            }
            public Long getTotal() {
                return total;
            }
            public void setTotal(Long total) {
                this.total = total;
            }
            public List getRows() {
                return rows;
            }
            public void setRows(List rows) {
                this.rows = rows;
            }
        }
    
    5.分页前端代码:
          //分页查询
                findPage() {
                    //分页参数
                    var param = {
                        currentPage:this.pagination.currentPage,//页码
                        pageSize:this.pagination.pageSize,//每页显示的记录数
                        queryString:this.pagination.queryString//查询条件
                    };
                    //请求后台
                    axios.post("/checkitem/findPage.do",param).then((response)=> {
                        //为模型数据赋值,基于VUE的双向绑定展示到页面
                        this.dataList = response.data.rows;
                        this.pagination.total = response.data.total;
                    });

    6.分页查询后台代码:
        Controller:
        
            //分页查询
            @RequestMapping("/findPage")
            @PreAuthorize(value = "hasAuthority('CHECKITEM_QUERY')")
            public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
                PageResult pageResult = checkItemService.pageQuery(
                        queryPageBean.getCurrentPage(),
                        queryPageBean.getPageSize(),
                        queryPageBean.getQueryString());
                return pageResult;
            }
            
        Service:
        
             public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
                //初始化数据
                PageHelper.startPage(currentPage,pageSize);
                //查询返回page
                Page<CheckItem> page = checkItemDao.selectByCondition(queryString);
                //封装成PageResult
                return new PageResult(page.getTotal(),page.getResult());
            }
        
        SQL语句:
        
        <!--分页查询-->
        <select id="selectByCondition" parameterType="String" resultType="com.itheima.health.pojo.CheckItem">
            select * from t_checkitem
            <where>
                <if test="value != null and value.length > 0">
                    AND code = #{value} or name = #{value}
                </if>
            </where>

        </select>
    
    
    
    
    
    
    
    
    
    
        
原文地址:https://www.cnblogs.com/lyle-liu/p/12891891.html