哈哈哈哈嘻嘻嘻

public interface ProductMapper {
    //[q2.1]
    //Please complete the answer of Question 2nd
    int deleteProduct(int pid);

    //[/q2.1]

第二题

<!--    [q2.2]-->
<!--    Please complete the answer of Question 2nd-->
    <delete id="deleteProduct" parameterType="int">
        DELETE FROM product WHERE pid = #{pid}
    </delete>

<!--    [/q2.2-->
package com.cqust.test;

import com.cqust.dao.ProductMapper;
import com.cqust.model.Product;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;

import java.io.InputStream;

import static org.junit.Assert.assertEquals;
import org.junit.Test;

public class JTest {
//[q2.3]
//   Please complete the answer of Question 2nd
private SqlSession sqlSession;

    @Before
    public void setUp() throws Exception {

        String resource = "mybatis-config.xml";
        InputStream inputStream = JTest.class.getClassLoader().getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        sqlSession = sqlSessionFactory.openSession();
    }

    @After
    public void tearDown() throws Exception {
        sqlSession.close();
    }

    @Test
    public void testDel() {
        ProductMapper productMapper = sqlSession.getMapper(ProductMapper.class);
        Product product = new Product();
        product.setPid(1);
        int result = productMapper.deleteProduct(product.getPid());
        assertEquals(1, result);
    }

//[/q2.3]

第三题

//[q3.1]
// Please complete the answer of Question 3rd
public class ProductBiz {
    public int delProduct(Product p) {

        if (p.getPrice() > 20)
        {
            return 1;
        }
        else
            {
            return 0;
        }
    }


//[/q3.1]
<!-- [q3.2]-->
    <!--    Please complete the answer of Question 3rd-->
    <bean id="productService" class="com.cqust.biz.ProductBiz"/>

<!--[/q3.2]  -->
package com.cqust.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import com.cqust.biz.ProductBiz;
import com.cqust.model.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Controller
public class ProductControler {
 // [q4.1]
// Please complete the answer of Question 4th
 @Autowired
 private ProductService ProductBiz;

    @GetMapping
    public ResponseEntity<List<Product>> getAllProducts() {

        List<Product> productList = productBiz.getAllProducts();


        if (productList != null && !productList.isEmpty()) {

            return new ResponseEntity<>(productList, HttpStatus.OK);
        } else {

            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }



// [/q4.1]

// [q5.1]
//Please complete the answer of Question 5th
@RestController
public class ProductController {

    @PostMapping("/delProduct")
    public String deleteProduct(@RequestBody String productName) {
        return "Product " + productName + " deleted successfully!";
    }
}



// [/q5.1]
<%-- [q5.2] --%>
<%--Please complete the answer of Question 5th--%>
    <meta charset="UTF-8">
    <title>Delete Product</title>


<%-- [/q5.2]--%>
<%-- [q5.3] --%>
<%--Please complete the answer of Question 5th--%>
    <h2>Delete Product</h2>
    <form action="http://localhost:8080/delProduct" method="post">
        <label for="productName">Product Name:</label>
        <input type="text" id="productName" name="productName" required>
        <br>
        <input type="submit" value="Delete">
    </form>

<%-- [/q5.3]--%>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值