- springboot、MyBatis框架
- RESTful风格
- PageHelper分页插件
编写流程:
1.写好sql及mapper.xml
<?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" >
<mapper namespace="com.zkx.mapper.ItemsMapperCustom" >
<select id="searchItems" parameterType="Map" resultType="com.zkx.pojo.vo.SearchItemsVO">
SELECT
i.id AS itemId,
i.item_name AS itemName,
i.sell_counts AS sellCounts,
ii.url AS imgUrl,
tempSpec.price_discount AS price
FROM
items i
LEFT JOIN
items_img ii
ON
i.id = ii.item_id
LEFT JOIN
(SELECT item_id,MIN(price_discount) AS price_discount from items_spec GROUP BY item_id) tempSpec
ON i.id = tempSpec.item_id
WHERE
ii.is_main = 1
<if test="paramsMap.keywords != null and paramsMap.keywords != ''">
AND i.item_name like '%${paramsMap.keywords}%'
</if>
order by
<choose>
<when test="paramsMap.sort == "c" ">
i.sell_counts desc
</when>
<when test="paramsMap.sort == "p" ">
tempSpec.price_discount asc
</when>
<otherwise>
i.item_name asc
</otherwise>
</choose>
</select>
</mapper>
2.mapper.java接口
package com.zkx.mapper;
import com.zkx.pojo.vo.ItemCommentVO;
import com.zkx.pojo.vo.SearchItemsVO;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
public interface ItemsMapperCustom {
public List<SearchItemsVO> searchItems(@Param("paramsMap") Map<String,Object> map);
}
3.vo类
package com.zkx.pojo.vo;
/**
* 用于展示商品搜索结果的VO
*/
public class SearchItemsVO {
private String itemId;
private String itemName;
private int sellCounts;
private String imgUrl;
private int price;
public String getItemId() {
return itemId;
}
public void setItemId(String itemId) {
this.itemId = itemId;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public int getSellCounts() {
return sellCounts;
}
public void setSellCounts(int sellCounts) {
this.sellCounts = sellCounts;
}
public String getImgUrl() {
return imgUrl;
}
public void setImgUrl(String imgUrl) {
this.imgUrl = imgUrl;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
4.service接口
package com.zkx.service;
import com.zkx.pojo.Items;
import com.zkx.pojo.ItemsImg;
import com.zkx.pojo.ItemsParam;
import com.zkx.pojo.ItemsSpec;
import com.zkx.pojo.vo.CommentLevelCountsVO;
import com.zkx.pojo.vo.ItemCommentVO;
import com.zkx.utils.PagedGridResult;
import java.util.List;
public interface ItemService {
/**
* 搜索商品列表
* @param keywords
* @param sort
* @param page
* @param pageSize
* @return
*/
public PagedGridResult searchItems(String keywords,String sort,
Integer page,Integer pageSize);
}
5.service接口实现类
package com.zkx.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zkx.enums.CommentLevel;
import com.zkx.mapper.*;
import com.zkx.pojo.*;
import com.zkx.pojo.vo.CommentLevelCountsVO;
import com.zkx.pojo.vo.ItemCommentVO;
import com.zkx.pojo.vo.SearchItemsVO;
import com.zkx.service.ItemService;
import com.zkx.utils.DesensitizationUtil;
import com.zkx.utils.PagedGridResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class ItemServiceImpl implements ItemService {
@Autowired
private ItemsMapperCustom itemsMapperCustom;
private PagedGridResult setterPagedGrid(List<?> list,Integer page){
PageInfo<?> pageList = new PageInfo<>(list);
PagedGridResult grid = new PagedGridResult();
grid.setPage(page);
grid.setRows(list);
grid.setTotal(pageList.getPages());
grid.setRecords(pageList.getTotal());
return grid;
}
@Transactional(propagation = Propagation.SUPPORTS)
@Override
public PagedGridResult searchItems(String keywords, String sort, Integer page, Integer pageSize) {
Map<String,Object> map = new HashMap<>();
map.put("keywords",keywords);
map.put("sort",sort);
PageHelper.startPage(page,pageSize);
List<SearchItemsVO> list = itemsMapperCustom.searchItems(map);
return setterPagedGrid(list,page);
}
}
6.controller
package com.zkx.controller;
import com.zkx.pojo.Items;
import com.zkx.pojo.ItemsImg;
import com.zkx.pojo.ItemsParam;
import com.zkx.pojo.ItemsSpec;
import com.zkx.pojo.vo.CommentLevelCountsVO;
import com.zkx.pojo.vo.ItemInfoVO;
import com.zkx.service.ItemService;
import com.zkx.utils.IMOOCJSONResult;
import com.zkx.utils.PagedGridResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Api(value = "商品接口",tags = {"商品信息展示的相关接口"})
@RestController
@RequestMapping("items")
public class ItemsController extends BaseController {
@Autowired
private ItemService itemService;
@ApiOperation(value = "搜索商品列表" ,notes = "搜索商品列表",httpMethod = "GET")
@GetMapping("/search")
public IMOOCJSONResult search(
@ApiParam(name = "keywords",value = "关键字",required = true)
@RequestParam String keywords,
@ApiParam(name = "sort",value = "排序",required = false)
@RequestParam String sort,
@ApiParam(name = "page",value = "查询下一页的第几页",required = false)
@RequestParam Integer page,
@ApiParam(name = "pageSize",value = "分页的每一页显示的条数",required = false)
@RequestParam Integer pageSize){
if(StringUtils.isBlank(keywords)){
return IMOOCJSONResult.errorMsg(null);
}
if(page == null){
page = 1;
}
if(pageSize == null){
pageSize = PAGE_SIZE;
}
PagedGridResult grid = itemService.searchItems(keywords,
sort,
page,
pageSize);
return IMOOCJSONResult.ok(grid);
}
}