您的位置:首页 > 编程语言 > Java开发

实战 Java 第4天:开发信息发布、获取列表信息接口

2019-07-10 14:35 1616 查看

实战 Java 第4天:开发信息发布、获取列表信息接口

前言

在前面的《实战 Java 第3天》学习了如何开发注册和登录接口,今天开始编写信息发布和获取列表信息接口,本文的内容只是业务逻辑,完整的项目需结合前面的内容一起看。

一、创建 Product 商品实体类

信息发布接口需要用到的属性是商品名称 productName、商品价格 productPrice、商品类型 productType、商品图片 productImg、商品描述 productDes。

  • 在 entity 文件包下面新建 Product 类,来用抽象商品信息,内容如下:
package com.dingding.entity;

/**
* Created by xpwu on 2019/6/28.
*/
public class Product {
String productName;
String productPrice;
String productType;
String productImg;
String productDes;
public Product(String productName, String productPrice, String productType, String productImg, String productDes) {
this.productName = productName;
this.productPrice = productPrice;
this.productType = productType;
this.productImg = productImg;
this.productDes = productDes;
}
public String getProductName() {
return productName;
}

public void setProductName(String productName) {
this.productName = productName;
}

public String getProductPrice() {
return productPrice;
}

public void setProductPrice(String productPrice) {
this.productPrice = productPrice;
}

public String getProductType() {
return productType;
}

public void setProductType(String productType) {
this.productType = productType;
}

public String getProductImg() {
return productImg;
}

public void setProductImg(String productImg) {
this.productImg = productImg;
}

public String getProductDes() {
return productDes;
}

public void setProductDes(String productDes) {
this.productDes = productDes;
}
public Product(){

}
}
  • 由于要返回信息列表,所以在 Response 类下面新增 setResponse 方法。内容如下:
package com.dingding.entity;

/**
* Created by xpwu on 2019/6/28.
*/
public class Response {
Object result;
public Object getResult() {
return result;
}
public void setResult(Object result) {
this.result = result;
}
public void setResponse(Boolean isSuc,String msg, int code,Object result) {
this.msg = msg;
this.code = code;
this.isSuc = isSuc;
this.result = result;
}
}

二、创建 ProductService 服务类

根据业务场景,需要提供一个添加商品信息的服务与获取商品信息列表的服务。

  • 在 service 文件包下新建 ProductService 类编写接口,内容如下:
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(String productName, String productPrice, String productType,String productImg, String productDes);
List<Product> getProductList();
}
  • 在 impl 文件包下新建 ProductServiceImpl 类来实现 ProductService 接口。
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(String productName, String productPrice, String productType, String productImg, String productDes){
Product pro = new Product(productName,productPrice,productType,productImg,productDes);
int count = productMapper.addProduct(pro);
return count;
}
public List<Product> getProductList(){
List<Product> proList = productMapper.getProductList();
return  proList;
}
}

三、创建 ProductMapper 类

根据 service 服务类创建对应的 mapper 类。
在 mapper 文件包下新建 ProductMapper 类,内容如下:

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();
}

四、创建 sql 语句

在 resources/mapper 文件夹下新建相关的 ProductMapper.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.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="VARCHAR" property="productPrice" />
<result column="product_type" jdbcType="VARCHAR" 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 >
</mapper>

五、创建 ProductController 控制类(调用服务,处理业务逻辑)

在 controller 文件包下新建 ProductController 类,在此类中调用调用相关服务并处理逻辑,内容如下:

  • 添加商品信息接口的逻辑处理
  1. 判断商品名称、商品价格、商品类型,商品图片,商品描述是否为空。如果为空,添加失败;
  2. 如果都不为空,判断返回的 count 是否大于0,不大于0则添加失败;
  3. 大于0,添加成功。
  • 获取商品信息接口的逻辑处理
  1. 获取 list 返回到前端;
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.ArrayList;
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 Map<String,String> product){
String productName = product.get("productName");
String productPrice = product.get("productPrice");
String productType = product.get("productType");
String productImg = product.get("productImg");
String productDes = product.get("productDes");
System.out.println(productName);
System.out.println(productPrice);
System.out.println(productType);
System.out.println(productImg);
System.out.println(productDes);
if(productName!=null && productPrice!=null && productType!=null && productImg!=null && productDes!=null){
int count = productService.addProduct(productName,productPrice,productType,productImg,productDes);
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;
}
}

六、测试接口是否成功

  1. 使用 postman 验证接口。

七、总结

以下是需要注意的几点。

  1. 因为 productId 是主键,所以写 sql 插入语句的时候应该按照以下这种方式写,而不是直接插入 。并且记得在数据库表中将这个字段设置为主键,还要勾选递增。
INSERT INTO`product`(`product_name`,`product_price`,`product_type`,`product_img`,`product_des`) VALUES(#{productName},#{productPrice},#{productType},#{productImg},#{productDes})

  1. 在写 xml 文件创建 sql 语句时,返回的是 list,resultMap 属性值为 “BaseResultMap”。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐