应用系统定制开发JavaWeb购物车项目

目录


一、项目前提

1、应用系统定制开发购物车并不是一直放数据库

2、应用系统定制开发选择使用的技术:

:(应用系统定制开发购物车项目使用session)

  • 好处:快(应用系统定制开发放在内存当中),存对象的
  • 坏处:占用内存,应用系统定制开发服务器意外待机数据会丢失

cookie

  • 好处:不占用内存,存储很久
  • 坏处:存在客户端上,只能存String,数据有泄露的风险

二、数据库的创建

用户表

  1. create table shop_user (
  2. id number primary key,--用户id
  3. account varchar2(30) not null,--用户账户
  4. password varchar2(30)not null--用户密码
  5. );

商品表

  1. create table shop_goods(
  2. id number primary key,--商品id
  3. name varchar2(50) not null,--商品名称
  4. price number default 0.0,--商品价格
  5. info varchar2(255) default '三五产品' not null--商品介绍
  6. );

三、eclipse进行创建包和类:

  • com.zking.util    帮助类
  • com.zking.pojo  实体类
  • com.zking.dao(放dao接口)
  • com.zking.dao.imp    (放dao接口实现类)
  • com.zking.biz (业务逻辑层放dao方法)
  • com.zking.biz.imp(业务逻辑层放dao实现类)
  •  com.zking.vo (view object 视图对象 前端用)

四、主要实现功能

1、购物车用户登录

login.jsp

界面效果

 

代码如下:

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

doLogin.jsp(处理登录界面)

  1. <%@page import="com.zking.vo.CarItem"%>
  2. <%@page import="java.util.ArrayList"%>
  3. <%@page import="java.util.List"%>
  4. <%@page import="com.zking.pojo.User"%>
  5. <%@page import="com.zking.biz.impl.UserBizImpl"%>
  6. <%@page import="com.zking.biz.IUserBiz"%>
  7. <%@ page language="java" contentType="text/html; charset=UTF-8"
  8. pageEncoding="UTF-8"%>
  9. <%
  10. request.setCharacterEncoding("UTF-8");
  11. String account=request.getParameter("account");
  12. String password=request.getParameter("password");
  13. IUserBiz userBiz=new UserBizImpl();
  14. User user=userBiz.login(new User(0,account,password));
  15. if(user==null){
  16. response.sendRedirect("login.jsp");
  17. }else{
  18. //首页需要登录数据
  19. session.setAttribute("user",user);
  20. //分配购物车
  21. List<CarItem>car=new ArrayList<>();
  22. //放到session中(把购物车给我)
  23. session.setAttribute("car", car);
  24. response.sendRedirect("index.jsp");
  25. }
  26. %>

用户实体类User.java

  1. package com.zking.pojo;
  2. public class User {
  3. private Integer id;
  4. private String account;
  5. private String password;
  6. public Integer getId() {
  7. return id;
  8. }
  9. public void setId(Integer id) {
  10. this.id = id;
  11. }
  12. public String getAccount() {
  13. return account;
  14. }
  15. public void setAccount(String account) {
  16. this.account = account;
  17. }
  18. public String getPassword() {
  19. return password;
  20. }
  21. public void setPassword(String password) {
  22. this.password = password;
  23. }
  24. public User(Integer id, String account, String password) {
  25. super();
  26. this.id = id;
  27. this.account = account;
  28. this.password = password;
  29. }
  30. public User() {
  31. super();
  32. }
  33. @Override
  34. public String toString() {
  35. return "User [id=" + id + ", account=" + account + ", password=" + password + "]";
  36. }
  37. }

IUserBiz.java (用户逻辑接口) 

  1. package com.zking.biz;
  2. import com.zking.pojo.User;
  3. /**
  4. * 用户逻辑接口
  5. * @author zjjt
  6. *
  7. */
  8. public interface IUserBiz {
  9. User login(User user);
  10. }

 UserBizImpl.java (用户逻辑接口实现类)

  1. package com.zking.biz.impl;
  2. import com.zking.biz.IUserBiz;
  3. import com.zking.dao.IUserDao;
  4. import com.zking.dao.impl.UserDaoImpl;
  5. import com.zking.pojo.User;
  6. /**
  7. * 用户逻辑接口实现类
  8. * @author zjjt
  9. *
  10. */
  11. public class UserBizImpl implements IUserBiz{
  12. private IUserDao userDao=new UserDaoImpl();
  13. @Override
  14. public User login(User user) {
  15. User u = userDao.login(user);
  16. if(u!=null) {
  17. if(u.getPassword().equals(user.getPassword())) {
  18. return u;
  19. }
  20. }
  21. return null;
  22. }
  23. }

  IUserDao.java(用户数据访问接口)

  1. package com.zking.dao;
  2. import com.zking.pojo.User;
  3. /**
  4. * 用户数据访问接口
  5. * @author zjjt
  6. *
  7. */
  8. public interface IUserDao {
  9. User login(User user);
  10. }

UserDaoImpl.java(用户数据访问接口实现类)

  1. package com.zking.dao.impl;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.util.List;
  6. import com.zking.dao.IUserDao;
  7. import com.zking.pojo.User;
  8. import com.zking.util.DBHelper;
  9. public class UserDaoImpl implements IUserDao {
  10. private Connection con;
  11. private PreparedStatement ps;
  12. private ResultSet rs;
  13. @Override
  14. public User login(User user) {
  15. try {
  16. con=DBHelper.getCon();
  17. ps=con.prepareStatement("select * from shop_user where account=?");
  18. ps.setString(1,user.getAccount());
  19. rs=ps.executeQuery();
  20. if(rs.next()) {
  21. User u=new User();
  22. u.setId(rs.getInt(1));
  23. u.setAccount(rs.getString(2));
  24. u.setPassword(rs.getString(3));
  25. return u;
  26. }
  27. } catch (Exception e) {
  28. e.printStackTrace();
  29. }finally {
  30. DBHelper.close(con, ps, rs);
  31. }
  32. return null;
  33. }
  34. }

2、商品显示

实现效果如下:

将数据库存放的数据显示在页面上,尊贵的xxx通过session拿到登录的用户名

 Goods.java(商品实体类)

  1. package com.zking.pojo;
  2. public class Goods {
  3. private Integer id;
  4. private String name;
  5. private Integer price;
  6. private String info;
  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 getPrice() {
  20. return price;
  21. }
  22. public void setPrice(Integer price) {
  23. this.price = price;
  24. }
  25. public String getInfo() {
  26. return info;
  27. }
  28. public void setInfo(String info) {
  29. this.info = info;
  30. }
  31. public Goods(Integer id, String name, Integer price, String info) {
  32. super();
  33. this.id = id;
  34. this.name = name;
  35. this.price = price;
  36. this.info = info;
  37. }
  38. public Goods() {
  39. super();
  40. }
  41. @Override
  42. public String toString() {
  43. return "Goods [id=" + id + ", name=" + name + ", price=" + price + ", info=" + info + "]";
  44. }
  45. }

index.jsp(首页代码)

  1. <%@page import="com.zking.pojo.Goods"%>
  2. <%@page import="com.zking.biz.impl.GoodsBizImpl"%>
  3. <%@page import="com.zking.biz.IGoodsBiz"%>
  4. <%@page import="com.zking.pojo.User"%>
  5. <%@ page language="java" contentType="text/html; charset=UTF-8"
  6. pageEncoding="UTF-8"%>
  7. <!DOCTYPE html>
  8. <html lang="zh">
  9. <head>
  10. <meta charset="UTF-8">
  11. <title>Document</title>
  12. <meta name="viewport" content="width=device-width, initial-scale=1">
  13. <link rel="stylesheet" href="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/css/bootstrap.css">
  14. <script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
  15. <script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
  16. <style>
  17. td:nth-child(3)::before{
  18. content: "$";
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <%
  24. Object obj=session.getAttribute("user");
  25. if(obj==null){
  26. response.sendRedirect("login.jsp");
  27. return;
  28. }
  29. %>
  30. <div class="jumbotron">
  31. <div class="container">
  32. <h1>欢迎光临苡桉SuperMarket</h1>
  33. <p>尊贵的<%=((User)obj).getAccount() %></p>
  34. </div>
  35. </div>
  36. <%=session.getAttribute("car")%>
  37. <div class="container">
  38. <table class="table">
  39. <tr>
  40. <th>商品序号</th>
  41. <th>商品名称</th>
  42. <th>商品单价</th>
  43. <th>商品描述</th>
  44. <th>操作</th>
  45. </tr>
  46. <%
  47. IGoodsBiz goodsBiz=new GoodsBizImpl();
  48. for(Goods goods:goodsBiz.getAll()){
  49. %>
  50. <tr>
  51. <td><%=goods.getId() %></td>
  52. <td><%=goods.getName() %></td>
  53. <td><%=goods.getPrice() %></td>
  54. <td><%=goods.getInfo() %></td>
  55. <td>
  56. <div class="btn-group btn-group-xs">
  57. <a href="doAddCar.jsp?id=<%=goods.getId() %>" class="btn btn-primary">添加购物车</a>
  58. </div>
  59. </td>
  60. </tr>
  61. <%
  62. }
  63. %>
  64. </table>
  65. </div>
  66. </body>
  67. </html>

IGoodsBiz.java (商品逻辑接口) 

  1. package com.zking.biz;
  2. import java.util.List;
  3. import com.zking.pojo.Goods;
  4. /**
  5. * 商品逻辑接口
  6. * @author zjjt
  7. *
  8. */
  9. public interface IGoodsBiz {
  10. List<Goods>getAll();
  11. //查询单个
  12. Goods getOne(Integer id);
  13. }

GoodsBizImpl.java (商品逻辑接口实现类)  

  1. package com.zking.biz.impl;
  2. import java.util.List;
  3. import com.zking.biz.IGoodsBiz;
  4. import com.zking.dao.IGoodsDao;
  5. import com.zking.dao.impl.GoodsDaoImpl;
  6. import com.zking.pojo.Goods;
  7. /**
  8. * 商品逻辑接口实现类
  9. * @author zjjt
  10. *
  11. */
  12. public class GoodsBizImpl implements IGoodsBiz {
  13. private IGoodsDao goodsDao=new GoodsDaoImpl();
  14. @Override
  15. public List<Goods> getAll() {
  16. return goodsDao.getAll();
  17. }
  18. @Override
  19. public Goods getOne(Integer id) {
  20. return goodsDao.getOne(id);
  21. }
  22. }

IGoodsDao.java (商品数据访问接口)

  1. package com.zking.dao;
  2. import java.util.List;
  3. import com.zking.pojo.Goods;
  4. /**
  5. * 商品数据访问接口
  6. * @author zjjt
  7. *
  8. */
  9. public interface IGoodsDao {
  10. //查询所有
  11. List<Goods>getAll();
  12. //查询单个
  13. Goods getOne(Integer id);
  14. }

GoodsDaoImpl.java (商品数据访问接口实现类)  

  1. package com.zking.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.zking.dao.IGoodsDao;
  8. import com.zking.pojo.Goods;
  9. import com.zking.pojo.User;
  10. import com.zking.util.DBHelper;
  11. /**
  12. * 访问数据接口实现类
  13. * @author zjjt
  14. *
  15. */
  16. public class GoodsDaoImpl implements IGoodsDao{
  17. private Connection con;
  18. private PreparedStatement ps;
  19. private ResultSet rs;
  20. /**
  21. * 查询全部商品
  22. */
  23. @Override
  24. public List<Goods> getAll() {
  25. List<Goods>list=new ArrayList<Goods>();
  26. try {
  27. con=DBHelper.getCon();
  28. ps=con.prepareStatement("select * from shop_goods ");
  29. rs=ps.executeQuery();
  30. while(rs.next()) {
  31. Goods goods=new Goods();
  32. goods.setId(rs.getInt(1));
  33. goods.setName(rs.getString(2));
  34. goods.setPrice(rs.getInt(3));
  35. goods.setInfo(rs.getString(4));
  36. list.add(goods);
  37. }
  38. return list;
  39. } catch (Exception e) {
  40. e.printStackTrace();
  41. }finally {
  42. DBHelper.close(con, ps, rs);
  43. }
  44. return list;
  45. }
  46. /**
  47. * 根据id查询商品
  48. */
  49. @Override
  50. public Goods getOne(Integer id) {
  51. try {
  52. con=DBHelper.getCon();
  53. ps=con.prepareStatement("select * from shop_goods where id=? ");
  54. ps.setInt(1, id);
  55. rs=ps.executeQuery();
  56. if(rs.next()) {
  57. Goods goods=new Goods();
  58. goods.setId(rs.getInt(1));
  59. goods.setName(rs.getString(2));
  60. goods.setPrice(rs.getInt(3));
  61. goods.setInfo(rs.getString(4));
  62. return goods;
  63. }
  64. } catch (Exception e) {
  65. e.printStackTrace();
  66. }finally {
  67. DBHelper.close(con, ps, rs);
  68. }
  69. return null;
  70. }
  71. }

3、购物车添加&商品总数和价格的计算

doAddCar.java(处理添加购物车的页面)

  1. <%@page import="java.util.List"%>
  2. <%@page import="com.zking.biz.impl.GoodsBizImpl"%>
  3. <%@page import="com.zking.biz.IGoodsBiz"%>
  4. <%@page import="com.zking.vo.CarItem"%>
  5. <%@ page language="java" contentType="text/html; charset=UTF-8"
  6. pageEncoding="UTF-8"%>
  7. <%
  8. //添加购物车的页面
  9. //拿购物车
  10. List<CarItem>car=(List<CarItem>)session.getAttribute("car");
  11. IGoodsBiz goodsBiz=new GoodsBizImpl();
  12. //1、得知道是那件商品
  13. String str=request.getParameter("id");
  14. int id=-1;
  15. if(str!=null){
  16. id=Integer.parseInt(str);
  17. }
  18. //2-1 判断该商品是否存在
  19. boolean f=true;
  20. for(CarItem item:car){
  21. //item.getGoods().getId() 条目的商品id
  22. if(id==item.getGoods().getId()){
  23. //商品应该是已经被添加了[购物车中某个条目的商品id和你需要添加的商品id相同了]
  24. item.setCount(item.getCount()+1);//数量+1
  25. item.setSum(item.getCount()*item.getGoods().getPrice());
  26. f=false;
  27. break;
  28. }
  29. }
  30. //只要判断f是否发生了改变
  31. if(f){
  32. //2-2、生成一个CarItem[如果购物车没有该商品]
  33. CarItem carItem=new CarItem();
  34. //设置对应的商品数据
  35. carItem.setGoods(goodsBiz.getOne(id));
  36. //数量
  37. carItem.setCount(1);
  38. //加车数量*商品单价
  39. //carItem.getCount()商品加车的数量
  40. //carItem.getGoods().getPrice() 商品的单价
  41. carItem.setSum(carItem.getCount()*carItem.getGoods().getPrice());
  42. //将购物条目carItem 绑定到购物车
  43. car.add(carItem);
  44. }
  45. //更新购物车
  46. session.setAttribute("car", car);
  47. //跳回首页
  48. response.sendRedirect("index.jsp");
  49. %>

  CarItem.java(购物车中的每一项,每一个条目)

  1. package com.zking.vo;
  2. import com.zking.pojo.Goods;
  3. /**
  4. * 购物车中的每一项,每一个条目
  5. * @author zjjt
  6. *
  7. */
  8. public class CarItem {
  9. private Integer count;//数量
  10. private Integer sum;//条目总价
  11. private Goods goods;//对应的商品
  12. public Integer getCount() {
  13. return count;
  14. }
  15. public void setCount(Integer count) {
  16. this.count = count;
  17. }
  18. public Integer getSum() {
  19. return sum;
  20. }
  21. public void setSum(Integer sum) {
  22. this.sum = sum;
  23. }
  24. public Goods getGoods() {
  25. return goods;
  26. }
  27. public void setGoods(Goods goods) {
  28. this.goods = goods;
  29. }
  30. public CarItem(Integer count, Integer sum, Goods goods) {
  31. super();
  32. this.count = count;
  33. this.sum = sum;
  34. this.goods = goods;
  35. }
  36. public CarItem() {
  37. // TODO Auto-generated constructor stub
  38. }
  39. @Override
  40. public String toString() {
  41. return "CarItem [count=" + count + ", sum=" + sum + ", goods=" + goods + "]";
  42. }
  43. }

效果先简单打印到页面 之后会进行修改 

 


今天的分享就到这里结束啦!!😊

以上就是关于JavaWeb购物车项目的一些内容!!📚

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发