目录
一.上传产品图片
二.修改产品
三.上下架产品
一.上传产品图片
在新增产品时,我们还需要上传产品图片。我们采用异步上传的方法进行图片上传。
1.在conmmon_ resources.html 中引入jqueryform.js
2.修改product_ add.html 页面
点击保存 ,自动跳转到产品页面
二.修改产品
前端代码略。
运行项目,测试修改功能。
三.上下架产品
由于产品和很多数据比如用户的收藏相关联,一般不删除产品, 而是下架产品,接下来我们
编写产品的上下架功能(其实就是修改产品的status功能)。
***************************************************************************************************
下面是后台产品管理这一块的后端代码:
mapper:
public interface ProductMapper extends BaseMapper<Product> {
//查询产品,参数是一个查询条件,即(page,size)
Page<Product> findProductPage(Page<Product> page);
Product findOne(int pid);
}
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.first.travel.mapper.ProductMapper">
<resultMap id="productMapper" type="com.first.travel.pojo.Product">
<id property="pid" column="pid"></id>
<result property="productName" column="productName"></result>
<result property="price" column="price"></result>
<result property="status" column="status"></result>
<result property="hotline" column="hotline"></result>
<result property="productDesc" column="productDesc"></result>
<result property="pImage" column="pImage"></result>
<result property="cid" column="cid"></result>
<association property="category" column="cid" javaType="com.first.travel.pojo.Category">
<id property="cid" column="cid"></id>
<result property="cname" column="cname"></result>
</association>
</resultMap>
<select id="findProductPage" resultMap="productMapper">
SELECT *
FROM product
LEFT JOIN category ON product.cid = category.cid
ORDER BY product.pid DESC
</select>
<select id="findOne" resultMap="productMapper">
SELECT *
FROM product
LEFT JOIN category ON product.cid = category.cid
where pid = #{pid}
</select>
</mapper>
service:
@Service
@Transactional
public class ProductService {
@Autowired
private ProductMapper productMapper;
public Page<Product> findPage(int page, int size){
Page<Product> productPage = productMapper.findProductPage(new Page(page, size));
return productPage;
}
public void add(Product product){
productMapper.insert(product);
}
public Product findOne(int pid){
return productMapper.findOne(pid);
}
public void update(Product product){
productMapper.updateById(product);
}
public void updateStatus(Integer pid){
Product product = productMapper.selectById(pid);
product.setStatus(!product.getStatus());
productMapper.updateById(product);
}
}
controller:
@Controller
@RequestMapping("/backstage/product")
public class ProductController {
@Autowired
private ProductService productService;
@Autowired
private CategoryService categoryService;
@RequestMapping("/all")
public ModelAndView all(@RequestParam(defaultValue = "1") int page,
@RequestParam(defaultValue = "5")int size){
Page<Product> productPage = productService.findPage(page, size);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("productPage",productPage);
modelAndView.setViewName("/backstage/product_all");
return modelAndView;
}
@RequestMapping("/addPage")
public ModelAndView addPage(){
List<Category> categoryList = categoryService.findAll();
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("categoryList",categoryList);
modelAndView.setViewName("/backstage/product_add");
return modelAndView;
}
@RequestMapping("/add")
public String add(Product product){
productService.add(product);
return "redirect:/backstage/product/all";
}
@RequestMapping("/upload")
@ResponseBody
public WangEditorResult upload(HttpServletRequest request, MultipartFile file) throws IOException {
// 创建文件夹,存放上传文件
// 1.设置上传文件夹的真实路径
String realPath = ResourceUtils.getURL("classpath:").getPath() + "/static/upload";
// 2.判断该文件夹是否存在,如果不存在,新建文件夹
File dir = new File(realPath);
if (!dir.exists()){
dir.mkdirs();
}
// 拿到上传文件名
String filename = file.getOriginalFilename();
filename = UUID.randomUUID()+filename;
// 创建空文件
File newFile = new File(dir, filename);
// 将上传的文件写到空文件中
file.transferTo(newFile);
// 构造返回结果
WangEditorResult wangEditorResult = new WangEditorResult();
wangEditorResult.setErrno(0);
String[] data = {"/upload/"+filename};
wangEditorResult.setData(data);
return wangEditorResult;
}
@RequestMapping("/edit")
public ModelAndView edit(Integer pid){
// 查询被修改的产品
Product product = productService.findOne(pid);
// 查询所有产品类别
List<Category> categoryList = categoryService.findAll();
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("product",product);
modelAndView.addObject("categoryList",categoryList);
modelAndView.setViewName("/backstage/product_edit");
return modelAndView;
}
@RequestMapping("/update")
public String update(Product product){
productService.update(product);
return "redirect:/backstage/product/all";
}
@RequestMapping("/updateStatus")
public String updateStatus(Integer pid,@RequestHeader("Referer") String referer){
productService.updateStatus(pid);
//点击启用/禁用按钮之后不跳到别的页面,还是在这个页面,所以要用到请求头referer
return "redirect:"+referer;
}
}
bean:
@Data
public class WangEditorResult {
private int errno;
private String[] data;
}