2.1
package com.cqust.dao; import java.util.List; public interface ProductMapper { //[q2.1] //Please complete the answer of Question 2nd int deleteProduct(Product product); //[/q2.1] }
2.2
<?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.cqust.dao.ProductMapper"> <!-- [q2.2]--> <!-- Please complete the answer of Question 2nd--> <delete id="deleteProduct" parameterType="com.cqust.model.Product"> DELETE FROM products WHERE id = #{id} </delete> <!-- [/q2.2--> </mapper>
2.3
//[q2.3] // Please complete the answer of Question 2nd @Autowired private ProductMapper productMapper; @Test public void testDel() { // 创建一个产品对象 Product product = new Product(); product.setId(1); // 假设要删除的产品 ID 为 1 // 调用删除方法 int rowsAffected = productMapper.deleteProduct(product); // 断言删除操作是否成功 assert rowsAffected == 1 : "Failed to delete product."; } //[/q2.3] }
3
//[q3.1] // Please complete the answer of Question 3rd @Service public class ProductService { @Autowired private ProductMapper productMapper; @Transactional public int delProduct(Product p) { // 检查产品价格是否大于20 if (p.getPrice() > 20) { // 删除产品 int rowsAffected = productMapper.deleteProduct(p); if (rowsAffected > 0) { // 删除成功,返回1 return 1; } else { // 删除失败,返回0 return 0; } } else { // 产品价格不大于20,不符合条件,返回0 return 0; } } } //[/q3.1]
4和5
// [q4.1] // Please complete the answer of Question 4th @Autowired private ProductService productService; @GetMapping("/getAllProducts") public List<Product> getProduct() { return productService.getAllProducts(); } } // [/q4.1] // [q5.1] //Please complete the answer of Question 5th @Controller public class ProductController { @Autowired private ProductService productService; @PostMapping("/delProduct") public String deleteProduct(String productName) { // Add logic to delete product based on the productName // For example, productService.deleteProduct(productName); // Redirect to a confirmation page or any other desired page return "redirect:/productDeleted"; } // Add more controller methods as needed @RequestMapping("/productDeleted") public String productDeletedPage() { return "productDeleted"; } } // [/q5.1]