0


JavaWeb 购物车项目

今天是基于我们所学的服务器存储端和三层架构来完善该项目,今天先完善一部分的功能。

一.购物车项目思路

1.登录

  • 先创建一个用户表,表中有id,name,pwd三个属性首。
  • 需要具备一个登录页面,一个处理登录数据的页面,在该页面进行判断,当该用户存在,我们跳转到商城,用户不存在回到登录界面

2.商城

  • 创建一张商品表
  • 当登录成功以后跳转到商城页面,商城有商品显示,商品数据来自于商品表中的数据。
  • 点击加入购物车,如果点击加入的商品在购物车中存在的话,只增加商品数量,而不会在显示一条该商品数据。
  • 点击加入购物车时携带商品id跳转到处理购物车数据的页面,我们把购物车中的数据存在session中。

**3.购物车 **

  • 购物车今天我们只做从session中把数据拿出来,显示在页面上,可以把总价计算出来,其他功能在下一篇文章给大家讲解

二.项目的文件路径展示

** biz **: 存放业务逻辑层的接口
** biz.impl : 存放业务逻辑层的实现类
** dao
: 存放数据访问层的接口
** dao.impl **: 存放数据访问层的实现


三.项目代码展示

1.ulit包下DBHeper类代码展示

作用:连接数据库oracle

  1. package com.yjx.ulit;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import oracle.jdbc.driver.OracleDriver;
  7. public class DBHeper {
  8. static {
  9. try {
  10. Class.forName("oracle.jdbc.driver.OracleDriver");
  11. } catch (Exception e) {
  12. e.printStackTrace();
  13. }
  14. }
  15. private static final String URL="jdbc:oracle:thin:@localhost:1521:orcl";
  16. public static Connection getCon() {
  17. try {
  18. return DriverManager.getConnection(URL,"scott","zking123");
  19. } catch (Exception e) {
  20. e.printStackTrace();
  21. }
  22. return null;
  23. }
  24. public static void getColes(Connection con,PreparedStatement ps,ResultSet rs) {
  25. try {
  26. if(con!=null&&!con.isClosed())con.close();
  27. if(ps!=null)ps.close();
  28. if(rs!=null)rs.close();
  29. } catch (Exception e) {
  30. e.printStackTrace();
  31. }
  32. }
  33. }

2.pojo包下方的实体类

user:用户实体类

  1. package com.yjx.pojo;
  2. public class User {
  3. private int id;
  4. private String name;
  5. private String pwd;
  6. public int getId() {
  7. return id;
  8. }
  9. public void setId(int id) {
  10. this.id = id;
  11. }
  12. public String getName() {
  13. return name;
  14. }
  15. public void setName(String name) {
  16. this.name = name;
  17. }
  18. public String getPwd() {
  19. return pwd;
  20. }
  21. public void setPwd(String pwd) {
  22. this.pwd = pwd;
  23. }
  24. public User() {
  25. }
  26. public User(int id, String name, String pwd) {
  27. super();
  28. this.id = id;
  29. this.name = name;
  30. this.pwd = pwd;
  31. }
  32. @Override
  33. public String toString() {
  34. return "User [id=" + id + ", name=" + name + ", pwd=" + pwd + "]";
  35. }
  36. }

shop:商品实体类

  1. package com.yjx.pojo;
  2. public class Shop {
  3. private int id;
  4. private String name;
  5. private Double price;
  6. private String text;
  7. public int getId() {
  8. return id;
  9. }
  10. public void setId(int id) {
  11. this.id = id;
  12. }
  13. public String getName() {
  14. return name;
  15. }
  16. public void setName(String name) {
  17. this.name = name;
  18. }
  19. public Double getPrice() {
  20. return price;
  21. }
  22. public void setPrice(Double price) {
  23. this.price = price;
  24. }
  25. public String getText() {
  26. return text;
  27. }
  28. public void setText(String text) {
  29. this.text = text;
  30. }
  31. public Shop(int id, String name, Double price, String text) {
  32. super();
  33. this.id = id;
  34. this.name = name;
  35. this.price = price;
  36. this.text = text;
  37. }
  38. public Shop() {
  39. }
  40. @Override
  41. public String toString() {
  42. return "Shop [id=" + id + ", name=" + name + ", price=" + price + ", text=" + text + "]";
  43. }
  44. }

3.vo包下购物车的实体类

  1. package com.yjx.vo;
  2. public class Car {
  3. private Integer id;
  4. private String name;
  5. private Integer count;
  6. private Double sum;
  7. public Integer getId() {
  8. return id;
  9. }
  10. public void setId(Integer id) {
  11. this.id = id;
  12. }
  13. public String getName() {
  14. return name;
  15. }
  16. public void setName(String name) {
  17. this.name = name;
  18. }
  19. public Integer getCount() {
  20. return count;
  21. }
  22. public void setCount(Integer count) {
  23. this.count = count;
  24. }
  25. public Double getSum() {
  26. return sum;
  27. }
  28. public void setSum(Double sum) {
  29. this.sum = sum;
  30. }
  31. @Override
  32. public String toString() {
  33. return "Car [id=" + id + ", name=" + name + ", count=" + count + ", sum=" + sum + "]";
  34. }
  35. public Car(Integer id, String name, Integer count, Double sum) {
  36. super();
  37. this.id = id;
  38. this.name = name;
  39. this.count = count;
  40. this.sum = sum;
  41. }
  42. public Car() {
  43. // TODO Auto-generated constructor stub
  44. }
  45. }

4.数据访问层

** user的数据访问层**:** dao** : 存放数据访问层的接口(IUserDao)
** dao.impl **: 存放数据访问层的实现类(UserDaoImpl)

dao : 存放数据访问层的接口(IUserDao)

  1. package com.yjx.dao;
  2. import java.util.List;
  3. import com.yjx.pojo.Shop;
  4. import com.yjx.pojo.User;
  5. public interface IUserDao {
  6. //登录方法
  7. User login(User u);
  8. }

** dao.impl **: 存放数据访问层的实现类(UserDaoImpl)

  1. package com.yjx.dao.impl;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import com.yjx.dao.IUserDao;
  8. import com.yjx.pojo.Shop;
  9. import com.yjx.pojo.User;
  10. import com.yjx.ulit.DBHeper;
  11. public class UserDaoimpl implements IUserDao{
  12. private Connection con;
  13. private PreparedStatement ps;
  14. private ResultSet rs;
  15. /**
  16. * 登录方法
  17. */
  18. public User login(User u) {
  19. try {
  20. con=DBHeper.getCon();
  21. ps=con.prepareStatement("select * from shop_user where uname=?");
  22. ps.setString(1,u.getName());
  23. rs=ps.executeQuery();
  24. if(rs.next()) {
  25. User user=new User();
  26. user.setId(rs.getInt(1));
  27. user.setName(rs.getString(2));
  28. user.setPwd(rs.getString(3));
  29. return user;
  30. }
  31. } catch (Exception e) {
  32. e.printStackTrace();
  33. }finally {
  34. DBHeper.getColes(con, ps, rs);
  35. }
  36. return null;
  37. }
  38. }

** shop的数据访问层**:** dao** : 存放数据访问层的接口(IShopDao)
** dao.impl **: 存放数据访问层的实现类(ShoprDaoImpl)

dao : 存放数据访问层的接口(IShopDao)

  1. package com.yjx.dao;
  2. import java.util.List;
  3. import com.yjx.pojo.Shop;
  4. public interface IShopDao {
  5. //查询数据库中商品表中的所有数据
  6. List<Shop> select();
  7. //根据id查询数据
  8. Shop selectId(int id);
  9. }

**dao.impl **: 存放数据访问层的实现类(ShoprDaoImpl)

  1. package com.yjx.dao.impl;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import com.yjx.dao.IShopDao;
  8. import com.yjx.pojo.Shop;
  9. import com.yjx.ulit.DBHeper;
  10. public class ShopDaoImpl implements IShopDao {
  11. private Connection con;
  12. private PreparedStatement ps;
  13. private ResultSet rs;
  14. public List<Shop> select() {
  15. List<Shop> list=new ArrayList();
  16. try {
  17. con=DBHeper.getCon();
  18. ps=con.prepareStatement("select * from shop");
  19. rs=ps.executeQuery();
  20. while(rs.next()) {
  21. Shop s=new Shop();
  22. s.setId(rs.getInt(1));
  23. s.setName(rs.getString(2));
  24. s.setPrice(rs.getDouble(3));
  25. s.setText(rs.getString(4));
  26. list.add(s);
  27. }
  28. } catch (Exception e) {
  29. e.printStackTrace();
  30. }finally {
  31. DBHeper.getColes(con, ps, rs);
  32. }
  33. return list;
  34. }
  35. @Override
  36. public Shop selectId(int id) {
  37. try {
  38. con=DBHeper.getCon();
  39. ps=con.prepareStatement("select * from shop where sidd=?");
  40. ps.setInt(1, id);
  41. rs=ps.executeQuery();
  42. if(rs.next()) {
  43. Shop s=new Shop();
  44. s.setId(rs.getInt(1));
  45. s.setName(rs.getString(2));
  46. s.setPrice(rs.getDouble(3));
  47. s.setText(rs.getString(4));
  48. return s;
  49. }
  50. } catch (Exception e) {
  51. e.printStackTrace();
  52. }finally {
  53. DBHeper.getColes(con, ps, rs);
  54. }
  55. return null;
  56. }
  57. }

5.业务逻辑层

user业务逻辑层: biz : 存放业务逻辑层的接口(IUserBiz)

** biz.imp**l : 存放业务逻辑层的实现类(UserBizImpl)

** biz** : 存放业务逻辑层的接口(IUserBiz)

  1. package com.yjx.biz;
  2. import com.yjx.pojo.User;
  3. public interface IUserBiz {
  4. //登录方法
  5. User login(User u);
  6. }

biz.impl : 存放业务逻辑层的实现类(UserBizImpl)

  1. package com.yjx.biz.impl;
  2. import com.yjx.biz.IUserBiz;
  3. import com.yjx.dao.IUserDao;
  4. import com.yjx.dao.impl.UserDaoimpl;
  5. import com.yjx.pojo.User;
  6. public class UserBizImpl implements IUserBiz{
  7. IUserDao dao=new UserDaoimpl();
  8. /**
  9. * 登录方法
  10. */
  11. public User login(User u) {
  12. User user=dao.login(u);
  13. //当用户不为空
  14. if(user!=null) {
  15. if(user.getPwd().equals(u.getPwd())) {
  16. return user;
  17. }
  18. }
  19. return null;
  20. }
  21. }

shop业务逻辑层: biz : 存放业务逻辑层的接口(IShopBiz)

** biz.imp**l : 存放业务逻辑层的实现类(ShopBizImpl)

** biz** : 存放业务逻辑层的接口(IShopBiz

  1. package com.yjx.biz;
  2. import java.util.List;
  3. import com.yjx.pojo.Shop;
  4. public interface IShopBiz {
  5. //查询数据库中商品表中的所有数据
  6. List<Shop> select();
  7. //根据id查询数据
  8. Shop selectId(int id);
  9. }

biz.impl : 存放业务逻辑层的实现类(ShopBizImpl)

  1. package com.yjx.biz.impl;
  2. import java.util.List;
  3. import com.yjx.biz.IShopBiz;
  4. import com.yjx.dao.IShopDao;
  5. import com.yjx.dao.impl.ShopDaoImpl;
  6. import com.yjx.pojo.Shop;
  7. public class ShopBizImpl implements IShopBiz{
  8. IShopDao dao=new ShopDaoImpl();
  9. /*
  10. *查询商品表的所有数据
  11. */
  12. @Override
  13. public List<Shop> select() {
  14. List<Shop> list=dao.select();
  15. return list;
  16. }
  17. /**
  18. * 根据id查询数据
  19. */
  20. public Shop selectId(int id) {
  21. Shop s=dao.selectId(id);
  22. return s;
  23. }
  24. }

6.登录界面

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <html lang="zh">
  4. <head>
  5. <meta charset="UTF-8">
  6. <title>Document</title>
  7. <meta name="viewport" content="width=device-width, initial-scale=1">
  8. <link rel="stylesheet" href="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/css/bootstrap.css">
  9. <script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
  10. <script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
  11. <style>
  12. * {
  13. outline: none !important;
  14. }
  15. html,
  16. body {
  17. background: #1abe9c;
  18. }
  19. form {
  20. width: 300px;
  21. background: #ebeff2;
  22. box-shadow: 0px 0px 50px rgba(0, 0, 0, .5);
  23. border-radius: 5px;
  24. padding: 20px;
  25. position: absolute;
  26. left: 50%;
  27. top: 50%;
  28. transform: translate(-50%, -50%);
  29. }
  30. .btn-group {
  31. width: 100%;
  32. }
  33. .btn-group button {
  34. width: 50%;
  35. }
  36. </style>
  37. </head>
  38. <body>
  39. <form action="doLogin.jsp" method="post">
  40. <h3 class="text-center" style="text-shadow: 2px 2px 1px #ed3f3f;">欢迎光临胡阿玛超市</h3>
  41. <div class="form-group">
  42. <input name="username" type="text" class="form-control" placeholder="请输入您的用户名">
  43. </div>
  44. <div class="form-group">
  45. <input name="userpwd" type="password" class="form-control" placeholder="请输入您的密码">
  46. </div>
  47. <div class="btn-group">
  48. <button type="submit" class="btn btn-primary">登录</button>
  49. <button type="button" class="btn btn-danger">没有账号?</button>
  50. </div>
  51. </form>
  52. </body></html>

7.处理登录数据页面

  1. <%@page import="java.util.ArrayList"%>
  2. <%@page import="com.yjx.pojo.Shop"%>
  3. <%@page import="java.util.List"%>
  4. <%@page import="com.yjx.pojo.User"%>
  5. <%@page import="com.yjx.biz.impl.UserBizImpl"%>
  6. <%@page import="com.yjx.biz.IUserBiz"%>
  7. <%@ page language="java" contentType="text/html; charset=UTF-8"
  8. pageEncoding="UTF-8"%>
  9. <%
  10. //接收数据
  11. String name=request.getParameter("username");
  12. String pwd=request.getParameter("userpwd");
  13. //实例一个用户,得到一个用户
  14. User u=new User(1,name,pwd);
  15. IUserBiz userbiz=new UserBizImpl();
  16. //调用登录方法,得到一个用户
  17. User user=userbiz.login(u);
  18. //判断该用户是否存在
  19. if(user!=null){
  20. //将用户存入session中
  21. session.setAttribute("user",user);
  22. List<Shop> list=new ArrayList();
  23. //将购物车存入session中
  24. session.setAttribute("list",list);
  25. //跳转到首页
  26. response.sendRedirect("index.jsp");
  27. }else{
  28. //当用户为空不存在,回到登录界面
  29. response.sendRedirect("login.jsp");
  30. }
  31. %>

8.商城首页

  1. <%@page import="com.yjx.vo.Car"%>
  2. <%@page import="java.util.List"%>
  3. <%@page import="com.yjx.pojo.Shop"%>
  4. <%@page import="com.yjx.biz.impl.ShopBizImpl"%>
  5. <%@page import="com.yjx.biz.IShopBiz"%>
  6. <%@page import="com.yjx.pojo.User"%>
  7. <%@ page language="java" contentType="text/html; charset=UTF-8"
  8. pageEncoding="UTF-8"%>
  9. <html lang="zh">
  10. <head>
  11. <meta charset="UTF-8">
  12. <title>Document</title>
  13. <meta name="viewport" content="width=device-width, initial-scale=1">
  14. <link rel="stylesheet" href="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/css/bootstrap.css">
  15. <script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
  16. <script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
  17. <style>
  18. td:nth-child(3)::before{
  19. content: "$";
  20. }
  21. </style>
  22. </head>
  23. <%
  24. Object obj=session.getAttribute("user");
  25. if(obj==null){
  26. response.sendRedirect("/login.jsp");
  27. return;
  28. }
  29. %>
  30. <body>
  31. <div class="jumbotron">
  32. <div class="container">
  33. <h1>欢迎光临胡阿玛SuperMarket</h1>
  34. <!-- 强转成一个用户,拿到用户名字 -->
  35. <p>尊贵的<%=((User)obj).getName()%></p>
  36. <a href="car.jsp">购物车</a>
  37. </div>
  38. </div>
  39. <table class="table">
  40. <tr>
  41. <th>商品序号</th>
  42. <th>商品名称</th>
  43. <th>商品单价</th>
  44. <th>商品描述</th>
  45. <th>操作</th>
  46. </tr>
  47. <%
  48. IShopBiz shop=new ShopBizImpl();
  49. //获得一个集合
  50. List<Shop>list=shop.select();
  51. //遍历数据
  52. for(Shop s:list){
  53. %>
  54. <tr>
  55. <td><%=s.getId() %></td>
  56. <td><%=s.getName() %></td>
  57. <td><%=s.getPrice() %></td>
  58. <td><%=s.getText() %></td>
  59. <td>
  60. <div class="btn-group btn-group-xs">
  61. <!-- 当点击了加入购物车将数商品的id传过去 -->
  62. <a class="btn btn-primary" href="${pageContext.request.contextPath}/doShop.jsp?id=<%=s.getId()%>">添加购物车</a>
  63. </div>
  64. </td>
  65. </tr>
  66. <%
  67. }
  68. %>
  69. </table>
  70. </body></html>

9.购物车数据处理页面

  1. <%@page import="com.yjx.vo.Car"%>
  2. <%@page import="java.util.List"%>
  3. <%@page import="com.yjx.biz.impl.ShopBizImpl"%>
  4. <%@page import="com.yjx.biz.IShopBiz"%>
  5. <%@page import="com.yjx.pojo.Shop"%>
  6. <%@page import="com.yjx.dao.impl.ShopDaoImpl"%>
  7. <%@page import="com.yjx.dao.IShopDao"%>
  8. <%@ page language="java" contentType="text/html; charset=UTF-8"
  9. pageEncoding="UTF-8"%>
  10. <%
  11. List<Car> list=(List<Car>)session.getAttribute("list");
  12. //接收到传过来的数据
  13. String str=request.getParameter("id");
  14. //判断当在首页点击加入购物车时,已经存在购物车中的商品,只在数量上面新增
  15. int id=-1;
  16. if(str!=null){
  17. id=Integer.parseInt(str);
  18. }
  19. boolean f=true;
  20. //遍历集合
  21. for(Car c:list){
  22. if(c.getId()==id){
  23. //数量+1
  24. c.setCount(c.getCount()+1);
  25. //总价
  26. c.setSum(c.getSum()*c.getCount());
  27. f=false;
  28. break;
  29. }
  30. }
  31. if(f){
  32. IShopBiz sbiz=new ShopBizImpl();
  33. Shop s=sbiz.selectId(id);
  34. //将数据加入购物车中
  35. Car car=new Car();
  36. //id
  37. car.setId(s.getId());
  38. car.setName(s.getName());
  39. car.setCount(1);
  40. //总价
  41. car.setSum(s.getPrice()*car.getCount());
  42. list.add(car);
  43. }
  44. //更新购物车
  45. session.setAttribute("list",list);
  46. //跳回首页
  47. response.sendRedirect("index.jsp");
  48. %>

10.购物车页面

  1. <%@page import="com.yjx.vo.Car"%>
  2. <%@page import="java.util.List"%>
  3. <%@page import="com.yjx.pojo.User"%>
  4. <%@ page language="java" contentType="text/html; charset=UTF-8"
  5. pageEncoding="UTF-8"%>
  6. <!DOCTYPE html>
  7. <html lang="en">
  8. <head>
  9. <meta charset="UTF-8">
  10. <title>购物车</title>
  11. <meta name="viewport" content="width=device-width, initial-scale=1">
  12. <link rel="stylesheet" href="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/css/bootstrap.css">
  13. <script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
  14. <script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
  15. <style>
  16. * {
  17. outline: none !important;
  18. }
  19. td,
  20. th {
  21. text-align: center;
  22. }
  23. input {
  24. text-align: center;
  25. }
  26. </style>
  27. </head>
  28. <%
  29. Object obj=session.getAttribute("user");
  30. %>
  31. <body>
  32. <div class="jumbotron">
  33. <div class="container">
  34. <h1>欢迎光临胡阿玛购物车🛒</h1>
  35. <p>尊贵的<%=((User)obj).getName()%></p>
  36. </div>
  37. </div>
  38. <a href="index.jsp" >商城</a>
  39. <table class="table">
  40. <tr>
  41. <th>商品序号</th>
  42. <th>商品名称</th>
  43. <th>商品个数</th>
  44. <th>商品总价</th>
  45. <th>操作</th>
  46. </tr>
  47. <%
  48. List<Car> list=(List<Car>)session.getAttribute("list");
  49. Double sum=0.00;
  50. //遍历
  51. for(Car c:list){
  52. sum+=c.getSum();
  53. %>
  54. <tr>
  55. <td style="line-height: 30.5px;"><%=c.getId()%></td>
  56. <td style="line-height: 30.5px;"><%=c.getName() %></td>
  57. <td>
  58. <div class="input-group" style="width: 120px;margin: auto;">
  59. <span class="input-group-btn">
  60. <a class="btn btn-default" type="button" href="doCar.jsp?math=1&id=<%=c.getId()%>">-</a>
  61. </span>
  62. <input value="<%=c.getCount()%>" type="text" class="form-control">
  63. <span class="input-group-btn">
  64. <a class="btn btn-default" type="button" href="doCar.jsp?math=2&id=<%=c.getId()%>">+</a>
  65. </span>
  66. </div>
  67. </td>
  68. <td style="line-height: 30.5px;">$<%=c.getSum() %></td>
  69. <td style="line-height: 30.5px;">
  70. <button class="btn btn-primary">删除</button>
  71. </td>
  72. </tr>
  73. <%
  74. }
  75. %>
  76. </table>
  77. <h1 class="alert alert-info">
  78. 当前购物车总价
  79. <small><%=sum %></small>
  80. <button class="btn btn-danger">点我结算</button>
  81. </h1>
  82. </body></html>

今天的学习就到这里啦,明天在给大家完善该购物车项目。

标签: html web java

本文转载自: https://blog.csdn.net/m0_65725031/article/details/124166695
版权归原作者 客行. 所有, 如有侵权,请联系我们删除。

“JavaWeb 购物车项目”的评论:

还没有评论