目录
1.三层架构
1.1什么是三层架构?
(1)表现层(UI):
(2)业务逻辑层:
(3)数据访问层:
2.表现层代码
2.1商品jsp页面
2.2购物车jsp页面
2.3绑定商品jsp
2.4删除购物车jsp
2.5用户登录jsp,连接数据库
2.6用户注册jsp,连接数据库
3.业务逻辑层(部分代码)
3.1商品表
4.数据访问层(部分代码)
4.1商品表
表现层也称为界面层,位于最外层(最上层),离用户最近。用于显示数据和接收用户输入的数据,为用户提供一种交互式操作的界面。
BLL:(业务逻辑层):UI层和DAL层之间的桥梁。实现业务逻辑。业务逻辑具体包含:验证、计算、业务规则等等。
DAL:(数据访问层):与数据库打交道。主要实现对数据的增、删、改、查。将存储在数据库中的数据提交给业务层,同时将业务层处理的数据保存到数据库。(当然这些操作都是基于UI层的。用户的需求反映给界面(UI),UI反映给BLL,BLL反映给DAL,DAL进行数据的操作,操作后再一一返回,直到将用户所需数据反馈给用户)
也就是视图的模样,jsp
<%@page import="com.zking.biz.imp.GoodsBiz_imp"%>
<%@page import="com.zking.biz.GoodsBiz"%>
<%@page import="com.zking.entitl.Goods"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.zking.dao.GoodsDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
商品编号
商品名称
商品单价
商品介绍
商品图片
操作一下
<%
GoodsBiz gb = new GoodsBiz_imp();
ArrayList list = gb.GoodsgetAll();
for (Goods gs : list) {
%>
<%=gs.getBid()%>
<%=gs.getBname()%>
<%=gs.getBprice()%>
<%=gs.getBinfo()%>
<%
}
%>
<%@page import="com.zking.biz.imp.GoodsBiz_imp"%>
<%@page import="com.zking.biz.GoodsBiz"%>
<%@page import="com.zking.entitl.Goods"%>
<%@page import="com.zking.biz.imp.UserBiz_imp"%>
<%@page import="com.zking.entitl.User"%>
<%@page import="com.zking.biz.UserBiz"%>
<%@page import="com.zking.biz.imp.ShopcarBiz_imp"%>
<%@page import="com.zking.biz.ShopcarBiz"%>
<%@page import="com.zking.entitl.ShopCar"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
返回商品页
商品编号
商品单价
商品数量
商品图片
商品总价
操作一下
<%
Object user = session.getAttribute("user");
User u = (User) user;
ShopcarBiz scb = new ShopcarBiz_imp();
ArrayList list = scb.ShopAll(u.getUuid());
for (ShopCar sc : list) {
%>
<%
Goods gs = new Goods();
GoodsBiz gd = new GoodsBiz_imp();
int bid = sc.getBid();
gs = gd.goodsOne(bid);
%>
<%=gs.getBname()%>
<%=gs.getBprice()%>
<%=sc.getGsumprice()%>
删除
<%
}
%>
<%@page import="com.zking.entitl.Goods"%>
<%@page import="com.zking.biz.imp.ShopcarBiz_imp"%>
<%@page import="com.zking.biz.ShopcarBiz"%>
<%@page import="com.zking.entitl.User"%>
<%@page import="com.zking.biz.imp.GoodsBiz_imp"%>
<%@page import="com.zking.biz.GoodsBiz"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.zking.dao.GoodsDao"%>
<%@page import="com.zking.entitl.ShopCar"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
//获取商品数量
String ccount = request.getParameter("count");
int count = 1;
if (ccount != null) {
count = Integer.valueOf(ccount);
}
//获取到商品id
String bbid = request.getParameter("bid");
int bid = Integer.valueOf(bbid);
//实例化dao类对象
GoodsBiz gb = new GoodsBiz_imp();
//根据id获取到goods的信息
gb.goodsOne(bid);
ShopcarBiz sb = new ShopcarBiz_imp();
//获取用户信息
Object user = session.getAttribute("user");
User u = (User)user;
//根据id获取到商品信息添加到goods中
Goods gd = gb.goodsOne(bid);
//实例化shopcar对象
ShopCar sp = new ShopCar();
//给订单中的商品编号赋值
sp.setBid(gd.getBid());
//给订单中的商品数量赋值
sp.setGnumber(1);
//给订单中的用户id赋值
sp.setUuid(u.getUuid());
//获取goods中的商品单价
int sprice = gd.getBprice();
//给订单中的总价赋值
sp.setGsumprice(sprice*sp.getGnumber());
//给订单状态赋值
sp.setOzt("false");
//根据用户id获取到订单集合
ArrayList list = sb.ShopAllById(bid);
boolean a = true;
for(int i = 0;i
<%@page import="com.zking.entitl.User"%>
<%@page import="com.zking.biz.imp.ShopcarBiz_imp"%>
<%@page import="com.zking.biz.ShopcarBiz"%>
<%@page import="com.zking.entitl.ShopCar"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.zking.dao.GoodsDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String ooid = request.getParameter("bid");
int oid = Integer.valueOf(ooid);
ShopcarBiz sb = new ShopcarBiz_imp();
sb.Shopdel(oid);
response.sendRedirect("Shopcar.jsp");
%>
<%@page import="com.zking.entitl.User"%>
<%@page import="com.zking.biz.imp.UserBiz_imp"%>
<%@page import="com.zking.biz.UserBiz"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String uname = request.getParameter("uname");
String upwd = request.getParameter("upwd");
UserBiz ub = new UserBiz_imp();
User u = ub.login(uname, upwd);
System.out.print(u);
if(u!=null){
session.setAttribute("user", u);
request.getRequestDispatcher("index.jsp").forward(request, response);
}else{
out.print("");
}
%>
<%@page import="com.zking.entitl.User"%>
<%@page import="com.zking.biz.imp.UserBiz_imp"%>
<%@page import="com.zking.biz.UserBiz"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String uname = request.getParameter("uname");
String upwd = request.getParameter("upwd");
String upwds = request.getParameter("upwds");
if(upwd.equals( upwds)){
UserBiz ub = new UserBiz_imp();
User u = new User(uname,upwd);
boolean i = ub.register(u);
if(i == true){
out.print("");
request.getRequestDispatcher("登录页面.jsp").forward(request, response);
}else{
out.print("");
}
}else{
out.print("");
}
%>
package com.zking.biz;
import java.util.ArrayList;
import com.zking.entitl.Goods;
public interface GoodsBiz {
/**
* 获取商品的所有的商品信息
* @return 成功返回信息集合,失败为null
*/
public ArrayList GoodsgetAll();
/**
* 根据id获取商品信息
* @param 商品id
* @return 成功返回商品信息,失败为null
*/
public Goods goodsOne(int bid);
}
package com.zking.biz.imp;
import java.util.ArrayList;
import com.zking.biz.GoodsBiz;
import com.zking.dao.GoodsDao;
import com.zking.dao.imp.GoodsDao_Imp;
import com.zking.entitl.Goods;
public class GoodsBiz_imp implements GoodsBiz {
GoodsDao gd = new GoodsDao_Imp();
@Override
public ArrayList GoodsgetAll() {
// TODO Auto-generated method stub
return gd.GoodsgetAll();
}
@Override
public Goods goodsOne(int bid) {
// TODO Auto-generated method stub
return gd.goodsOne(bid);
}
}
package com.zking.dao;
import java.util.ArrayList;
import com.zking.entitl.Goods;
public interface GoodsDao {
/**
* 获取商品的所有的商品信息
* @return 成功返回信息集合,失败为null
*/
public ArrayList GoodsgetAll();
/**
* 根据id获取商品信息
* @param 商品id
* @return 成功返回商品信息,失败为null
*/
public Goods goodsOne(int bid);
}
package com.zking.dao.imp;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import com.zking.dao.GoodsDao;
import com.zking.entitl.Goods;
import com.zking.util.DBHelper;
public class GoodsDao_Imp implements GoodsDao{
public ArrayList GoodsgetAll() {
ArrayList list = new ArrayList<>();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = DBHelper.getcon();
ps = con.prepareStatement("select * from goods");
rs = ps.executeQuery();
while (rs.next()) {
int bid = rs.getInt(1);
String bname = rs.getString(2);
int bprice = rs.getInt(3);
String binfo = rs.getString(4);
String bface = rs.getString(5);
Goods gs = new Goods(bid, bname, bprice, binfo, bface);
list.add(gs);
}
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
} finally {
DBHelper.getclose(con, ps, rs);
}
return list;
}
public Goods goodsOne(int bid) {
Goods gs = null;
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = DBHelper.getcon();
ps = con.prepareStatement("select * from goods where bid=" + bid);
rs = ps.executeQuery();
if (rs.next()) {
String bname = rs.getString(2);
int bprice = rs.getInt(3);
String binfo = rs.getString(4);
String bface = rs.getString(5);
gs = new Goods(bid, bname, bprice, binfo, bface);
}
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
} finally {
DBHelper.getclose(con, ps, rs);
}
return gs;
}
}