实现商品搜索功能

  • 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 == &quot;c&quot; ">
                i.sell_counts desc
            </when>
            <when test="paramsMap.sort == &quot;p&quot; ">
                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);
    }

    
}

 

好的,那我来为您简单介绍一下这些功能实现方式: 1. 实现用户注册功能:在前端页面上,需要提供一个注册表单,用户填写完信息后点击提交按钮,前端将表单数据发送到后端接口进行处理,后端将用户信息保存至数据库中。 2. 实现用户登录功能:在前端页面上,需要提供一个登录表单,用户填写完账号密码后点击提交按钮,前端将表单数据发送到后端接口进行验证,后端将验证结果返回给前端,前端根据结果进行相应的处理。 3. 实现购物车功能:在前端页面上,需要提供一个购物车页面,用户可以在商品列表中选择需要购买的商品,点击添加至购物车按钮,前端将商品信息保存至本地存储中。用户在购物车页面中可以查看已添加的商品信息,修改商品数量、删除商品等操作,前端将修改后的购物车信息发送至后端接口进行处理。 4. 实现商品分类查询功能:在前端页面上,需要提供一个商品分类列表,用户可以选择所需的分类,前端将用户选择的分类信息发送至后端接口进行处理,后端返回符合条件的商品列表给前端进行展示。 5. 实现商品搜索功能:在前端页面上,需要提供一个搜索框,用户可以输入关键字进行搜索,前端将搜索关键字发送至后端接口进行处理,后端返回符合条件的商品列表给前端进行展示。 以上就是实现这些功能的基本方式,具体的实现过程和技术选型要根据具体的需求和情况进行选择。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值