SSM购物车之产品模块day02
1.SQL
先创建数据库cart,然后再创建表product,再向里面添加数据
create database cart; DROP TABLE IF EXISTS `product`; CREATE TABLE `product` ( `id` int(11) DEFAULT NULL, `name` varchar(50) DEFAULT NULL, `price` float DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; INSERT INTO product VALUES(1,'岁岁',500); INSERT INTO product VALUES(2,'老四',2500); INSERT INTO product VALUES(3,'老三',180); INSERT INTO product VALUES(4,'康桑',0.20);
2.Product类(实体类)
1 package cn.gb.pojo; 2 3 public class Product { 4 private int id; 5 private String name; 6 private float price; 7 8 public int getId() { 9 return id; 10 } 11 12 public void setId(int id) { 13 this.id = id; 14 } 15 16 public String getName() { 17 return name; 18 } 19 20 public void setName(String name) { 21 this.name = name; 22 } 23 24 public float getPrice() { 25 return price; 26 } 27 28 public void setPrice(float price) { 29 this.price = price; 30 } 31 }
3.Mapper层(数据访问层)
建立mapper接口与sql语句的映射
1 package cn.gb.mapper; 2 3 import cn.gb.pojo.Product; 4 5 import java.util.List; 6 7 public interface ProductMapper { 8 List<Product> list(); 9 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <mapper namespace="cn.gb.mapper.ProductMapper"> 6 <!--List<Product> list();--> 7 <select id="list" resultType="Product"> 8 SELECT * FROM product order by id desc 9 </select> 10 11 </mapper>
4.Service层(业务实现层)
1 package cn.gb.service; 2 3 import cn.gb.mapper.ProductMapper; 4 import cn.gb.pojo.Product; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Service; 7 import org.springframework.transaction.annotation.Transactional; 8 9 import java.util.List; 10 11 @Service 12 @Transactional 13 public class ProductService { 14 @Autowired 15 private ProductMapper mapper; 16 public List<Product> list(){ 17 return mapper.list(); 18 } 19 }
5.Controller层()
1 package cn.gb.controller; 2 3 import cn.gb.pojo.Product; 4 import cn.gb.service.ProductService; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Controller; 7 import org.springframework.ui.Model; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 10 import java.util.List; 11 12 @Controller 13 @RequestMapping("product") 14 public class ProductController { 15 @Autowired 16 private ProductService service; 17 @RequestMapping("info") 18 public String info(Model model){ 19 List<Product> list=service.list(); 20 model.addAttribute("list",list); 21 return "/product_info.jsp"; 22 } 23 }
6.jsp页面
注:我这儿的jsp文件是放在web目录下,还有相关的Bootstrap需要的文件(优化表格,也可以不需要)
1 <%-- 2 Created by IntelliJ IDEA. 3 User: Administrator 4 Date: 2021/2/2 0002 5 Time: 10:24 6 To change this template use File | Settings | File Templates. 7 --%> 8 <%@ page language="java" contentType="text/html; charset=UTF-8" 9 pageEncoding="UTF-8" %> 10 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 11 <script src="${pageContext.request.contextPath}/js/jquery-1.11.3.min.js"/> 12 <script src="${pageContext.request.contextPath}/js/bootstrap.min.js"></script> 13 <link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap.min.css"> 14 <html> 15 <head> 16 <title>Title</title> 17 </head> 18 <body> 19 <table class="table table-striped" 20 style="width: 500px;margin: 44px auto" border="1" align="center" cellspacing="0"> 21 <tr> 22 <td>编号</td> 23 <td>名称</td> 24 <td>价格</td> 25 <td>购买</td> 26 </tr> 27 <c:forEach items="${list}" var="pro"> 28 <tr> 29 <td>${pro.id}</td> 30 <td>${pro.name}</td> 31 <td>${pro.price}</td> 32 <td> 33 数量<input pid="${pro.id}" type="text" id="number" value="1" name="num"> 34 <input type="submit" pid="${pro.id}" value="购买"> 35 </td> 36 </tr> 37 </c:forEach> 38 </table> 39 </body> 40 </html>