9.SpringBoot简易项目商品模块之移除商品

SpringBoot简易项目商品模块之移除商品

完成:第一遍

真正项目中移除一般是有个属性为是否移除
这是为了重要的数据可以帮助用户找回

1.在list.html页面添加移除的链接

list.html

该功能代码:

<td><a th:href="@{'/product/remove/'+${product.code}}" th:text="#{remove.text}"></a></td>

该文件此时的代码:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
	<title th:text="#{product.list.title}">Product List</title>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
</head>
<body>
	<h1 th:text="#{product.list.title}">Product List</h1>
	
	<a th:href="@{/product/show}" th:text="#{product.add.title}">New Product</a>
	
	<table>
		<tr>
			<td th:text="#{product.id}">Product ID</td>
			<td th:text="#{product.name}">Product Name</td>
			<td th:text="#{product.summary}">Product Summary</td>
			<td></td>
			<td></td>
		</tr>
		<tr th:each="product:${products}">
			<td th:text="${product.code}"></td>
			<td th:text="${product.name}"></td>
			<td th:text="${product.summary}"></td>
			<td><a th:href="@{'/product/update/'+${product.code}}" th:text="#{update.text}"></a></td>
			<td><a th:href="@{'/product/remove/'+${product.code}}" th:text="#{remove.text}"></a></td>
		</tr>
	</table>
</body>
</html>

2.在国际化配置文件中配置
remove.text属性

message.properties

该功能代码:

remove.text=Remove

该文件此时的代码:

product.add.title=Add Product
product.id=ID
product.name=Name
product.summary=Summary
product.description=Description
product.list.title=Product List
submit.text=Submit
id.valid=id is required
name.valid=Name is required
summary.valid=Summary is required
description.valid=Description is required
update.text=Update
product.update.title=Update Product
remove.text=Remove

message_en.properties

该功能代码:

remove.text=Remove

该文件此时的代码:

product.add.title=Add Product
product.id=ID
product.name=Name
product.summary=Summary
product.description=Description
product.list.title=Product List
submit.text=Submit
id.valid=id is required
name.valid=Name is required
summary.valid=Summary is required
description.valid=Description is required
update.text=Update
product.update.title=Update Product
remove.text=Remove

message_zh.properties

该功能代码:

remove.text=\u79FB\u9664

该文件此时的代码:

product.add.title=\u65B0\u4EA7\u54C1
product.add.title=\u65B0\u589E\u4EA7\u54C1
product.id=ID
product.name=\u540D\u79F0
product.summary=\u7B80\u8FF0
product.description=\u63CF\u8FF0
product.list.title=\u4EA7\u54C1\u5217\u8868
submit.text=\u63D0\u4EA4
id.valid=ID\u4E0D\u80FD\u4E3A\u7A7A
name.valid=\u540D\u79F0\u4E0D\u80FD\u4E3A\u7A7A
summary.valid=\u7B80\u8FF0\u4E0D\u80FD\u4E3A\u7A7A
description.valid=\u63CF\u8FF0\u4E0D\u80FD\u4E3A\u7A7A
update.text=\u66F4\u65B0
product.update.title=\u66F4\u65B0\u4EA7\u54C1
remove.text=\u79FB\u9664

3.在ProductController 中添加移除功能

ProductController

该功能代码:

@GetMapping("remove/{code}")
	public String remove(@PathVariable("code") String code, ModelMap model) {
		//根据code,从数据库中移除数据
		productService.removeProduct(code);
		
		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";
		
	}
	
	@GetMapping("remove/{code}")
	public String remove(@PathVariable("code") String code, ModelMap model) {
		//根据code,从数据库中移除数据
		productService.removeProduct(code);
		
		return "redirect:/product/list";
	}
}

4.mapper层完成商品移除功能

ProductMapper

该功能代码:

@Delete("delete from product where code=#{code}")
	public int removeProduct(String code);

该文件此时的代码:

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);
	
	@Delete("delete from product where code=#{code}")
	public int removeProduct(String code);
}

5.service层完成商品移除功能

ProductService

该功能代码:

	public int removeProduct(String code);

该文件此时的代码:

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 removeProduct(String code) {
		// TODO Auto-generated method stub
		return productMapper.removeProduct(code);
	}

该文件此时的代码:

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);
	}

	@Override
	public int removeProduct(String code) {
		
		return productMapper.removeProduct(code);
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值