1创建数据库
2.创建实体类Product
public class Product extends BaseEntity implements Serializable {
private Integer id;
private Integer categoryId;
private String itemType;
private String title;
private String sellPoint;
private Long price;
private Integer num;
private String image;
private Integer status;
private Integer priority;
3 创建持久层
3.1规划需要执行的SQL语句
select * from t_product where status=1 order by priority DESC LIMIT 0,4
3.2 接口与抽象方法
/**
* 热销商品查询
* @return 前四的热销商品
*/
List<Product> findHotList();
3.3 配置SQL映射
<?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">
<!-- namespace属性:用于指定当前的映射文件和哪个接口进行映射,需要指定接口的文件路径,需要标注包的完整路径 -->
<mapper namespace="com.cy.store.mapper.ProductMapper">
<resultMap id="ProductEntityMap" type="com.cy.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>
<result column="created_time" property="createdTime"></result>
<result column="modified_user" property="modifiedUser"></result>
<result column="modified_time" property="modifiedTime"></result>
</resultMap>
<select id="findHotList" resultMap="ProductEntityMap">
select * from t_product where status=1 order by
priority DESC limit 0,4
</select>
</mapper>
4 业务层
4.1 规划异常
无异常
4.2 接口与抽象方法
package com.cy.store.service;
import com.cy.store.entity.Product;
import java.util.List;
public interface IProductService {
List<Product> findHotList();
}
4.3 实现抽象方法
package com.cy.store.service.impl;
import com.cy.store.entity.Product;
import com.cy.store.mapper.ProductMapper;
import com.cy.store.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
public class IProductServiceImpl implements IProductService {
@Autowired
private ProductMapper productMapper;
@Override
public List<Product> findHotList() {
List<Product> list = productMapper.findHotList();
for (Product product : list) {
product.setPriority(null);
product.setCreatedUser(null);
product.setCreatedTime(null);
product.setModifiedTime(null);
product.setModifiedUser(null);
}
return list;
}
}
5. 控制器
5.1 处理异常
无异常
5.2 请求设计
/products/hot_list
get
JsonResult<Product>
加入白名单
5.3 处理请求
package com.cy.store.controller;
import com.cy.store.entity.Product;
import com.cy.store.service.IProductService;
import com.cy.store.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("products")
public class ProductsController extends BaseController{
@Autowired
private IProductService productService;
@RequestMapping("hot_list")
public JsonResult<List<Product>> getHostList(){
List<Product> list = productService.findHotList();
return new JsonResult<>(OK,list);
}
}