分类列表_连接查询

本文介绍了一个商品分类系统的实现,包括CategoryVO实体类定义及其属性、CategoryMapper接口及对应的XML配置文件,展示了如何通过多表联接查询来获取不同层级的商品分类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

com.qfedu.fmmall.entity.CategoryVO

package com.qfedu.fmmall.entity;

import javax.persistence.Column;
import javax.persistence.Id;
import java.util.List;

public class CategoryVO {

    @Override
    public String toString() {
        return "CategoryVO{" +
                "categoryId=" + categoryId +
                ", categoryName='" + categoryName + '\'' +
                ", categoryLevel=" + categoryLevel +
                ", parentId=" + parentId +
                ", categoryIcon='" + categoryIcon + '\'' +
                ", categorySlogan='" + categorySlogan + '\'' +
                ", categoryPic='" + categoryPic + '\'' +
                ", categoryBgColor='" + categoryBgColor + '\'' +
                '}';
    }

    /**
     * 主键 分类id主键
     */
    @Id
    @Column(name = "category_id")
    private Integer categoryId;

    /**
     * 分类名称 分类名称
     */
    @Column(name = "category_name")
    private String categoryName;

    /**
     * 分类层级 分类得类型,
     * 1:一级大分类
     * 2:二级分类
     * 3:三级小分类
     */
    @Column(name = "category_level")
    private Integer categoryLevel;

    /**
     * 父层级id 父id 上一级依赖的id,1级分类则为0,二级三级分别依赖上一级
     */
    @Column(name = "parent_id")
    private Integer parentId;

    /**
     * 图标 logo
     */
    @Column(name = "category_icon")
    private String categoryIcon;

    /**
     * 口号
     */
    @Column(name = "category_slogan")
    private String categorySlogan;

    /**
     * 分类图
     */
    @Column(name = "category_pic")
    private String categoryPic;

    /**
     * 背景颜色
     */
    @Column(name = "category_bg_color")
    private String categoryBgColor;

    private List<CategoryVO> categories;

    public List<CategoryVO> getCategories() {
        return categories;
    }

    public void setCategories(List<CategoryVO> categories) {
        this.categories = categories;
    }

    /**
     * 获取主键 分类id主键
     *
     * @return category_id - 主键 分类id主键
     */
    public Integer getCategoryId() {
        return categoryId;
    }

    /**
     * 设置主键 分类id主键
     *
     * @param categoryId 主键 分类id主键
     */
    public void setCategoryId(Integer categoryId) {
        this.categoryId = categoryId;
    }

    /**
     * 获取分类名称 分类名称
     *
     * @return category_name - 分类名称 分类名称
     */
    public String getCategoryName() {
        return categoryName;
    }

    /**
     * 设置分类名称 分类名称
     *
     * @param categoryName 分类名称 分类名称
     */
    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }

    /**
     * 获取分类层级 分类得类型,
     * 1:一级大分类
     * 2:二级分类
     * 3:三级小分类
     *
     * @return category_level - 分类层级 分类得类型,
     * 1:一级大分类
     * 2:二级分类
     * 3:三级小分类
     */
    public Integer getCategoryLevel() {
        return categoryLevel;
    }

    /**
     * 设置分类层级 分类得类型,
     * 1:一级大分类
     * 2:二级分类
     * 3:三级小分类
     *
     * @param categoryLevel 分类层级 分类得类型,
     *                      1:一级大分类
     *                      2:二级分类
     *                      3:三级小分类
     */
    public void setCategoryLevel(Integer categoryLevel) {
        this.categoryLevel = categoryLevel;
    }

    /**
     * 获取父层级id 父id 上一级依赖的id,1级分类则为0,二级三级分别依赖上一级
     *
     * @return parent_id - 父层级id 父id 上一级依赖的id,1级分类则为0,二级三级分别依赖上一级
     */
    public Integer getParentId() {
        return parentId;
    }

    /**
     * 设置父层级id 父id 上一级依赖的id,1级分类则为0,二级三级分别依赖上一级
     *
     * @param parentId 父层级id 父id 上一级依赖的id,1级分类则为0,二级三级分别依赖上一级
     */
    public void setParentId(Integer parentId) {
        this.parentId = parentId;
    }

    /**
     * 获取图标 logo
     *
     * @return category_icon - 图标 logo
     */
    public String getCategoryIcon() {
        return categoryIcon;
    }

    /**
     * 设置图标 logo
     *
     * @param categoryIcon 图标 logo
     */
    public void setCategoryIcon(String categoryIcon) {
        this.categoryIcon = categoryIcon;
    }

    /**
     * 获取口号
     *
     * @return category_slogan - 口号
     */
    public String getCategorySlogan() {
        return categorySlogan;
    }

    /**
     * 设置口号
     *
     * @param categorySlogan 口号
     */
    public void setCategorySlogan(String categorySlogan) {
        this.categorySlogan = categorySlogan;
    }

    /**
     * 获取分类图
     *
     * @return category_pic - 分类图
     */
    public String getCategoryPic() {
        return categoryPic;
    }

    /**
     * 设置分类图
     *
     * @param categoryPic 分类图
     */
    public void setCategoryPic(String categoryPic) {
        this.categoryPic = categoryPic;
    }

    /**
     * 获取背景颜色
     *
     * @return category_bg_color - 背景颜色
     */
    public String getCategoryBgColor() {
        return categoryBgColor;
    }

    /**
     * 设置背景颜色
     *
     * @param categoryBgColor 背景颜色
     */
    public void setCategoryBgColor(String categoryBgColor) {
        this.categoryBgColor = categoryBgColor;
    }
}

com.qfedu.fmmall.dao.CategoryMapper.java

package com.qfedu.fmmall.dao;

import com.qfedu.fmmall.entity.Category;
import com.qfedu.fmmall.entity.CategoryVO;
import com.qfedu.fmmall.general.GeneralDAO;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface CategoryMapper extends GeneralDAO<Category> {

    public List<CategoryVO> selectAllCategories();
}

mappers/CategoryMapper.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.qfedu.fmmall.dao.CategoryMapper">
  <resultMap id="BaseResultMap" type="com.qfedu.fmmall.entity.Category">
    <!--
      WARNING - @mbg.generated
    -->
    <id column="category_id" jdbcType="INTEGER" property="categoryId" />
    <result column="category_name" jdbcType="VARCHAR" property="categoryName" />
    <result column="category_level" jdbcType="INTEGER" property="categoryLevel" />
    <result column="parent_id" jdbcType="INTEGER" property="parentId" />
    <result column="category_icon" jdbcType="VARCHAR" property="categoryIcon" />
    <result column="category_slogan" jdbcType="VARCHAR" property="categorySlogan" />
    <result column="category_pic" jdbcType="VARCHAR" property="categoryPic" />
    <result column="category_bg_color" jdbcType="VARCHAR" property="categoryBgColor" />
  </resultMap>

  <resultMap id="categoryVOMap" type="com.qfedu.fmmall.entity.CategoryVO">
    <id column="category_id1" jdbcType="INTEGER" property="categoryId"/>
    <result column="category_name1" jdbcType="VARCHAR" property="categoryName"/>
    <result column="category_level1" jdbcType="INTEGER" property="categoryLevel"/>
    <result column="parent_id1" jdbcType="INTEGER" property="parentId"/>
    <result column="category_icon1" jdbcType="VARCHAR" property="categoryIcon"/>
    <result column="category_slogan1" jdbcType="VARCHAR" property="categorySlogan"/>
    <result column="category_pic1" jdbcType="VARCHAR" property="categoryPic"/>
    <result column="category_bg_color1" jdbcType="VARCHAR" property="categoryBgColor"/>
    <collection property="categories" ofType="com.qfedu.fmmall.entity.CategoryVO">
        <id column="category_id2" jdbcType="INTEGER" property="categoryId"/>
        <result column="category_name2" jdbcType="VARCHAR" property="categoryName"/>
        <result column="category_level2" jdbcType="INTEGER" property="categoryLevel"/>
        <result column="parent_id2" jdbcType="INTEGER" property="parentId"/>
        <collection property="categories" ofType="com.qfedu.fmmall.entity.CategoryVO">
            <id column="category_id3" jdbcType="INTEGER" property="categoryId"/>
            <result column="category_name3" jdbcType="VARCHAR" property="categoryName"/>
            <result column="category_level3" jdbcType="INTEGER" property="categoryLevel"/>
            <result column="parent_id3" jdbcType="INTEGER" property="parentId"/>
        </collection>
    </collection>
  </resultMap>

    <!-- 联合查询-->
  <select id="selectAllCategories" resultMap="categoryVOMap">
      select
        c1.category_id 'category_id1',
        c1.category_name 'category_name1',
        c1.category_level 'category_level1',
        c1.parent_id 'parent_id',
        c1.category_icon 'category_icon1',
        c1.category_slogan 'category_slogan1',
        c1.category_pic 'category_pic1',
        c1.category_bg_color 'category_bg_color1',
        c2.category_id 'category_id2',
        c2.category_name 'category_name2',
        c2.category_level 'category_level2',
        c2.parent_id 'parent_id2',
        c3.category_id 'category_id3',
        c3.category_name 'category_name3',
        c3.category_level 'category_level3',
        c3.parent_id 'parent_id3'
        from category c1
        inner join category c2
        on c2.parent_id = c1.category_id
        inner join category c3
        on c3.parent_id = c2.parent_id
        where c1.category_level = 1
    </select>
</mapper>

在这里插入图片描述

总结
复杂度 超出自己的预期 和 想象

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值