请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

出于什么考虑还单独searchListCount了一下?

<select id="searchListByPage" parameterType="Map" resultType="HashMap">
    SELECT id,
           `code`,
           title,
           description,
           image,
           initial_price AS "initialPrice",
           current_price AS "currentPrice",
           sales_volume AS "salesVolume"
    FROM tb_goods
    WHERE `status` = 1
    <if test="keyword!=null">
        AND ( title LIKE CONCAT("%",#{keyword},"%") OR 
              code LIKE CONCAT("%",#{keyword},"%") 
            )
    </if>
    <if test="type!=null">
        AND type = #{type}
    </if>
    <if test="sex!=null">
        AND JSON_CONTAINS (tag, CONCAT('"',#{sex},'"'))
    </if>
    <choose>
        <when test="priceType==1">
            AND current_price &gt;= 0 AND current_price &lt; 100
        </when>
        <when test="priceType==2">
            AND current_price &gt;= 100 AND current_price &lt; 500
        </when>
        <when test="priceType==3">
            AND current_price &gt;= 500 AND current_price &lt; 1000
        </when>
        <when test="priceType==4">
            AND current_price &gt;= 1000
        </when>
    </choose>
    <choose>
        <when test="orderType==1">
            ORDER BY id DESC
        </when>
        <when test="orderType==2">
            ORDER BY sales_volume DESC
        </when>
        <when test="orderType==3">
            ORDER BY currentPrice ASC
        </when>
        <when test="orderType==4">
            ORDER BY currentPrice DESC
        </when>
        <otherwise>
            ORDER BY sales_volume DESC, id DESC
        </otherwise>
    </choose>
    LIMIT #{start}, #{length}
</select>
<select id="searchListCount" parameterType="Map" resultType="long">
    SELECT COUNT(*)
    FROM tb_goods
    WHERE `status` = 1
    <if test="keyword!=null">
        AND ( title LIKE CONCAT("%",#{keyword},"%") OR 
              code LIKE CONCAT("%",#{keyword},"%") 
            )
    </if>
    <if test="type!=null">
        AND type = #{type}
    </if>
    <if test="sex!=null">
        AND JSON_CONTAINS (tag, CONCAT('"',#{sex},'"'))
    </if>
    <choose>
        <when test="priceType==1">
            AND current_price &gt;= 0 AND current_price &lt; 100
        </when>
        <when test="priceType==2">
            AND current_price &gt;= 100 AND current_price &lt; 500
        </when>
        <when test="priceType==3">
            AND current_price &gt;= 500 AND current_price &lt; 1000
        </when>
        <when test="priceType==4">
            AND current_price &gt;= 1000
        </when>
    </choose>
</select>
@Service("FrontGoodsServiceImpl")
@Slf4j
public class GoodsServiceImpl implements GoodsService {
    ……
    @Override
    public PageUtils searchListByPage(Map param) {
        ArrayList<HashMap> list = new ArrayList<>();
        long count = goodsDao.searchListCount(param);
        if (count > 0) {
            list = goodsDao.searchListByPage(param);
        }
        int page = MapUtil.getInt(param, "page");
        int length = MapUtil.getInt(param, "length");
        PageUtils pageUtils = new PageUtils(list, count, page, length);
        return pageUtils;
    }
}

直接从 searchListByPage 里取size不就可以了吗

而且这俩sql都蛮不简单的,单独查下count感觉也消耗不小


本人犯二。。。 是要单独写

正在回答

1回答

你这么说都把我惊到了,你想想看limit子句是干什么的

0 回复 有任何疑惑可以回复我~
  • 提问者 demonCry #1
    没懂。。。 是因为相对searchListByPage下面 searchListCount 的 count(*) 有什么优化吗?
    回复 有任何疑惑可以回复我~ 2023-08-23 00:01:01
  • 神思者 回复 提问者 demonCry #2
    sql语句要查询记录总数是多少,不是要查询一页有多少条记录
    回复 有任何疑惑可以回复我~ 2023-08-23 02:47:18
  • 提问者 demonCry 回复 神思者 #3
    嗷~ 我sb了。。。我咋想的。。。
    回复 有任何疑惑可以回复我~ 2023-08-23 13:52:54
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号