Web.14.三层架构之购物车项目01

一、数据库部分

create table shop_user(//用户表
       id number primary key,
       account varchar2(30) not null,
       password varchar2(30) not null

);

insert into shop_user values(0,'root','root123');

//插入数据后,记得一定要提交哦!
commit;

create table shop_goods(//商品表
       id number primary key,
       name varchar2(30) not null,
       price number default 0.0 ,
       info varchar2(255) default '三无产品' not null

)

二、eclipse部分

项目目录
在这里插入图片描述
在这里插入图片描述

1.util包中的DBHelper类的编写:

package com.zking.util;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
 
import com.sun.net.httpserver.Authenticator.Result;
 
import oracle.jdbc.driver.OracleDriver;
 
public class DBHelper {
	//加载驱动
	static {
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			
		} catch (Exception e) {
			e.printStackTrace();
			
		}
	}
	
	//定义字符串
	private static final String URL="jdbc:oracle:thin:@localhost:1521:orcl"; 
	
	//获得连接
	public static Connection getCon() {
		try {
			return DriverManager.getConnection(URL, "scott", "123");
			
		} catch (Exception e) {
			e.printStackTrace();
			
		}
		
		return null;
	}
	
	//关闭资源
	public static void close(Connection con,PreparedStatement ps,ResultSet rs) {
		try {
			if(con!=null&&!con.isClosed()) {
				con.close();
			}
			if(ps!=null) {
				ps.close();
				
			}
			if(rs!=null) {
				rs.close();
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			
		}
		
	}
 
	public static void main(String[] args) {
		System.out.println(DBHelper.getCon());
	}
	
}

登录页面 login.jsp的编写:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh">
 
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="${pageContext.request.contextPath }/bootstrap-3.3.7-dist/css/bootstrap.css">
    <script src="${pageContext.request.contextPath }/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
    <script src="${pageContext.request.contextPath }/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
    <style>
        * {
            outline: none !important;
        }
 
        html,
        body {
            background: #1abe9c;
        }
 
        form {
            width: 300px;
            background: #ebeff2;
            box-shadow: 0px 0px 50px rgba(0, 0, 0, .5);
            border-radius: 5px;
            padding: 20px;
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
        }
 
        .btn-group {
            width: 100%;
        }
 
        .btn-group button {
            width: 50%;
        }
    </style>
</head>
 
<body>
    <form action="doLogin.jsp" method="post">
        <h3 class="text-center" style="text-shadow: 2px 2px 1px #ed3f3f;">欢迎光临胡阿玛超市</h3>
        <div class="form-group">
            <input name="account" type="text" class="form-control" placeholder="请输入您的用户名">
        </div>
        <div class="form-group">
            <input name="password" type="password" class="form-control" placeholder="请输入您的密码">
        </div>
        <div class="btn-group">
            <button type="submit" class="btn btn-primary">登录</button>
            <button type="button" class="btn btn-danger" >没有账号?</button>
        </div>
    </form>
</body></html>

pojo包实体类 User 的编写:

package com.zking.pojo;
 
public class User {
	
	private Integer id;
	private String account;
	private String password;
	
	public User() {
		// TODO Auto-generated constructor stub
	}
 
	public User(Integer id, String account, String password) {
		super();
		this.id = id;
		this.account = account;
		this.password = password;
	}
 
	public Integer getId() {
		return id;
	}
 
	public void setId(Integer id) {
		this.id = id;
	}
 
	public String getAccount() {
		return account;
	}
 
	public void setAccount(String account) {
		this.account = account;
	}
 
	public String getPassword() {
		return password;
	}
 
	public void setPassword(String password) {
		this.password = password;
	}
 
	@Override
	public String toString() {
		return "User [id=" + id + ", account=" + account + ", password=" + password + "]";
	}
	
	
 
}

数据库访问层:

dao包中的IUserDao接口:

package com.zking.dao;
 
import com.zking.pojo.User;
 
public interface IUserDao {
	
	User login(User user);
 
}

dao.impl包中UserDaoImpl实现类的编写:

package com.zking.dao.impl;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
 
import com.zking.dao.IUserDao;
import com.zking.pojo.User;
import com.zking.util.DBHelper;
 
public class UserDaoImpl implements IUserDao{
 
	private Connection con;
	private PreparedStatement ps;
	private ResultSet rs;
	
	@Override
	public User login(User user) {
		
		try {
			con=DBHelper.getCon();
			ps=con.prepareStatement("select * from shop_user where account=?");
			ps.setString(1, user.getAccount());
			rs=ps.executeQuery();
			if(rs.next()) {
			//	System.out.println("------sadas");
				User u=new User();
				u.setId(rs.getInt(1));
				u.setAccount(rs.getString(2));
				u.setPassword(rs.getString(3));
				return u;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBHelper.close(con, ps, rs);
		}
		return null;
	}
 
}

业务逻辑层:

biz包中IUserBiz接口的编写:

package com.zking.biz;
 
import com.zking.pojo.User;
 
public interface IUserBiz {
	
	User login(User user);
 
}

修改后的web.xml文件中的代码:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>web14</display-name>
  <welcome-file-list>
  	<!-- / 就是根目录,localhost:8080 -->
    <welcome-file>/login.jsp</welcome-file>
  </welcome-file-list>
</web-app>

表示层:

具备处理登录功能的doLogin.jsp 的编写:

<%@page import="com.zking.vo.CarItem"%>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.zking.pojo.User"%>
<%@page import="com.zking.biz.impl.UserBizImpl"%>
<%@page import="com.zking.biz.IUserBiz"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	request.setCharacterEncoding("utf-8");
	String account=request.getParameter("account");
	String password=request.getParameter("password");
	
	IUserBiz userBiz=new UserBizImpl();
	User user=userBiz.login(new User(0,account,password));
 
	if(user==null){
		response.sendRedirect("login.jsp");
	}else{
		
		//首页需要登录数据
		session.setAttribute("user", user);
		
		//找来购物车
		List<CarItem> car=new ArrayList<>();
		//放到session中(把购物车给我)
		session.setAttribute("car", car);
		
		
		response.sendRedirect("index.jsp");
	}
 
%>

登陆成功后跳转到主页面 index.jsp 的编写:

<%@page import="com.zking.vo.CarItem"%>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.zking.pojo.User"%>
<%@page import="com.zking.biz.impl.UserBizImpl"%>
<%@page import="com.zking.biz.IUserBiz"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	request.setCharacterEncoding("utf-8");
	String account=request.getParameter("account");
	String password=request.getParameter("password");
	
	IUserBiz userBiz=new UserBizImpl();
	User user=userBiz.login(new User(0,account,password));
 
	if(user==null){
		response.sendRedirect("login.jsp");
	}else{
		
		//首页需要登录数据
		session.setAttribute("user", user);
		
		//找来购物车
		List<CarItem> car=new ArrayList<>();
		//放到session中(把购物车给我)
		session.setAttribute("car", car);
		
		
		response.sendRedirect("index.jsp");
	}
 
%>

商品对象 pojo包中的Goods实体类 的编写:

package com.zking.pojo;
 
public class Goods {
	
	private Integer id;
	private String name;
	private Integer price;
	private String info;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getPrice() {
		return price;
	}
	public void setPrice(Integer price) {
		this.price = price;
	}
	public String getInfo() {
		return info;
	}
	public void setInfo(String info) {
		this.info = info;
	}
	public Goods(Integer id, String name, Integer price, String info) {
		super();
		this.id = id;
		this.name = name;
		this.price = price;
		this.info = info;
	}
	@Override
	public String toString() {
		return "Goods [id=" + id + ", name=" + name + ", price=" + price + ", info=" + info + "]";
	}
 
	public Goods() {
		// TODO Auto-generated constructor stub
	}
	
 
}

数据库访问层:

dao包中IGoodsDao接口的编写:

package com.zking.dao;
 
import java.util.List;
 
import com.zking.pojo.Goods;
 
public interface IGoodsDao  {
		//查询所有
		List<Goods> getAll();
		//查询单个
		Goods getOne(Integer id);
}

业务逻辑层:

biz包中IGoodsBiz接口的编写:

package com.zking.biz;
 
import java.util.List;
 
import com.zking.pojo.Goods;
 
public interface IGoodsBiz {
	
	List<Goods> getAll();
	
	//查询单个
	Goods getOne(Integer id);
 
}

biz.impl包中GoodsBizImpl实现类的编写:

package com.zking.biz.impl;
 
import java.util.List;
 
import com.zking.biz.IGoodsBiz;
import com.zking.dao.IGoodsDao;
import com.zking.dao.impl.GoodsDaoImpl;
import com.zking.pojo.Goods;
 
public class GoodsBizImpl implements IGoodsBiz{
	
	private IGoodsDao goodsDao=new GoodsDaoImpl();
 
	@Override
	public List<Goods> getAll() {
		return goodsDao.getAll();
	}
 
	@Override
	public Goods getOne(Integer id) {
		return goodsDao.getOne(id);
	}
 
}

VO包:

view 试图,object 模型 / 对象,前端用的包

vo包中新建的CarItem类的意思是购物车选项(可以看成是显示在购物车的一行商品,但是不止有商品类Goods的属性),用于满足购物车的需求:需要商品的数量列和总价列

vo包中CarItem类的编写:

package com.zking.vo;
 
import com.zking.pojo.Goods;
 
//购物车中的每一项,每一个条目
public class CarItem {
	
	private Integer count;//数量
	private Integer sum;//条目总价
	private Goods goods;//对应的商品
	
	public Integer getCount() {
		return count;
	}
	public void setCount(Integer count) {
		this.count = count;
	}
	public Integer getSum() {
		return sum;
	}
	public void setSum(Integer sum) {
		this.sum = sum;
	}
	public Goods getGoods() {
		return goods;
	}
	public void setGoods(Goods goods) {
		this.goods = goods;
	}
	@Override
	public String toString() {
		return "CarItem [count=" + count + ", sum=" + sum + ", goods=" + goods + "]";
	}
	public CarItem(Integer count, Integer sum, Goods goods) {
		super();
		this.count = count;
		this.sum = sum;
		this.goods = goods;
	}
	
	public CarItem() {
		// TODO Auto-generated constructor stub
	}
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值