老师请问这个为什么没有成功
来源:6-5 资源映射开发
rudtjd
2023-04-05 15:29:50






package com.imooc.mall.common;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
/**
* 描述: 常量值
*/
@Configuration
public class Constant {
public static final String IMOOC_MALL_USER = "imooc_mall_user";
public static final String SALT = "8svbsvjkweDF,.03[";
@Value("${file.upload.dir}")
public static String FILE_UPLOAD_DIR;
}package com.imooc.mall.controller;
import com.imooc.mall.common.ApiRestResponse;
import com.imooc.mall.common.Constant;
import com.imooc.mall.exception.ImoocMallException;
import com.imooc.mall.exception.ImoocMallExceptionEnum;
import com.imooc.mall.model.request.AddProductReq;
import com.imooc.mall.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.UUID;
@RestController
public class ProductAdminController {
@Autowired
ProductService productService;
@PostMapping("admin/product/add")
public ApiRestResponse addProduct(@Valid @RequestBody AddProductReq addProductReq){
productService.add(addProductReq);
return ApiRestResponse.success();
}
@PostMapping("admin/upload/file")
public ApiRestResponse upload(HttpServletRequest httpServletRequest, @RequestParam("file") MultipartFile file){
String fileName = file.getOriginalFilename();
String suffixName = fileName.substring(fileName.lastIndexOf("."));
UUID uuid = UUID.randomUUID();
String newFileName = uuid.toString() + suffixName;
File fileDirectory = new File(Constant.FILE_UPLOAD_DIR);
File destFile = new File(Constant.FILE_UPLOAD_DIR + newFileName);
if ( !fileDirectory.exists() ){
if ( !fileDirectory.mkdir() ){
throw new ImoocMallException(ImoocMallExceptionEnum.MKDIR_FAILED);
}
}
try {
file.transferTo(destFile);
} catch (IOException e) {
e.printStackTrace();
}
try {
return ApiRestResponse.success(getHost(new URI(httpServletRequest.getRequestURL()+""))+
"/images/"+newFileName);
} catch (URISyntaxException e) {
return ApiRestResponse.error(ImoocMallExceptionEnum.UPLOAD_FAILED);
}
}
private URI getHost(URI uri){
URI effectiveURI;
try {
effectiveURI = new URI(uri.getScheme(),uri.getUserInfo(),uri.getHost(),uri.getPort(),null,null,null);
} catch (URISyntaxException e) {
effectiveURI = null;
}
return effectiveURI;
}
}package com.imooc.mall.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(message = "商品名称不能为null")
private String name;
@NotNull(message = "商品图片不能为null")
private String image;
private String detail;
@NotNull(message = "商品分类不能为null")
private Integer categoryId;
@NotNull(message = "商品价格不能为null")
@Min(value = 1,message = "价格不能小于1分")
private Integer price;
@NotNull(message = "商品库存不能为null")
@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;
}
}package com.imooc.mall.exception;
/**
* 描述: 异常枚举
*/
public enum ImoocMallExceptionEnum {
NEED_USER_NAME(10001, "用户名不能为空"),
NEED_PASSWORD(10002, "密码不能为空"),
PASSWORD_TOO_SHORT(10003, "密码长度不能小于8位"),
NAME_EXISTED(10004, "不允许重名"),
INSERT_FAILED(10005, "插入失败,请重试"),
WRONG_PASSWORD(10006, "密码错误"),
NEED_LOGIN(10007, "用户未登录"),
UPDATE_FAILED(10008, "更新失败"),
NEED_ADMIN(10009, "无管理员权限"),
PARA_NOT_NULL(10010, "参数不能为空"),
CREATE_FAILED(10011, "新增失败"),
REQUEST_PARAM_ERROR(10012, "参数错误"),
DELETE_FAILED(10013, "删除失败"),
MKDIR_FAILED(10014, "文件夹创建失败"),
UPLOAD_FAILED(10015, "图片上传失败"),
SYSTEM_ERROR(20000, "系统异常,请从控制台或日志中查看具体错误信息");
/**
* 异常码
*/
Integer code;
/**
* 异常信息
*/
String msg;
ImoocMallExceptionEnum(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
<?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.imooc.mall.model.dao.ProductMapper">
<resultMap id="BaseResultMap" type="com.imooc.mall.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.imooc.mall.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.imooc.mall.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.imooc.mall.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.imooc.mall.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"></include>
from imooc_mall_product
where name=#{name,jdbcType = VARCHAR}
</select>
</mapper>1回答
好帮手慕小蓝
2023-04-06
同学你好,同学的问题描述不够清晰,老师不能确定是否理解了同学的问题。同学是指添加商品没有成功吗?
如果是,建议同学提供一下ProductService的代码,或者同学可以对比一下ProductService中的代码。同学提供的代码中没有明显的错误,所以猜测问题出在ProductService中。
如果不是这个问题,建议同学详细描述一下问题。
祝学习愉快~
相似问题