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

  • 前言
  • 一、在 ProductService 类中添加接口
  • 二、在 ProductMapper 类中添加接口
  • 三、增加 sql 语句
  • 四、在 ProductController 类中添加业务逻辑
  • 五、测试接口是否成功
  • 六、总结

前言

在前面的《实战 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 {@AutowiredProductMapper 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 {@AutowiredProductService 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>

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

  1. 实战 Java 第10天:商品分页查询

    实战 Java 第10天:商品分页查询 前言 一.添加pagehelper相关依赖 二.在 ProductService 类中添加接口 三.在 ProductMapper 类中添加接口 四.增加 sq ...

  2. java 商城 商品查询_Javaweb网上商城项目实战(17)实现商品详情查询

    原理分析 具体实现 下面是商品详情页面product_info.jsp显示的样子,我们最初的模板的静态资源已经写死了, 这里我们需要先对这个页面进行改造,使得到时候主页点击商品能输出对应的商品详情页面 ...

  3. vue实战(11):开发店铺详情(二)

    懒癌发作-- 本篇有关于列表滚动的内容,有点复杂,好好分析 0. 其它 vue实战(1):准备与资料整理 vue实战(2):初始化项目.搭建底部导航路由 vue实战(3):底部导航显示.搭建各模块静态 ...

  4. 首选电商淘宝商品详情API接口(数据获取)

    API接口是什么?         API API全称是:Application Programming Interface,即:应用程序接口.开发人员可以使用这些API接口进行编程开发,而又无需访问 ...

  5. 谷粒商城 Day05 商品详情页接口准备

    Day05 商品详情页接口准备 一.Thymeleaf 1.thymeleaf 简介 ① HelloController com.atguigu.thymeleaf.controller.HelloC ...

  6. 阿里巴巴商品详情API接口(item_get-获得商品详情接口),阿里巴巴API接口

    阿里巴巴商品详情API接口(item_get-获得商品详情接口),阿里巴巴API接口可获取到商品链接,商品ID,商品标题,商品价格,品牌名称,店铺昵称,sku规格,sku属性,发货地,详情属性,店铺信 ...

  7. 小红书商品详情API接口(商品详情页面数据接口)

    小红书商品详情API接口(商品详情页面数据接口)代码对接如下: 1.公共参数 名称 类型 必须 描述 key String 是 调用key(必须以GET方式拼接在URL中,点击获取请求key和secr ...

  8. 谷粒商城高级篇(38)——异步编排之商品详情查询

    异步编排之商品详情查询 异步编排 CompletableFuture介绍 创建异步对象 计算完成时回调方法 handle 方法 线程串行化方法 两任务组合 全部完成 一个完成即可 多任务组合 业务描述 ...

  9. aliexpress商品详情API接口(速卖通商品详情页面数据接口)

    aliexpress商品详情API接口(速卖通商品详情页面数据接口)代码对接如下: 1.公共参数 名称 类型 必须 描述 key String 是 调用key(必须以GET方式拼接在URL中,点击获取 ...

最新文章

  1. Python3中的内置函数总结
  2. 轻松看懂java设计模式简单工厂模式
  3. 操作系统课设——吃水果问题
  4. 一位java大牛10年资料总结
  5. mysql 用户管理表_Mysql—用户表详解(mysql.user)
  6. 80.简单搭建nodeJS服务,访问本地站点文件
  7. 服务器无限关机重启,服务器反复关机重启
  8. 学习Spring(四) -- Spring的继承与依赖
  9. html email template
  10. 仓库进销存管理软件系统如何更换电脑使用
  11. Java视频教程免费分享(网盘直接取)
  12. Vulkan::0.0::开始于VulKanSDK(Getting Started with the Vulkan SDK)
  13. CMD中可执行的结束进程命令
  14. $ is not defined
  15. c++ 的绝对值函数
  16. php 生成单色位图,使用PHP实现将jpg/png转成.wbmp/.bmp格式图片后再转为16进制字符串(单色位图取模)...
  17. 第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(上海)G Fibonacci
  18. LTE PWS CMAS CBS消息
  19. sap客户信贷_SAP 客户信贷重建一则
  20. setTimeout/setIntervel 的类型定义

热门文章

  1. 2019年最值得学习的编程语言TOP5
  2. mongo启动报错:ERROR: child process failed, exited with error number 1
  3. NET Namespace(1)
  4. java 将对象写入链表_在Java中,_____类可用于创建链表数据结构的对象。
  5. win7 解决飞秋无法接收文件(准备接收)问题
  6. Beyond Compare下载安装
  7. 农夫追牛(bfs算法)
  8. 解决eclipse debug运行项目时下一步按钮一直为灰色不可用的问题
  9. 矩阵树定理--luoguP4208 [JSOI2008]最小生成树计数
  10. 解决androidstudio unable to delete directory的办法