编写步骤:
- 数据库的设计(每个表都要序列)
- 编写包
- 开发的界面
- 购物车的具体实现代码
绑定主界面数据
加入到我的购物车,对商品数量的更改以及商品总价的计算
实现删除我的购物车商品的方法
修改商品数量
结账
订单
订单项
5.方法
1.数据库的设计
–用户表
create table sp_user(
uuid int primary key not null,
uname varchar2(25) not null,
upwd varchar2(25)not null,
address varchar2(50),
tel varchar2(50)
)
–商品表
create table sp_goods(
gid int primary key not null,
gname varchar2(25)not null,
gprice number
)
–订单表
create table sp_order(
ooid int primary key not null,
address varchar2(50),
orderPrice float,
uuid int
)
–订单明细表/订单项
create table sp_orderitem(
otid int primary key not null,
ooid int,
gid int,
scount int,
orderItemPrice float
)
–购物车表
create table sp_cart(
cid int primary key not null,
gname varchar2(25),
gprice float,
gcount int,
cprice float,
uuid int
)
–序列
create sequence 序列名;
–触发器
create or replace trigger 触发器名
before insert
on 表名
for each row
begin
:new. 要设置为标识列的字段:= 序列名.nextval;
end;
2.编写包
3.开发的界面
- 用户登录界面(index.jsp)
- 主页面(shopping.jsp)
- 购物车界面(myCart.jsp)
- 订单界面(myOrder.jsp)
- 订单详情界面(myOrderItem.jsp)
用户登录界面(index.jsp)
带下雪背景的登录注册页面 注册带下雪背景的登录注册页面
登录系统
这里注册如果你还没有一个账号.
© 2019 带下雪背景的登录注册页面 . All rights reserved | Design by Reserved
主页面(shopping.jsp)
Insert title here 我的购物车我的订单
商品序号 商品名称 商品单价 操作 ${g.gId } ${g.gName } ${g.gPrice }
购物车界面(myCart.jsp)
我的购物车 // 修改购物车里面商品数量和价格小计function update(cid,gname){// 参数 cid:文本框的id(购物车的序号);gname:商品名字// 获取到用户修改后的商品数据量var gcount = document.getElementById(cid).value;//console.log(cid+"_"+gcount);// 跳转到修改购物车商品数量的servlet:updateCartServlet.dolocation.href="/my_cart/updateCartServlet.do" />【我的购物车】
全选 序号 用户名 商品名称 商品单价 商品数量 商品总价 操作 ${c.cid} ${user.uname} ${c.goods.gName} ${c.goods.gPrice} ${c.cPrice} 删除修改
我要结账
返回购物首页
订单界面(myOrder.jsp)
Insert title here 我的订单!
订单编号 订单地址 订单总价 订单用户 操作 ${o.ooid } ${o.adress } ${o.sumprice } ${o.user.uname } ooid=${o.ooid}">查看详情
订单详情界面(myOrderItem.jsp)
Insert title here 订单详情
订单编号 订单项编号 订单名称 订单数量 订单项总价 ${oi.order.ooid } ${oi.otid } ${oi.goods.gName } ${oi.gcount } ${oi.mysum }
4.购物车的具体实现代码
登录数据处理页面
package com.zking.servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import com.zking.biz.IUserBiz;import com.zking.biz.impl.UserBiz;import com.zking.entity.User;@WebServlet("/indexServlet.do")public class IndexServlet extends HttpServlet {@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);}@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {try {HttpSession session = req.getSession();PrintWriter out = resp.getWriter();IUserBiz iub = new UserBiz();// 获取登录的用户名和密码String uname = req.getParameter("uname");String upwd = req.getParameter("upwd");/*System.out.println(uname);System.out.println(upwd);*/// 调用通过用户名获取用户对象,然后保存到sesssionUser user = iub.queryUserByUname(uname);boolean flag = iub.login(user);if(flag) {// 将获取的用户对象保存到session里面session.setAttribute("user", user);resp.sendRedirect(req.getContextPath()+"/home/shopping.jsp");}else {out.print("alert('账号或者密码有误,登录失败!');location.href='home/index.jsp'");}} catch (Exception e) {e.printStackTrace();}}}
1.主页面绑定数据
package com.zking.servlet;import java.io.IOException;import java.io.PrintWriter;import java.util.List;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import com.zking.biz.IGoodsBiz;import com.zking.biz.impl.GoodsBiz;import com.zking.entity.Goods;//@WebServlet("/loadDataServlet.do")public class LoadDataServlet extends HttpServlet {@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);}@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {HttpSession session = req.getSession();PrintWriter out = resp.getWriter();IGoodsBiz igb = new GoodsBiz();List listGoods = igb.list();session.setAttribute("listGoods", listGoods);//System.out.println(listGoods);resp.sendRedirect(req.getContextPath()+"/home/shopping.jsp");}}
2.点击加入购物车进入我的购物车页面
package com.zking.servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import com.zking.biz.ICartBiz;import com.zking.biz.IGoodsBiz;import com.zking.biz.impl.CartBiz;import com.zking.biz.impl.GoodsBiz;import com.zking.entity.Cart;import com.zking.entity.Goods;import com.zking.entity.User;@WebServlet("/addCartServlet.do")public class AddCartServlet extends HttpServlet{@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {try {IGoodsBiz igb = new GoodsBiz();ICartBiz icb = new CartBiz();// 创建sessoin和out对象HttpSession session = req.getSession();PrintWriter out = resp.getWriter();// 从session里面获取登录的用户对象User user = (User)session.getAttribute("user");// 获取前端提交的要加入到购物车的商品编号String gid = req.getParameter("gid");// 通过商品编号获取商品对象Goods goods = igb.query(Integer.parseInt(gid));//先通过用户id和商品名在购物车里面查询该商品是否已经存在Cart cart =icb.query(goods.getgName(), user);if(null==cart) {//当前用户没有购物车里面添加过该购物车(目前要添加到购物车的商品)// 创建一个购物车对象cart = new Cart();cart.setGoods(goods);cart.setgCount(1);// 每次加入一件商品到购物车cart.calcCPrice();// 计算购物车里面同一种商品价格小计cart.setUuid(user.getUuid());// 调用往购物车表里面增加数据的方法int n = icb.insert(cart);if(n>0) {// 跳转到购物车数据加载servletresp.sendRedirect(req.getContextPath()+"/loadCartDataServlet.do");}else {out.print("alert('加入购物车失败!');location.href='"+req.getContextPath()+"/home/shopping.jsp'");}}else {//否者该商品(目前要添加到个购物车的商品)已经存在购物车里面,只需要修改商品数量即可//调用通过用户id和商品名修改购物车里面商品数量的方法//计算修改后的商品的价格小计Double cprice =(cart.getgCount()+1) *goods.getgPrice();int i=icb.update(goods.getgName(), user, cart.getgCount()+1,cprice);if(i>0) {// 跳转到购物车数据加载servletresp.sendRedirect(req.getContextPath()+"/loadCartDataServlet.do");}else {out.print("alert('加入购物车失败!');location.href='"+req.getContextPath()+"/home/shopping.jsp'");} }} catch (Exception e) {e.printStackTrace();}}}
3.实现删除我的购物车商品的方法
package com.zking.servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import com.zking.biz.ICartBiz;import com.zking.biz.impl.CartBiz;import com.zking.entity.User;@WebServlet("/deleteCartServlet.do")public class DeleteCartServlet extends HttpServlet{@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// TODO Auto-generated method stubdoPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// TODO Auto-generated method stubtry {//创建session和out对象HttpSession session=req.getSession();PrintWriter out=resp.getWriter();ICartBiz icb=new CartBiz();//从session里面获取用户对象User user=(User)session.getAttribute("user");//获取前传过来的商品名字(购物车表里面没有商品编号字段)String gname=req.getParameter("gname");//调用删除购物车商品的方法int n=icb.delete(gname, user);if(n>0) {//删除成功// 跳转到购物车数据加载servletresp.sendRedirect(req.getContextPath()+"/loadCartDataServlet.do");}else {//删除删除//也可以直接跳转购物车首页(因删除失败,数据没有发生改变,不用去数据加载servlet里面从新加载数据)out.print("alert('删除失败!');location.href='"+req.getContextPath()+"/loadCartDataServlet.do'");} } catch (Exception e) {e.printStackTrace();}}}
4:修改商品
package com.zking.servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import com.zking.biz.ICartBiz;import com.zking.biz.impl.CartBiz;import com.zking.entity.Cart;import com.zking.entity.User;@WebServlet("/updateCartServlet.do")public class UpdateCartServlet extends HttpServlet{@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// TODO Auto-generated method stubdoPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// TODO Auto-generated method stubtry {//创建session和out对象HttpSession session=req.getSession();PrintWriter out=resp.getWriter();ICartBiz icb=new CartBiz();//从session里面获取用户对象User user=(User)session.getAttribute("user");String gname=req.getParameter("gname");Integer gcount=Integer.parseInt(req.getParameter("gcount"));Cart cart =icb.query(gname, user);int n=icb.update(gname, user, gcount, cart.getGoods().getgPrice()*gcount);if(n>0) {// 跳转到购物车数据加载servletresp.sendRedirect(req.getContextPath()+"/loadCartDataServlet.do");}else {out.print("alert('修改失败!');location.href='"+req.getContextPath()+"/loadCartDataServlet.do'");}} catch (Exception e) {e.printStackTrace();}}}
5:购物车数据绑定
package com.zking.servlet;import java.io.IOException;import java.io.PrintWriter;import java.util.List;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import com.zking.biz.ICartBiz;import com.zking.biz.IUserBiz;import com.zking.biz.impl.CartBiz;import com.zking.biz.impl.UserBiz;import com.zking.entity.Cart;import com.zking.entity.User;@WebServlet("/loadCartDataServlet.do")public class LoadCartDataServlet extends HttpServlet{@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {try {HttpSession session = req.getSession();PrintWriter out = resp.getWriter();ICartBiz icb = new CartBiz();// 从session里面获取当前登录的用户对象User user = (User)session.getAttribute("user");// 通过用户id从购物表里面获取该用户加入到购物车里面的所有商品List listCart = icb.query(user);// 将购物车集合 listCart保存到request里面req.setAttribute("listCart", listCart);//跳转到购物车页面req.getRequestDispatcher("/home/myCart.jsp").forward(req, resp);} catch (Exception e) {e.printStackTrace();}}}
6.:结账
package com.zking.servlet;import java.io.IOException;import java.io.PrintWriter;import java.util.ArrayList;import java.util.List;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import com.zking.biz.ICartBiz;import com.zking.biz.IGoodsBiz;import com.zking.biz.IOrderBiz;import com.zking.biz.impl.CartBiz;import com.zking.biz.impl.GoodsBiz;import com.zking.biz.impl.OrderBiz;import com.zking.entity.Cart;import com.zking.entity.Order;import com.zking.entity.OrderItem;import com.zking.entity.User;@WebServlet("/payMoneyServlet.do")public class PayMoneyServlet extends HttpServlet{@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {try {//商品的biz层IGoodsBiz igb = new GoodsBiz();//购物车的biz层ICartBiz icb = new CartBiz();//订单biz层IOrderBiz iob=new OrderBiz();// 创建sessoin和out对象HttpSession session = req.getSession();PrintWriter out = resp.getWriter();// 从session里面获取登录的用户对象User user = (User)session.getAttribute("user");//创建订单项集合List listOrderItem=new ArrayList();//1.通过用户编号在购物车表里面获取该用户的所有购物车数据List listCart=icb.query(user);//2.遍历购物车集合for (Cart cart : listCart) {//创建订单项对象OrderItem orderItem= new OrderItem();orderItem.setGoods(cart.getGoods());//商品对象orderItem.setGcount(cart.getgCount());//商品数量orderItem.setMysum(cart.getcPrice());//订单项的价格就是购物车里面的价格小计listOrderItem.add(orderItem);}//往订单表里面增加订单数据-调用增加订单的方法(在该方法里面已经调用了增加订单项的方法 )//订单:即一个订单里面包含了多个订单项//创建一个订单对象Order order=new Order();order.setAddress(user.getAddress());//订单地址,即用户的地址,也可以临时新增地址order.setUser(user);//下单用户order.setListOrderItem(listOrderItem);//订单项集合order.calcOrderPrice();//计算订单价格//调用增加订单的方法int n=iob.insert(order);if(n>0) {//结账成功,跳转到获取我的订单的servlet//清空购物车icb.delete(user);resp.sendRedirect(req.getContextPath()+"/myOrderServlet.do");}else {out.print("alert('结账失败!');location.href='"+req.getContextPath()+"/home/myCart.jsp'");}//往订单项表里面增加当前订单里面的所包含的所有订单单项数据-调用增加订单项的方法//清空当前用户的购物车表里面的数据-调用删除购物车数据的方法} catch (Exception e) {// TODO: handle exception}}}
7.查看订单项
package com.zking.servlet;import java.io.IOException;import java.io.PrintWriter;import java.util.List;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import com.zking.biz.ICartBiz;import com.zking.biz.IGoodsBiz;import com.zking.biz.IOrderBiz;import com.zking.biz.IOrderItemBiz;import com.zking.biz.impl.CartBiz;import com.zking.biz.impl.GoodsBiz;import com.zking.biz.impl.OrderBiz;import com.zking.biz.impl.OrderItemBiz;import com.zking.entity.OrderItem;import com.zking.entity.User;@WebServlet("/lookOrderItemServlet.do")public class LookOrderItemServlet extends HttpServlet{@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {try {//商品的biz层IGoodsBiz igb = new GoodsBiz();//购物车的biz层ICartBiz icb = new CartBiz();//订单biz层IOrderBiz iob=new OrderBiz();//订单项biz层IOrderItemBiz ioib=new OrderItemBiz();// 创建sessoin和out对象HttpSession session = req.getSession();PrintWriter out = resp.getWriter();// 从session里面获取登录的用户对象User user = (User)session.getAttribute("user");//获取前端传来的订单编号Integer ooid=Integer.parseInt(req.getParameter("ooid"));//调用通过订单编号获取订单项的方法aList listOrderItem=ioib.query(ooid);//将订单项集合保存到req作用域里面req.setAttribute("listOrderItem", listOrderItem);//跳转到我的订单详情里面req.getRequestDispatcher("/hoem/myOrderItem.jsp").forward(req, resp);} catch (Exception e) {e.printStackTrace();}}}
8:我的订单
package com.zking.servlet;import java.io.IOException;import java.io.PrintWriter;import java.util.List;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import com.zking.biz.ICartBiz;import com.zking.biz.IGoodsBiz;import com.zking.biz.IOrderBiz;import com.zking.biz.impl.CartBiz;import com.zking.biz.impl.GoodsBiz;import com.zking.biz.impl.OrderBiz;import com.zking.entity.Order;import com.zking.entity.User;@WebServlet("/myOrderServlet.do")public class MyOrderServlet extends HttpServlet{@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {try {//商品的biz层IGoodsBiz igb = new GoodsBiz();//购物车的biz层ICartBiz icb = new CartBiz();//订单biz层IOrderBiz iob=new OrderBiz();// 创建sessoin和out对象HttpSession session = req.getSession();PrintWriter out = resp.getWriter();// 从session里面获取登录的用户对象User user = (User)session.getAttribute("user");//调用通过用户编号获取用户所有订单的方法List listOrder=iob.query(user);req.setAttribute("listOrder", listOrder);//跳转到我的订单页面req.getRequestDispatcher("/home/myOrder.jsp").forward(req, resp); } catch (Exception e) {e.printStackTrace();}}}
方法
- 购物车dao包
- 用户dao包
- 订单dao包
- 订单想dao包
- 商品dao包
购物车dao包
package com.zking.dao.impl;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.ArrayList;import java.util.List;import javax.smartcardio.Card;import com.zking.biz.IGoodsBiz;import com.zking.biz.impl.GoodsBiz;import com.zking.dao.ICartDao;import com.zking.entity.Cart;import com.zking.entity.Goods;import com.zking.entity.User;import com.zking.util.DBhelper;public class CartDao implements ICartDao{private IGoodsBiz igb = new GoodsBiz();private List listCart;private PreparedStatement ps;private Connection con;private boolean flag;private ResultSet rs;private String sql;private Cart cart;private int n;@Overridepublic int insert(Cart cart) throws Exception{con = DBhelper.getCon();sql = "insert into sp_cart(gname,gprice,gcount,cprice,uuid)values(?,?,?,?,?)";ps = con.prepareStatement(sql);ps.setString(1, cart.getGoods().getgName());ps.setDouble(2, cart.getGoods().getgPrice());ps.setInt(3, cart.getgCount());ps.setDouble(4, cart.getcPrice());ps.setInt(5, cart.getUuid());n = ps.executeUpdate();return n;}@Overridepublic List query(User user) throws Exception{listCart = new ArrayList();con = DBhelper.getCon();sql = "select * from sp_cart where uuid = "+user.getUuid()+" order by cid";ps = con.prepareStatement(sql);rs = ps.executeQuery();while(rs.next()) {// 获取rs里面的商品名称String gname = rs.getString("gname");// 调用通过商品名称获取商品对象的方法Goods goods = igb.query(gname);cart = new Cart(rs.getInt("uuid"), rs.getInt("cid"), goods, rs.getInt("gcount"), rs.getDouble("cPrice"));listCart.add(cart);}return listCart;}@Overridepublic int update(String gname,User user,Integer gcount,double cprice)throws Exception{con=DBhelper.getCon();sql="update sp_cart set gcount =?,cprice=? where gname ='"+gname+"' "+"and uuid ="+user.getUuid();ps=con.prepareStatement(sql);ps.setInt(1, gcount);ps.setDouble(2, cprice);n=ps.executeUpdate();DBhelper.closeObj(con, ps, rs);return n;}@Overridepublic Cart query(String gname,User user)throws Exception{con=DBhelper.getCon();sql="select * from sp_cart where gname ='"+gname+"' and uuid ="+user.getUuid();ps=con.prepareStatement(sql);rs=ps.executeQuery();if(rs.next()) {cart=new Cart();Goods goods=new Goods();goods.setgNrice(rs.getDouble("gprice"));cart.setgCount(rs.getInt("gcount"));//只需要商品数量属性即可cart.setGoods(goods);}DBhelper.closeObj(con, ps, rs);return cart;}@Overridepublic int delete(String gname,User user)throws Exception{con=DBhelper.getCon();sql="delete from sp_cart where gname ='"+gname+"' and uuid="+user.getUuid();ps=con.prepareStatement(sql);n=ps.executeUpdate();DBhelper.closeObj(con, ps, rs);return n;}@Overridepublic int delete(User user) throws Exception{con=DBhelper.getCon();sql="delete from sp_cart where uuid="+user.getUuid();ps=con.prepareStatement(sql);n=ps.executeUpdate();DBhelper.closeObj(con, ps, rs);return n;}}
用户dao包
package com.zking.dao.impl;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import com.zking.dao.IUserDao;import com.zking.entity.User;import com.zking.util.DBhelper;public class UserDao implements IUserDao {private List listUser = new ArrayList();private PreparedStatement ps;private Connection con;private boolean flag;private ResultSet rs;private String sql;private User user;private int n;@Overridepublic boolean login(User user) {try {con = DBhelper.getCon();sql = "select * from sp_user where uname = ? and upwd = ?";ps = con.prepareStatement(sql);ps.setString(1, user.getUname());ps.setString(2, user.getUpwd());rs = ps.executeQuery();if(rs.next()) {flag = true;}} catch (Exception e) {e.printStackTrace();} finally {try {DBhelper.closeObj(con, ps, rs);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}return flag;}@Overridepublic User queryUserByUname(String uname) throws Exception{con = DBhelper.getCon();sql = "select * from sp_user where uname like '%"+uname+"%'";ps = con.prepareStatement(sql);rs = ps.executeQuery();if(rs.next()) {user = new User(rs.getInt("uuid"),rs.getString("uname"), rs.getString("upwd"), rs.getString("address"), rs.getString("tel"));}return user;}}
订单dao包
package com.zking.dao.impl;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.ArrayList;import java.util.List;import com.zking.biz.IGoodsBiz;import com.zking.biz.IOrderItemBiz;import com.zking.biz.impl.GoodsBiz;import com.zking.biz.impl.OrderItemBiz;import com.zking.dao.IOrderDao;import com.zking.entity.Order;import com.zking.entity.OrderItem;import com.zking.entity.User;import com.zking.util.DBhelper;public class OrderDao implements IOrderDao{private IOrderItemBiz ioib=new OrderItemBiz();private List listOrder;private PreparedStatement ps;private Connection con;private boolean flag;private ResultSet rs;private String sql;private Order order;private int n;private int maxOoid;//最大订单编号@Overridepublic int insert(Order order) throws Exception{con=DBhelper.getCon();sql="insert into sp_order(address,orderprice,uuid)values(?,?,?)";ps=con.prepareStatement(sql);ps.setString(1, order.getAddress());//订单地址ps.setDouble(2, order.getSumprice());//订单总价ps.setInt(3, order.getUser().getUuid());//用户idn=ps.executeUpdate();//如果订单项增加成功if(n>0) {//开始增加订单项,调用增加订单项的方法(把当前订单里面包含的所有订单项数据增加到订单项表里面)//1.获取该订单里面的所有订单项(订单里面的订单项集合属性)List listOrderItem=order.getListOrderItem();//2.遍历增加订单项的方法for (OrderItem oi : listOrderItem) {order.setOoid(getMaxOoid());//给订单对象赋订单编号属性oi.setOrder(order);//调用增加订单项的方法ioib.insert(oi);}}DBhelper.closeObj(con, ps, rs);return n;}@Overridepublic int getMaxOoid() throws Exception{con=DBhelper.getCon();sql="select max(ooid) from sp_order";ps=con.prepareStatement(sql);rs=ps.executeQuery();if(rs.next()) {maxOoid=rs.getInt(1);}DBhelper.closeObj(con, ps, rs);return maxOoid;}@Overridepublic List query(User user) throws Exception{listOrder =new ArrayList();con=DBhelper.getCon();sql="select * from from sp_order where uuid=? order by ooid ";ps=con.prepareStatement(sql);ps.setInt(1, user.getUuid());rs=ps.executeQuery();while(rs.next()) {order=new Order(rs.getInt("ooid"),rs.getString("address"),user,rs.getDouble("orderprice"));listOrder.add(order);}DBhelper.closeObj(con, ps, rs);return listOrder;}}
订单项dao包
package com.zking.dao.impl;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.ArrayList;import java.util.List;import com.zking.biz.IGoodsBiz;import com.zking.biz.impl.GoodsBiz;import com.zking.dao.IOrderItemDao;import com.zking.entity.Goods;import com.zking.entity.Order;import com.zking.entity.OrderItem;import com.zking.util.DBhelper;public class OrderItemDao implements IOrderItemDao{private IGoodsBiz igb = new GoodsBiz();private List listOrderItem;private PreparedStatement ps;private Connection con;private boolean flag;private ResultSet rs;private String sql;private OrderItem orderItem;private int n;@Overridepublic int insert(OrderItem orderItem) throws Exception{con=DBhelper.getCon();sql="insert into sp_orderitem(ooid,gname,gcount,orderitemprice)values(?,?,?,?)";ps=con.prepareStatement(sql);ps.setInt(1, orderItem.getOrder().getOoid());//订单编号ps.setString(2, orderItem.getGoods().getgName());//商品名字ps.setInt(3, orderItem.getGcount());//商品数量ps.setDouble(4, orderItem.getMysum());//订单项的价格n=ps.executeUpdate();DBhelper.closeObj(con, ps, rs);return n;}@Overridepublic List query(Integer ooid) throws Exception{listOrderItem =new ArrayList();con=DBhelper.getCon(); sql="select ooid,otid,gname,gcount,orderitemprice from sp_orderitem where "+"ooid=? order by otid";ps=con.prepareStatement(sql);ps.setInt(1, ooid);rs=ps.executeQuery();while(rs.next()) {Order order=new Order();order.setOoid(rs.getInt("ooid"));Goods goods=new Goods();goods.setgName(rs.getString("gname"));orderItem= new OrderItem();orderItem.setOrder(order);orderItem.setGoods(goods); orderItem.setOtid(rs.getInt("otid"));orderItem.setGcount(rs.getInt("gcount"));orderItem.setMysum(rs.getDouble("orderitempeice"));listOrderItem.add(orderItem);}DBhelper.closeObj(con, ps, rs);return listOrderItem;}}
商品dao包
package com.zking.dao.impl;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import com.zking.dao.IGoodsDao;import com.zking.entity.Goods;import com.zking.util.DBhelper;public class GoodsDao implements IGoodsDao {private List listGoods = new ArrayList();private PreparedStatement ps;private Connection con;private boolean flag;private ResultSet rs;private String sql;private Goods goods;private int n;@Overridepublic List list() {try {con = DBhelper.getCon();sql = "select * from sp_goods";ps = con.prepareStatement(sql);rs = ps.executeQuery();while(rs.next()) {goods = new Goods(rs.getInt(1), rs.getString(2), rs.getFloat(3));listGoods.add(goods);}} catch (Exception e) {e.printStackTrace();} finally {try {DBhelper.closeObj(con, ps, rs);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}return listGoods;}@Overridepublic Goods query(Integer gid) throws Exception{con = DBhelper.getCon();sql = "select * from sp_goods where gid = " + gid;ps = con.prepareStatement(sql);rs = ps.executeQuery();if(rs.next()) {goods = new Goods(rs.getInt(1),rs.getString(2),rs.getDouble(3));}return goods;}@Overridepublic Goods query(String gname) throws Exception{con = DBhelper.getCon();sql = "select * from sp_goods where gname = '"+gname+"'";ps = con.prepareStatement(sql);rs = ps.executeQuery();if(rs.next()) {goods = new Goods(rs.getInt(1),rs.getString(2),rs.getDouble(3));}return goods;}}