天天看點

7.查詢熱銷商品前四名的實作

1.資料庫

1 CREATE TABLE t_product (
 2   id int(20) NOT NULL COMMENT '商品id',
 3   category_id int(20) DEFAULT NULL COMMENT '分類id',
 4   item_type varchar(100) DEFAULT NULL COMMENT '商品系列',
 5   title varchar(100) DEFAULT NULL COMMENT '商品标題',
 6   sell_point varchar(150) DEFAULT NULL COMMENT '商品賣點',
 7   price bigint(20) DEFAULT NULL COMMENT '商品單價',
 8   num int(10) DEFAULT NULL COMMENT '庫存數量',
 9   image varchar(500) DEFAULT NULL COMMENT '圖檔路徑',
10   status int(1) DEFAULT '1' COMMENT '商品狀态  1:上架   2:下架   3:删除',
11   priority int(10) DEFAULT NULL COMMENT '顯示優先級',
12   created_time datetime DEFAULT NULL COMMENT '建立時間',
13   modified_time datetime DEFAULT NULL COMMENT '最後修改時間',
14   created_user varchar(50) DEFAULT NULL COMMENT '建立人',
15   modified_user varchar(50) DEFAULT NULL COMMENT '最後修改人',
16   PRIMARY KEY (id)
17 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;      

Product資料表

2.product實體類

package com.ku.store.entity;

import lombok.Data;

@Data
public class Product extends BaseEntity{
    private Integer id;
    private Integer categoryId;//類别id
    private String itemType;//項目
    private String title;
    private String sellPoint;//銷售點
    private Long price;
    private Integer num;
    private String image;
    private Integer status;
    private Integer priority;//優先權      

Product實體類

3.mapper層

  3.1.接口實作

/**
     * 查詢熱銷商品前四名
     * @return 熱銷商品前四名的集合
     */
    List<Product> findHostList();      

  3.2.接口的xml實作

<resultMap id="ProductEntityMap" type="com.ku.store.entity.Product">
        <id column="id" property="id"/>
        <result column="category_id" property="categoryId"/>
        <result column="item_type" property="itemType"/>
        <result column="sell_point" property="sellPoint"/>
        <result column="created_user" property="createdUser"/>
        <result column="created_time" property="createdTime"/>
        <result column="modified_user" property="modifiedUser"/>
        <result column="modified_time" property="modifiedTime"/>
    </resultMap>

    <!-- 查詢熱銷商品的前四名:List<Product> findHostList() -->
    <select id="findHostList" resultMap="ProductEntityMap">
        select
            *
        from
            store.t_product
        where
            status = 1
        order by
            priority desc
            limit 0,4
    </select>      

ProductMapper.xml

  3.3.測試接口方法

@Autowired
    ProductMapper productMapper;
    @Test
    public void findHostList(){
        List<Product> hostList = productMapper.findHostList();
        System.out.println("count:"+hostList.size());
        for (Product product : hostList) {
            System.out.println(product);
        }
    }      

  3.4.測試結果

7.查詢熱銷商品前四名的實作

4.service層

  4.1.接口實作

List<Product> getHostList();      

  4.2.接口實作類

@Service
public class ProductServiceImpl implements IProductService {

    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<Product> getHostList() {
        List<Product> list = productMapper.findHostList();
        for (Product product : list) {
            product.setCreatedUser(null);
            product.setCreatedTime(null);
            product.setModifiedUser(null);
            product.setModifiedTime(null);
        }
        return list;
    }
}      

ProdoctServiceImpl

  4.3.實作類測試

@Autowired
    IProductService productService;

    @Test
    public void getHostList(){
        try {
            List<Product> list = productService.getHostList();
            System.out.println("count=" + list.size());
            for (Product item : list) {
                System.out.println(item);
            }
        } catch (ServiceException e) {
            System.out.println(e.getClass().getSimpleName());
            System.out.println(e.getMessage());
        }
    }      

ProductServiceTests

  4.4.測試結果

7.查詢熱銷商品前四名的實作

5.controller層

  5.1.增強BaseController

  5.2.實作ProductController

@RestController
@RequestMapping("/products")
public class ProductController extends BaseController{
    @Autowired
    private IProductService productService;

    @GetMapping("hot_list")
    public JsonResult<List<Product>> getHotList(){
        List<Product> data = productService.getHostList();
        return new JsonResult<>(OK, data);
    }
}      

ProductController

  5.3.postman測試

   

7.查詢熱銷商品前四名的實作
上一篇: swustoj 0259
下一篇: 網絡思考