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]--%>