mybatis+mysql查询大类别下的所有图书(递归)

场景:查询某个大类别下所有类别(及类别下子类别..)下的所有图书(递归查询)[分页\排序]

实体类

public class Category implements Serializable {

    private int id;
    private String cname;
    private int count;//类别数量
    private List<Category> categoryList;//子类类别集合

    private List<Book> bookList;//类别下的图书集合


    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public List<Book> getBookList() {
        return bookList;
    }

    public void setBookList(List<Book> bookList) {
        this.bookList = bookList;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getCname() {
        return cname;
    }

    public void setCname(String cname) {
        this.cname = cname;
    }

    public List<Category> getCategoryList() {
        return categoryList;
    }

    public void setCategoryList(List<Category> categoryList) {
        this.categoryList = categoryList;
    }

    @Override
    public String toString() {
        return "Category{" +
                "id=" + id +
                ", cname='" + cname + '\'' +
                ", count=" + count +
                ", categoryList=" + categoryList +
                ", bookList=" + bookList +
                '}';
    }
}

dao

//根据类别查找图书1:按上架时间排序 2:按销量排序(热度) 3价格排序
    //ispa 0:无父类别 1:有父类别
    public List<Category> books(@Param("categoryId") int categoryId,
                                @Param("isPa") int isPa,
                                @Param("orderBy") int orderBy,
                                @Param("startPos") int startPos);

    //查询类别下图书数量
    public int count(@Param("categoryId") int categoryId,
                     @Param("isPa") int isPa);

mapper

<!--查询类别下的图书-->
    <resultMap id="categoryMap3" type="Category">
        <collection property="bookList" ofType="Book">
            <id property="bookid" column="b_id"></id>
            <result property="bname" column="bname"></result>
            <result property="author" column="author"></result>
            <result property="publisher" column="publisher"></result>
            <result property="publishTime" column="publish_time"></result>
            <result property="brief" column="brief"></result>
            <result property="price" column="price"></result>
            <result property="ddprice" column="ddprice"></result>
            <result property="img" column="img"></result>
        </collection>
    </resultMap>

    <select id="books" resultMap="categoryMap3">
        SELECT *
        FROM dcategory c, dbook b
        WHERE c.id = b.category_id
        <if test="isPa==0">
            AND c.parent_id = #{categoryId}
        </if>
        <if test="isPa==1">
            AND c.id = #{categoryId}
        </if>
        <if test="orderBy==1">
            ORDER BY b.publish_time DESC
        </if>
        <if test="orderBy==2">
            ORDER BY b.sales DESC
        </if>
        <if test="orderBy==3">
            ORDER BY b.ddprice DESC
        </if>
        LIMIT #{startPos},4
    </select>

    <select id="count" resultType="int">
        SELECT count(*)
        FROM dcategory c, dbook b
        WHERE c.id = b.category_id
        <if test="isPa==0">
            AND c.parent_id = #{categoryId}
        </if>
        <if test="isPa==1">
            AND c.id = #{categoryId}
        </if>
    </select>

分页工具类在博客列表

service

@Override
 public Page<Category> getBooks(int categoryId,
                                   int isPa,
                                   int orderBy,
                                   int pageNow) {
        int totalCount = categoryDao.count(categoryId, isPa);
        Page<Category> categoryPage = new Page<Category>(totalCount, pageNow);
        int startPos = categoryPage.getStartPos();
        List<Category> categoryList = categoryDao.books(categoryId, isPa, orderBy, startPos);
        categoryPage.setItems(categoryList);
        return categoryPage;
    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孟林洁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值