sprinboot商品信息管理的简单功能接口


本文记录这两天学习springboot写的一些接口,便于加强记忆及以后查阅。

搭建springboot环境

可查看杏子姐链接:

商品实体类

package com.example.demo.entity;

import lombok.Data;
import lombok.experimental.Accessors;

/**
 * 商品实体类
 *
 * @param null
 * @author zhouquan
 * @return
 * @create 2020/4/24 15:40
 */
@Data
@Accessors(chain = true)
public class Product {
    /**
     *商品ID
     */
    String id;

    /**
     *商品名称
     */
    String productName;

    /**
     *商品价格
     */
    Double productPrice;

    /**
     *商品类型
     */
    int productType;

    /**
     *商品图片
     */
    String productImg;

    /**
     *商品描述
     */
    String productDoc;
}

还有一个返回信息的实体类

package com.example.demo.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**  *
 * 返回接口调用信息描述
 *
 * @param null
 * @author zhouquan
 * @return
 * @create 2020/4/23 18:13
 */
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Response {
    String msg;
    int code;
    Boolean isSuc=true;
    Object result;

    /**
     * 用户登录注册时用到的构造方法
     * @param msg
     * @param code
     * @param isSuc
     */
    public Response(String msg,int code,Boolean isSuc){
        this.msg = msg;
        this.code = code;
        this.isSuc = isSuc;
    }
    /** *
     * 返回List等信息
     *
     * @param msg
     * @param code
     * @param isSuc
     * @param result
     * @author zhouquan
     * @return void
     * @create 2020/4/24 22:56
     */
    public void setResponse(String msg,int code,boolean isSuc,Object result){
        this.msg = msg;
        this.isSuc = isSuc;
        this.code = code;
        this.result = result;
    }
}

service接口

package com.example.demo.service;

import com.example.demo.entity.Product;

import java.util.List;

/**
 * 商品接口
 *
 * @param null
 * @author zhouquan
 * @return
 * @create 2020/4/24 16:31
 */
public interface ProductService {

    int addProduct(Product product);

    List<Product> getProductList();

    List<Product> getProductByKey(String productName);

    List<Product> getProductByCondition(String productName,String productType);

    int updateProdect(Product product);
}

service 实现类

package com.example.demo.service.impl;

import cn.hutool.core.util.IdUtil;
import com.example.demo.entity.Product;
import com.example.demo.mapper.ProductMapper;
import com.example.demo.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
/** *
 * 商品相关功能实现类
 *
 * @param null
 * @author zhouquan
 * @return
 * @create 2020/4/24 16:37
 */
public class ProductServiceImpl implements ProductService {
    @Autowired
    ProductMapper productMapper;
    /**
     * 发布商品服务
     *
     * @param product
     * @author zhouquan
     * @return int
     * @create 2020/4/25 10:09
     */
    @Override
    public int addProduct(Product product) {
        String uuid = IdUtil.simpleUUID();
        product.setId(uuid);
        int count = productMapper.addProduct(product);
        return count;
    }

    /** *
     * 查询所有商品
     *
     * @author zhouquan
     * @return java.util.List<com.example.demo.entity.Product>
     * @create 2020/4/25 10:10
     */
    @Override
    public List<Product> getProductList() {
        List<Product> list = productMapper.getProductList();
        return list;
    }

    /** *
     * 根据商品名称查询商品
     *
     * @param productName
     * @author zhouquan
     * @return java.util.List<com.example.demo.entity.Product>
     * @create 2020/4/25 16:23
     */
    @Override
    public List<Product> getProductByKey(String productName) {
        List<Product> list = productMapper.getProductByKey(productName);
        return list;
    }

    /** *
     * 根据商品名称、商品种类查询商品
     *
     * @param productName
     * @param productType
     * @author zhouquan
     * @return java.util.List<com.example.demo.entity.Product>
     * @create 2020/4/25 16:24
     */
    @Override
    public List<Product> getProductByCondition(String productName, String productType) {
        List<Product> list = productMapper.getProductCondition(productName,productType);
        return list;
    }

    /** *
     * 更新商品信息
     *
     * @param product
     * @author zhouquan
     * @return int
     * @create 2020/4/25 16:24
     */
    @Override
    public int updateProdect(Product product) {
        int count = 0;
        try{
            count = productMapper.updateProdect(product);
        }catch (Exception ex){
            System.out.println(ex);
        }
        return count;
    }
}

mapper接口

package com.example.demo.mapper;

import com.example.demo.entity.Product;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/** *
 * 商品mapper接口
 *
 * @param null
 * @author zhouquan
 * @return
 * @create 2020/4/25 15:16
 */
@Repository
public interface ProductMapper {
    List<Product> getProductList();

    int addProduct(Product product);

    List<Product> getProductByKey(String productName);

    List<Product> getProductCondition(@Param("productName") String productName, @Param("productType") String productType);

    int updateProdect(Product product);
}

mapper.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.example.demo.mapper.ProductMapper">
    <insert id="addProduct" parameterType="com.example.demo.entity.Product">
        INSERT INTO product (id, productName, productPrice, productType, productImg, productDoc)
        VALUES (#{id}, #{productName}, #{productPrice}, #{productType}, #{productImg}, #{productDoc})
    </insert>

    <select id="getProductList" resultType="com.example.demo.entity.Product">
        SELECT *
        FROM product
    </select>

    <select id="getProductByKey" resultType="com.example.demo.entity.Product" parameterType="String">
        select *
        from product
        where productName like CONCAT('%', #{productName}, '%')
    </select>

    <select id="getProductCondition" resultType="com.example.demo.entity.Product" parameterType="String">
        SELECT *
        FROM product
        where productName like concat('%', #{productName}, '%') or productType like concat('%', #{productType}, '%')
    </select>

    <update id="updateProdect" parameterType="com.example.demo.entity.Product">
        update product
        <trim prefix="SET" suffixOverrides=",">
            <if test="null != productName and '' != productName">
                productName=#{productName},
            </if>
            <if test="null != productType and -1 != productType">
                productType=#{productType},
            </if>
            <if test="null != productPrice and -1 != productPrice">
                productPrice=#{productPrice},
            </if>
            <if test="null != productImg and '' != productImg">
                productImg=#{productImg},
            </if>
            <if test="null != productDoc and '' != productDoc">
                productDoc=#{productDoc},
            </if>
        </trim>
        where id=#{id}
    </update>
</mapper>

controller类

package com.example.demo.controller;

import com.example.demo.entity.Product;
import com.example.demo.entity.Response;
import com.example.demo.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;

/** *
 * 商品相关功能类
 *
 * @param null
 * @author zhouquan
 * @return
 * @create 2020/4/25 14:52
 */
@RestController
public class ProductController {
    @Autowired
    ProductService service;

    /**
     * 添加商品服务入口
     *
     * @param product
     * @return com.example.demo.entity.Response
     * @author zhouquan
     * @create 2020/4/24 22:11
     */
    @RequestMapping(value = "/addProduct", method = RequestMethod.POST)
    public Response addProduct(@RequestBody Product product) {
        if (product.getProductName() != null && product.getProductPrice() != null && product.getProductType() != 0) {
            int count = service.addProduct(product);
            if (count > 0) {
                Response response = new Response("添加成功", 1, true);
                return response;
            } else {
                Response response = new Response("添加失败", -1, false);
                return response;
            }
        } else {
            Response response = new Response("有参数为空,请填入后提交", -1, false);
            return response;
        }
    }

    /**
     * 获取所有商品接口
     *
     * @return com.example.demo.entity.Response
     * @author zhouquan
     * @create 2020/4/25 10:21
     */
    @RequestMapping(value = "/getProductList", method = RequestMethod.GET)
    public Response getProductList() {
        Response response = new Response();
        List<Product> list = service.getProductList();
        response.setResponse("查询成功", 1, true, list);
        return response;
    }

    /** *
     * 条件查询
     *
     * @param para
     * @author zhouquan
     * @return com.example.demo.entity.Response
     * @create 2020/4/25 14:45
     */
    @RequestMapping(value = "/getProductByKey", method = RequestMethod.POST)
    public Response getProductByKey(@RequestBody Map<String, String> para) {
        String productName = para.get("productName");
        String productTye = para.get("productType");
        Response response = new Response();
        if (productTye != null && productTye != "" || productName != null && productName != "") {
            List<Product> list = service.getProductByCondition(productName, productTye);
            if (list.size() > 0) {
                response.setResponse("查询成功", 1, true, list);
                return response;
            } else {
                response.setResponse("未查询到结果", -1, false, list);
                return response;
            }
        } else {
            List<Product> list = service.getProductByKey(productName);
            if (productName != null && productName != "") {
                if (list.size() > 0) {
                    response.setResponse("查询成功", 1, true, list);
                    return response;
                } else {
                    response.setResponse("未查询到符合添加的结果", -1, false, list);
                    return response;
                }
            } else {
                Response res = new Response("请输入商品名称", -1, false);
                return res;
            }
        }
    }

    /** *
     * 更新商品
     *
     * @param product
     * @author zhouquan
     * @return com.example.demo.entity.Response 
     * @create 2020/4/25 15:46
     */
    @RequestMapping(value = "/updateProduct",method = RequestMethod.POST)
    public Response updateProduct(@RequestBody Product product){
        String id = product.getId();
        if(id != null && id != ""){
            int count = service.updateProdect(product);
            if (count>0){
                Response response = new Response("更新成功",1,true);
                return  response;
            }else {
                Response response = new Response("更新失败", -1, false);
                return response;
            }
        }else{
            Response response = new Response("商品ID不能为空", -1, false);
            return response;
        }
    }
}

测试接口

在这里插入图片描述

总结

  1. 项目里还有到了两个插件:lombok、hutool,极大的提高了我的编程效率。
  2. 使用编程约束规范,使我的代码更加规范、工整,以后要养成良好的编码习惯。在这里插入图片描述
    3.xml里的条件查询是用 if 语句,如果为数值类型,则判断是否为 -1,而不是空 “”。
    在这里插入图片描述
    4.使用的是mybatis-plus的依赖。在这里插入图片描述
    也可以使用:
<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper</artifactId>
            <version>3.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>

第一个依赖是 mysql 数据库,不用多说。
第二个依赖是 tk.mybatis,mybatis 包。
第三个依赖是 org.mybatis.spring.boot,主要用来集成 mybatis 和 spring 框架。
这两种包还是有一定的区别的:Mybatis-Plus和Mybatis的区别

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值