实战 Java 第8天:开发商品详情查询接口

前言

在前面的《实战 Java 第5天》学习了如何开发商品查询(模糊查询与条件查询)接口,今天开始编写商品详情查询接口。本文的内容只是业务逻辑,完整的项目需结合前面的内容一起看。

一、在 ProductService 类中添加接口

  • 在 ProductService 类中添加 getProductDetailById 接口,实现根据商品 ID 查询商品详情。
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);
    int updateProduct(@Param("pro") Product product);
    int deleteProduct(int productId);
    Product getProductDetailById(int productId);
}
  • 在 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;
    }
    public int updateProduct(Product product){
        int count = 0;
        try {
            count = productMapper.updateProduct(product);
        }catch (Exception err){
            System.out.println(err);
        }
        return count;
    }
    public int deleteProduct(int productId){
        int count = 0;
        try {
            count = productMapper.deleteProduct(productId);
        }catch (Exception err){
            System.out.println(err);
        }
        return count;
    }
    public Product getProductDetailById(int productId) {
		return productMapper.getProductDetailById(productId);
	}
}

二、在 ProductMapper 类中添加接口

在 ProductMapper 类中添加 getProductDetailById 接口。

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);
    int updateProduct(@Param("pro") Product product);
    int deleteProduct(int productId);
    Product getProductDetailById(int productId);
}

三、增加 sql 语句

添加 getProductDetailById 的查询语句。

<?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>
    <update id="updateProduct"  parameterType="com.dingding.entity.Product">
        update product
        <trim prefix="SET" suffixOverrides=",">
            <if test="null != pro.productName and '' != pro.productName">
                product_name=#{pro.productName},
            </if>
            <if test="null != pro.productType and -1!= pro.productType">
                product_type=#{pro.productType},
            </if>
            <if test="null != pro.productPrice and -1!= pro.productPrice">
                product_price=#{pro.productPrice},
            </if>
            <if test="null != pro.productImg and '' != pro.productImg">
                product_img=#{pro.productImg},
            </if>
            <if test="null != pro.productDes and '' != pro.productDes">
                product_des=#{pro.productDes},
            </if>
        </trim>
        where product_id=#{pro.productId}
</update>
    <delete id="deleteProduct">
        DELETE FROM product WHERE product_id = #{productId}
    </delete>
    <select id="getProductDetailById" resultMap="BaseResultMap">
    	select * from `product` where product_id = #{productId} limit 1
    </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;
    }
    @RequestMapping(value = "/updateProduct",method = RequestMethod.POST)
    public Response updateProduct(@RequestBody Product product){
        int productId = product.getProductId();
        if(productId!=0){
            int count = productService.updateProduct(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,"请传入商品id",-1);
            return  response;
        }
    }
    @RequestMapping(value = "/deleteProduct",method = RequestMethod.POST)
    public Response deleteProduct(@RequestBody Product product){
        int productId = product.getProductId();
        if(productId!=0){
            int count = productService.deleteProduct(productId);
            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,"删除失败,请传入商品id",-1);
            return response;
        }
    }
    @RequestMapping(value = "/getProductDetailById",method = RequestMethod.GET)
    public Response getProductDetailById(@RequestParam("productId") Integer productId){
        Response response = new Response();
        Product product = productService.getProductDetailById(productId);
        response.setResponse(true,"查询成功",1,product);
        return response;
    }
}

五、测试接口是否成功

  1. 使用 postman 验证接口。
  • 验证商品详情查询接口
    1)选择请求方式为 GET, 在地址栏中输入 http://localhost:8080/getProductDetailById?productId=1 。
    在这里插入图片描述
    2)查看数据库数据是否相符。
    在这里插入图片描述

六、总结

查询商品详情时,接口的接收对象为 Product 实体,为了保证 mybatis 能正常接收查询结果,建议在sql后面加上 limit 1,保证查询结果只有一条。(查询条件为表主键时,主键具有唯一性,可以不需要加 limit 1)。
sql 语句如下:

<select id="getProductDetailById" resultMap="BaseResultMap">
    	select * from `product` where product_id = #{productId} limit 1
    </select>
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值