字段校验有的不生效

来源:6-2 添加商品接口开发

慕仰4837686

2023-04-24 13:06:25

为什么我的字段校验中对于name和image两个是否为空的判断没生效,而其他字段的校验生效了

图片描述

我的controller:

package com.example.springbootprj0322.controller;

import com.example.springbootprj0322.common.ApiRestResponse;
import com.example.springbootprj0322.model.request.AddProductReq;
import com.example.springbootprj0322.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @ClassName ProductAdminController
 * @description: 后台商品管理控制器
 * @author: 自己名字
 * @create: 2023-04-23 16:11
 **/
@RestController
public class ProductAdminController {
    @Autowired
    ProductService productService;

    @PostMapping("admin/product/add")
    //@ResponseBody
    public ApiRestResponse addProduct(@Valid @RequestBody AddProductReq addProductReq){
        productService.addProduct(addProductReq);
        return ApiRestResponse.success();

    }
}

我的serviceImpl:


package com.example.springbootprj0322.service.impl;

import com.example.springbootprj0322.exception.ExceptionEnum;
import com.example.springbootprj0322.exception.MallException;
import com.example.springbootprj0322.model.dao.ProductMapper;
import com.example.springbootprj0322.model.pojo.Product;
import com.example.springbootprj0322.model.request.AddProductReq;
import com.example.springbootprj0322.service.ProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName ProductServiceImpl
 * @description: 商品服务
 * @author: 自己名字
 * @create: 2023-04-23 16:52
 **/
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    ProductMapper productMapper;

    @Override
    public void addProduct(AddProductReq addProductReq){
        Product product =new Product();
        BeanUtils.copyProperties(addProductReq,product);
        Product productOld=productMapper.selectByName(addProductReq.getName());
        if (productOld!=null) {
            throw new MallException(ExceptionEnum.NAME_EXISTED);
        }
        int count=productMapper.insertSelective(product);
        if(count==0){
            throw new MallException(ExceptionEnum.CREATE_FAILED);
        }

    }
}

我的mapper

package com.example.springbootprj0322.model.dao;

import com.example.springbootprj0322.model.pojo.Product;
import org.springframework.stereotype.Repository;

@Repository
public interface ProductMapper {
    int deleteByPrimaryKey(Integer id);

    int insert(Product record);

    int insertSelective(Product record);

    Product selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(Product record);

    int updateByPrimaryKey(Product record);

    Product selectByName(String name);
}

我的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.springbootprj0322.model.dao.ProductMapper">
  <resultMap id="BaseResultMap" type="com.example.springbootprj0322.model.pojo.Product">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="image" jdbcType="VARCHAR" property="image" />
    <result column="detail" jdbcType="VARCHAR" property="detail" />
    <result column="category_id" jdbcType="INTEGER" property="categoryId" />
    <result column="price" jdbcType="INTEGER" property="price" />
    <result column="stock" jdbcType="INTEGER" property="stock" />
    <result column="status" jdbcType="INTEGER" property="status" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
  </resultMap>
  <sql id="Base_Column_List">
    id, `name`, image, detail, category_id, price, stock, `status`, create_time, update_time
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from imooc_mall_product
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from imooc_mall_product
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.example.springbootprj0322.model.pojo.Product">
    insert into imooc_mall_product (id, `name`, image,
      detail, category_id, price,
      stock, `status`, create_time,
      update_time)
    values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{image,jdbcType=VARCHAR},
      #{detail,jdbcType=VARCHAR}, #{categoryId,jdbcType=INTEGER}, #{price,jdbcType=INTEGER},
      #{stock,jdbcType=INTEGER}, #{status,jdbcType=INTEGER}, #{createTime,jdbcType=TIMESTAMP},
      #{updateTime,jdbcType=TIMESTAMP})
  </insert>
  <insert id="insertSelective" parameterType="com.example.springbootprj0322.model.pojo.Product">
    insert into imooc_mall_product
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="name != null">
        `name`,
      </if>
      <if test="image != null">
        image,
      </if>
      <if test="detail != null">
        detail,
      </if>
      <if test="categoryId != null">
        category_id,
      </if>
      <if test="price != null">
        price,
      </if>
      <if test="stock != null">
        stock,
      </if>
      <if test="status != null">
        `status`,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="updateTime != null">
        update_time,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="name != null">
        #{name,jdbcType=VARCHAR},
      </if>
      <if test="image != null">
        #{image,jdbcType=VARCHAR},
      </if>
      <if test="detail != null">
        #{detail,jdbcType=VARCHAR},
      </if>
      <if test="categoryId != null">
        #{categoryId,jdbcType=INTEGER},
      </if>
      <if test="price != null">
        #{price,jdbcType=INTEGER},
      </if>
      <if test="stock != null">
        #{stock,jdbcType=INTEGER},
      </if>
      <if test="status != null">
        #{status,jdbcType=INTEGER},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null">
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.example.springbootprj0322.model.pojo.Product">
    update imooc_mall_product
    <set>
      <if test="name != null">
        `name` = #{name,jdbcType=VARCHAR},
      </if>
      <if test="image != null">
        image = #{image,jdbcType=VARCHAR},
      </if>
      <if test="detail != null">
        detail = #{detail,jdbcType=VARCHAR},
      </if>
      <if test="categoryId != null">
        category_id = #{categoryId,jdbcType=INTEGER},
      </if>
      <if test="price != null">
        price = #{price,jdbcType=INTEGER},
      </if>
      <if test="stock != null">
        stock = #{stock,jdbcType=INTEGER},
      </if>
      <if test="status != null">
        `status` = #{status,jdbcType=INTEGER},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null">
        update_time = #{updateTime,jdbcType=TIMESTAMP},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.example.springbootprj0322.model.pojo.Product">
    update imooc_mall_product
    set `name` = #{name,jdbcType=VARCHAR},
      image = #{image,jdbcType=VARCHAR},
      detail = #{detail,jdbcType=VARCHAR},
      category_id = #{categoryId,jdbcType=INTEGER},
      price = #{price,jdbcType=INTEGER},
      stock = #{stock,jdbcType=INTEGER},
      `status` = #{status,jdbcType=INTEGER},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      update_time = #{updateTime,jdbcType=TIMESTAMP}
    where id = #{id,jdbcType=INTEGER}
  </update>
  <select id="selectByName" parameterType="java.lang.String" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from imooc_mall_product
    where name = #{name,jdbcType=VARCHAR}
  </select>
</mapper>

我的请求参数

package com.example.springbootprj0322.model.request;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.Date;
/*添加的请求参数类*/
public class AddProductReq {

    @NotNull
    private String name;

    @NotNull(message = "商品图片不能为空")
    private String image;

    private String detail;

    @NotNull(message = "商品分类不能为空")
    private Integer categoryId;

    @NotNull(message = "商品价格不能为空")
    @Min(value = 1,message = "商品价格不能小于1分")
    private Integer price;

    @NotNull(message = "商品库存不能为空")
    @Max(value = 10000,message = "商品库存不能大于10000")
    private Integer stock;

    private Integer status;





    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name == null ? null : name.trim();
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image == null ? null : image.trim();
    }

    public String getDetail() {
        return detail;
    }

    public void setDetail(String detail) {
        this.detail = detail == null ? null : detail.trim();
    }

    public Integer getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(Integer categoryId) {
        this.categoryId = categoryId;
    }

    public Integer getPrice() {
        return price;
    }

    public void setPrice(Integer price) {
        this.price = price;
    }

    public Integer getStock() {
        return stock;
    }

    public void setStock(Integer stock) {
        this.stock = stock;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }


}

写回答

1回答

好帮手慕小蓝

2023-04-24

同学你好,@NotNull注解如其名字所示,只能校验值是否为null,而不能判断字符串是否为空串。如果需要判断不为空串并且不为null值,需要使用@NotEmpty注解。

祝学习愉快~

0

0 学习 · 9886 问题

查看课程