实战 Java 第5天:开发商品查询(模糊查询与条件查询)接口

前言

在前面的《实战 Java 第4天》学习了如何开发发布信息和获取列表信息接口,今天开始编写两种业务场景下的商品查询接口,一种是关键字模糊查询,一种是条件查询。本文的内容只是业务逻辑,完整的项目需结合前面的内容一起看。

一、在 ProductService 类中添加接口

  • 在 ProductService 类中添加 getProductByKey 接口,实现按照商品名称和商品描述关键字模糊查询。添加 getProductByCondition 接口,实现商品名称和类型条件查询。
package com.dingding.service;
import com.dingding.entity.Product;
import java.util.List;

/**
 * Created by xpwu on 2019/7/10.
 */
public interface ProductService {
    int addProduct(Product product);
    List<Product> getProductList();
    List<Product> getProductByKey(String productName);
    List<Product> getProductByCondition(String productName,int productType);
}
  • 在 ProductServiceImpl 类中添加实现。
package com.dingding.service.impl;
import com.dingding.entity.Product;
import com.dingding.mapper.ProductMapper;
import com.dingding.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * Created by xpwu on 2019/7/10.
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    ProductMapper productMapper;
    public int addProduct(Product product){
        int count = 0;
        try {
            count = productMapper.addProduct(product);
        }catch (Exception err){
           System.out.println(err);
        }
        return count;
    }
    public List<Product> getProductList(){
        List<Product> proList = productMapper.getProductList();
        return  proList;
    }
    public List<Product> getProductByKey(String productName){
        List<Product> proList1 = productMapper.getProductByKey(productName);
        return  proList1;
    }
    public List<Product> getProductByCondition(String productName,int productType){
        List<Product> proList2 = productMapper.getProductByCondition(productName,productType);
        return  proList2;
    }
}

二、在 ProductMapper 类中添加接口

在 ProductMapper 类中添加 getProductByKey 接口和 getProductByCondition 接口。

package com.dingding.mapper;
import com.dingding.entity.Product;
import org.springframework.stereotype.Repository;
import java.util.List;

/**
 * Created by xpwu on 2019/7/10.
 */
@Repository
public interface ProductMapper {
    int addProduct(Product product);
    List<Product> getProductList();
    List<Product>getProductByKey(String productName);
    List<Product>getProductByCondition(String productName,int productType);
}

三、增加 sql 语句

添加 getProductByKey 与 getProductByCondition 的查询语句。

<?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.dingding.mapper.ProductMapper">
    <resultMap id="BaseResultMap" type="com.dingding.entity.Product">
        <result column="product_id" jdbcType="VARCHAR" property="productId" />
        <result column="product_name" jdbcType="VARCHAR" property="productName" />
        <result column="product_price" jdbcType="DOUBLE" property="productPrice" />
        <result column="product_type" jdbcType="INTEGER" property="productType" />
        <result column="product_img" jdbcType="VARCHAR" property="productImg" />
        <result column="product_des" jdbcType="VARCHAR" property="productDes" />
    </resultMap>
    <insert id="addProduct" parameterType="com.dingding.entity.Product">
        INSERT INTO `product` (`product_name`,`product_price`,`product_type`,`product_img`,`product_des`) VALUES(#{productName},#{productPrice},#{productType},#{productImg},#{productDes})
    </insert>
    <select id="getProductList" resultMap="BaseResultMap">
        SELECT * FROM `product`
    </select >
    <select id="getProductByKey" resultMap="BaseResultMap">
        SELECT * FROM `product` where product_name like concat('%',#{productName},'%') or product_des like  concat('%',#{productName},'%')
    </select >
    <select id="getProductByCondition" resultMap="BaseResultMap">
        SELECT * FROM `product`
        <where>
        <if test="productName != null and productName != ''">
            and product_name like concat('%',#{productName},'%')
        </if>
        <if test="productType != null and productType != -1">
            and product_type = #{productType}
        </if>
        </where>
    </select >
</mapper>

四、在 ProductController 类中添加业务逻辑

package com.dingding.controller;
import com.dingding.entity.Product;
import com.dingding.entity.Response;
import com.dingding.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;

/**
 * Created by xpwu on 2019/7/10.
 */
@RestController
public class ProductController {
    @Autowired
    ProductService productService;
    @RequestMapping(value = "/addProduct",method = RequestMethod.POST)
    public Response addProduct(@RequestBody Product product){
        if(product.getProductName()!=null && product.getProductPrice()!=0 && product.getProductType()!=0 && product.getProductImg()!=null && product.getProductDes()!=null){
            int count = productService.addProduct(product);
            if(count >  0){
                Response response = new Response(true,"添加成功",1);
                return response;
            }else {
                Response response = new Response(false,"添加失败",-1);
                return response;
            }
        }else {
            Response response = new Response(false,"有参数为空",-1);
            return response;
        }
    }
    @RequestMapping(value = "/getProductList",method = RequestMethod.POST)
    public Response getProductList(){
        Response response = new Response();
        List<Product> productList = productService.getProductList();
        response.setResponse(true,"查询成功",1,productList);
        return response;
    }
    @RequestMapping(value = "/getProductByKey",method = RequestMethod.POST)
    public Response getProductByKey(@RequestBody Map<String,String> product){
        String productName = product.get("productName");
        String productDes= product.get("productDes");
        if(productDes!=null){
           productName = productDes;
        }
        Response response = new Response();
        List<Product> productList = productService.getProductByKey(productName);
        response.setResponse(true,"查询成功",1,productList);
        return response;
    }
    @RequestMapping(value = "/getProductByCondition",method = RequestMethod.POST)
    public Response getProductByCondition(@RequestBody Product product){
        String productName = product.getProductName();
        int productType = product.getProductType();
        Response response = new Response();
        List<Product> productList = productService.getProductByCondition(productName,productType);
        response.setResponse(true,"查询成功",1,productList);
        return response;
    }
}

五、测试接口是否成功

  1. 使用 postman 验证接口。

六、总结

以下是需要注意的几点。

  1. 多字段模糊查询时,使用 like 关键字,需要通过 concat 在参数的前后拼接 %,并且需要与前端约定好字段。比如按照商品名称、商品描述模糊查询,就需要与前端约定好不管是商品名称还是商品描述,都使用 productName 作为关键字传过来,后端也用这个字段接收。
    sql 语句如下:
 SELECT * FROM `product` where product_name like concat('%',#{productName},'%') or product_des like  concat('%',#{productName},'%')
  1. 条件查询是用 if 语句,如果为数值类型,则判断是否为 -1,而不是空 “”。
    sql 语句如下:
 SELECT * FROM `product`
        <where>
        <if test="productName != null and productName != ''">
            and product_name like concat('%',#{productName},'%')
        </if>
        <if test="productType != null and productType != -1">
            and product_type = #{productType}
        </if>
        </where>
  • 5
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
查询Memcached中的所有key,可以使用`stats items`命令。这个命令将返回所有存储在Memcached中的item的统计信息,包括key和其他有用的信息。 具体步骤如下: 1. 连接到Memcached服务器。可以使用telnet或nc等工具,也可以使用Memcached客户端库连接。 2. 发送`stats items`命令。 3. 服务器会返回一些统计信息,其中包括`STAT items`和`STAT keys`。`STAT items`表示存储在Memcached中的所有item的数量,`STAT keys`表示所有key的数量。 4. 可以解析返回的信息,提取出所有的key。 下面是一个使用telnet查询所有key的示例: ```shell $ telnet localhost 11211 stats items STAT items:1:number 1 STAT items:1:age 120 STAT items:1:evicted 0 STAT items:1:evicted_nonzero 0 STAT items:1:evicted_time 0 STAT items:1:outofmemory 0 STAT items:1:tailrepairs 0 STAT items:1:reclaimed 0 STAT items:2:number 1 STAT items:2:age 120 STAT items:2:evicted 0 STAT items:2:evicted_nonzero 0 STAT items:2:evicted_time 0 STAT items:2:outofmemory 0 STAT items:2:tailrepairs 0 STAT items:2:reclaimed 0 STAT items:3:number 2 STAT items:3:age 120 STAT items:3:evicted 0 STAT items:3:evicted_nonzero 0 STAT items:3:evicted_time 0 STAT items:3:outofmemory 0 STAT items:3:tailrepairs 0 STAT items:3:reclaimed 0 STAT items:4:number 2 STAT items:4:age 120 STAT items:4:evicted 0 STAT items:4:evicted_nonzero 0 STAT items:4:evicted_time 0 STAT items:4:outofmemory 0 STAT items:4:tailrepairs 0 STAT items:4:reclaimed 0 END ``` 在这个例子中,Memcached中存储了4个item,它们的key分别为1、2、3和4。注意,这里的key并不是我们使用set命令时指定的那个字符串,而是Memcached内部生成的一个数字。但是这个数字也可以用来唯一地标识一个item。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值