实战 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;
    double productPrice;
    int productType;
    String productImg;
    String productDes;
    public Product(String productName, double productPrice, int 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 double getProductPrice() {
        return productPrice;
    }

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

    public int getProductType() {
        return productType;
    }

    public void setProductType(int 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(Product product);
    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(Product product){
        int count = productMapper.addProduct(product);
        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="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 >
</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.List;

/**
 * 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;
    }
}

六、测试接口是否成功

  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”。
    在这里插入图片描述
  • 18
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值