下面是product
和shopping_cart
的MySQL表。
在product
表中,product_id是自动递增的主键字段。
create table product(
product_id int NOT NULL AUTO_INCREMENT,
product_name varchar(20),
manufacture varchar(20),
available_units INTEGER(10),
price varchar(20),
primary key(product_id));
//3.creating shopping cart
shopping cart table description
create table shopping_cart(
shopping_cart_id int NOT NULL AUTO_INCREMENT,
product_id INTEGER(20),
price varchar(20),
primary key(shopping_cart_id),
PROD_ID INT references product(product_id));
查看产品JSP代码
在这里,我在ViewProduct.jsp
<div align="center">
<h1>Product List</h1>
<table border="1">
<th>Product ID</th>
<th>Product Name</th>
<th>Manufacture </th>
<th>Available Units</th>
<th>Price</th>
<th>Buy</th>
<c:forEach var="product" items="${listProd}">
<tr>
<td>${product.product_id}</td>
<td>${product.product_name}</td>
<td>${product.manufacture}</td>
<td>${product.available_units}</td>
<td>${product.price}</td>
<td><a href="buynow?product_id=${product.product_id}&price=${product.price}">Buy Now</a></td>
</tr>
</c:forEach>
</table>
</div>
控制器方法代码在这里
在控制器方法中,我从ViewProduct.jsp
获取请求参数
在该帮助下,我使用product_id
,价格列将列值添加到购物车表中,并使用product
列更新了product_id
表
// adding shopping_cart details to shopping cart table and updating product table.
@RequestMapping(value = "/buynow", method = RequestMethod.GET)
public ModelAndView buynow(ModelAndView model,@RequestParam Integer product_id,@RequestParam String price,
@ModelAttribute Product product) {
//adding product_id and price to the shopping_cart table
shopping_cartService.addShopping_Cart(product_id,price);
//reducing 1 to product table available_unit column
productService.updateProduct(product_id);
// sending to ViewShoppingCart.jsp, in ViewShoppingCart.jsp i need to retrieve product_id, price with shopping_cart_id columns
//but here i dont have shopping cart column value. because it is autoincremented column.
//how to get auto_incremented column value to buynow controller method from shopping_cartDAOImpl class.
return new ModelAndView("ViewShoppingCart");
}
我附加了ViewProduct.jsp显示,产品MySQL表详细信息和Shopping_Cart表详细信息的图像 此图像显示有关产品列表的详细信息,即Product_id,价格,Available_units,产品的制造公司。
下图显示了产品表的详细信息,例如product_id,价格,制造商公司,产品的可用单位。
下图显示了shopping_cart表的详细信息,它由shopping_cart_id,product_id和产品价格组成。