补222222

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]
  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值