8.SpringBoot简易项目商品模块之更新商品页面点击提交到controller然后实现数据库更新商品最后跳转到显示商品的过程

SpringBoot简易项目商品模块之更新商品页面点击提交到controller然后实现数据库更新商品最后跳转到显示商品的过程

完成:第一遍

1.如何实现更新商品页面点击提交到controller并实现数据库商品更新的功能?

该功能代码:

	@PostMapping("update")
	public String submitUpdatePage(@Valid @ModelAttribute ProductForm productForm, 
			BindingResult bindingResult, ModelMap model) {
		//验证未通过
		if(bindingResult.hasErrors()) {
			model.addAttribute("productForm", productForm);
			return "product/update";
		}
		
		//验证通过,更新数据库
		Product product = new Product();
		BeanUtils.copyProperties(productForm, product);
		productService.updateProduct(product);
		
		return "redirect:/product/list";
		
	}

该文件此时的代码:

package com.cherry.product.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cherry.product.entity.Product;
import com.cherry.product.form.ProductForm;
import com.cherry.product.service.ProductService;

@Controller
@RequestMapping("product")
public class ProductController {
	
	@Autowired
	private ProductService productService;
	
	@GetMapping("show")
	public String showAddPage(ModelMap model) {
		model.addAttribute("productForm", new ProductForm());
		return "product/add";
	}
	
	@PostMapping("add")
	public String submitAddProduct(@Valid @ModelAttribute ProductForm productForm, 
			BindingResult bindingResult, ModelMap model) {
		if(bindingResult.hasErrors()) {
			model.addAttribute("productForm", productForm);
			return "product/add";
		}
		
		Product product = new Product();
		BeanUtils.copyProperties(productForm, product);
		productService.insertProduct(product);
		return "redirect:list";
	}
	
	@GetMapping("list")
	public String showListPage(ModelMap model) {
		List<Product> products = productService.findAllProducts();
		model.addAttribute("products", products);
		return "product/list";
	}
	
	@GetMapping("update/{code}")
	public String showUpdatePage(@PathVariable("code") String code, ModelMap model) {
		//get the product info
		Product product = productService.findProductByCode(code);
		ProductForm productForm = new ProductForm();
		BeanUtils.copyProperties(product, productForm);
		model.addAttribute("productForm", productForm);
		return "product/update";
	}
	
	@PostMapping("update")
	public String submitUpdatePage(@Valid @ModelAttribute ProductForm productForm, 
			BindingResult bindingResult, ModelMap model) {
		//验证未通过
		if(bindingResult.hasErrors()) {
			model.addAttribute("productForm", productForm);
			return "product/update";
		}
		
		//验证通过,更新数据库
		Product product = new Product();
		BeanUtils.copyProperties(productForm, product);
		productService.updateProduct(product);
		
		return "redirect:/product/list";
		
	}
	
}

2.service层实现商品更新

ProductService

该功能代码:

	public int updateProduct(Product product);

该文件此时的代码:

package com.cherry.product.service;

import java.util.List;

import com.cherry.product.entity.Product;

public interface ProductService {
	public int insertProduct(Product product);
	public List<Product> findAllProducts();
	public Product findProductByCode(String code);
	public int updateProduct(Product product);
	public int removeProduct(String code);
}

ProductServiceImpl

该功能代码:

	@Override
	public int updateProduct(Product product) {
		
		return productMapper.updateProduct(product);
	}

该文件此时的代码:

package com.cherry.product.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cherry.product.entity.Product;
import com.cherry.product.mapper.ProductMapper;
import com.cherry.product.service.ProductService;

@Service
public class ProductServiceImpl implements ProductService{

	@Autowired
	private ProductMapper productMapper;
	
	@Override
	public int insertProduct(Product product) {
		
		return productMapper.insertProduct(product);
	}

	@Override
	public List<Product> findAllProducts() {
		
		return productMapper.findAllProducts();
	}

	@Override
	public Product findProductByCode(String code) {
	
		return productMapper.findProductByCode(code);
	}

	@Override
	public int updateProduct(Product product) {
		
		return productMapper.updateProduct(product);
	}



}

3.ProductMapper 实现商品更新

该功能代码:

	@Update("update product set name=#{name}, summary=#{summary}, description=#{description} where code=#{code}")
	public int updateProduct(Product product);

该文件此时的代码:

package com.cherry.product.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.cherry.product.entity.Product;

@Mapper
public interface ProductMapper {
	
	@Insert("insert into product values(#{code}, #{name}, #{summary}, #{description})")
	public int insertProduct(Product product);
	
	@Select("select * from product")
	public List<Product> findAllProducts();
	
	@Select("select * from product where code=#{code}")
	public Product findProductByCode(String code);
	
	@Update("update product set name=#{name}, summary=#{summary}, description=#{description} where code=#{code}")
	public int updateProduct(Product product);
	
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值