<?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"><mappernamespace="co.dongchen.shop.mapper.ProductMapper"><sqlid="productFields">
p.id, p.created_at, p.updated_at, p.sku, p.name, p.stock, p.price, p.is_enabled, p.images, p.description, p.user_id
</sql><sqlid="whereCondition"><where><iftest="productCondition.id != null and productCondition.id != 0">
and p.id = #{productCondition.id}
</if><iftest="productCondition.isEnabled != null and productCondition.isEnabled == true">
and p.is_enabled = 1
</if><iftest="productCondition.isEnabled != null and productCondition.isEnabled == false">
and p.is_enabled = 0
</if><iftest="productCondition.name != null and productCondition.name != ''"><bindname="likeName"value="'%' + productCondition.name + '%'"/>
and p.name like #{likeName}
</if><iftest="productCondition.ids != null and productCondition.ids.size() > 0 ">
and p.id in
<foreachcollection="productCondition.ids"item="item"index="index"open="("separator=","close=")">
#{item}
</foreach></if></where></sql><selectid="queryPagedProducts"resultType="product">
select <includerefid="productFields"/>
from product p
<includerefid="whereCondition"/><!-- NEWLY ADDED CODE BEGIN --><choose><whentest="productCondition.orderBy != null and productCondition.orderBy == 'created_at'">
order by p.created_at desc
</when></choose><!-- NEWLY ADDED CODE END --><iftest="pageHelperCondition.offSet != null and pageHelperCondition.limit != null">
limit #{pageHelperCondition.offSet},#{pageHelperCondition.limit}
</if><iftest="pageHelperCondition.offSet == null and pageHelperCondition.limit != null">
limit #{pageHelperCondition.limit}
</if></select><selectid="queryPagedCount"resultType="long">
select count(id)
from product p
<includerefid="whereCondition"/></select></mapper>
Update TopProductService
Replace the original content with the following one in the
TopProductService.java file under the
service/src/main/java/co.dongchen.shop/service directory:
packageco.dongchen.shop.service;importco.dongchen.shop.common.model.Product;importco.dongchen.shop.common.util.PageHelper;importcom.google.common.collect.Lists;importcom.google.common.collect.Ordering;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importredis.clients.jedis.Jedis;importjava.util.List;importjava.util.Set;importjava.util.stream.Collectors;@ServicepublicclassTopProductService{privatestaticfinalStringTOP_PRODUCT_KEY="TOP_PRODUCT_KEY";@AutowiredprivateProductServiceproductService;publicvoidincrease(Longid){Jedisjedis=newJedis("127.0.0.1");// Adds the score value of the member named the product id in the TOP_PRODUCT_KEY sorted set to one and returns the new score value of the member.
jedis.zincrby(TOP_PRODUCT_KEY,1.0D,String.valueOf(id));// Removes members after the fifth of the top five ranked members in the TOP_PRODUCT_KEY sorted set and returns the number of removed members.
jedis.zremrangeByRank(TOP_PRODUCT_KEY,5,-1);jedis.close();}privateList<Long>getTopIds(){Jedisjedis=newJedis("127.0.0.1");// Retrieve all members of the sorted set named TOP_PRODUCT_KEY.
Set<String>ids=jedis.zrevrange(TOP_PRODUCT_KEY,0,-1);jedis.close();returnids.stream().map(Long::parseLong).collect(Collectors.toList());}publicList<Product>getTopProducts(intsize){ProductproductCondition=newProduct();List<Long>topIds=getTopIds();// Retrieve minimum available products from the beginning.
topIds=topIds.subList(0,Math.min(topIds.size(),size));if(topIds.isEmpty()){returnLists.newArrayList();}productCondition.setIds(topIds);finalList<Long>orderedIds=topIds;List<Product>products=productService.queryWithImgs(productCondition,PageHelper.init(size,1));Ordering<Product>productOrdering=Ordering.natural().onResultOf(ps->{returnorderedIds.indexOf(ps.getId());});returnproductOrdering.sortedCopy(products);}// NEWLY ADDED CODE BEGIN
publicList<Product>getNewArrivals(){ProductproductCondition=newProduct();productCondition.setOrderBy("created_at");returnproductService.queryWithImgs(productCondition,PageHelper.init(6,1));}// NEWLY ADDED CODE END
}
Create HomeController
Right click
controller/src/main/java/co.dongchen.shop/controller
directory: New > Java Class
Fill in “HomeController”
Click “OK” button
Copy and paste the following content into HomeController.java: