Jsp电子商务之七 订单篇2
【摘要】
从View页面,点击超链接查询订单,进入到控制器
OrderlistServlet
package com.cart.web;
import java.io.IOException;import java.util.*; import javax.servlet.ServletException;import javax.ser...
从View页面,点击超链接查询订单,进入到控制器
OrderlistServlet
package com.cart.web;
-
-
import java.io.IOException;
-
import java.util.*;
-
-
import javax.servlet.ServletException;
-
import javax.servlet.http.HttpServlet;
-
import javax.servlet.http.HttpServletRequest;
-
import javax.servlet.http.HttpServletResponse;
-
import javax.servlet.http.HttpSession;
-
-
import com.cart.entity.*;
-
import com.cart.service.OrderService;
-
-
public class OrderlistServlet extends HttpServlet {
-
@Override
-
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
-
throws ServletException, IOException {
-
//从View来获取查询订单;
-
OrderService orderService=new OrderService();
-
Map<Order,ArrayList<Book>> map=(Map<Order,ArrayList<Book>>)orderService.getOrders(null, null);
-
HttpSession session=req.getSession();
-
session.setAttribute("map",map); //将订单map存放到session中;
-
//Iterator it=map.entrySet().iterator();
-
//System.out.println(map.entrySet().size());//长度为2是对的;
-
//下面如何获取订单对象和对应的图书详情信息呢;
-
//后台测试代码
-
// Order key;
-
// ArrayList<Book> value;
-
// while(it.hasNext()){
-
// Map.Entry<Order,ArrayList<Book>>entry=(Map.Entry<Order,ArrayList<Book>>)it.next();
-
// key=entry.getKey();
-
// value=entry.getValue();
-
//
-
// System.out.println("订单"+key.getId());
-
// System.out.println("书名\t\t价格\t数量\t总价");
-
// for(Book book:value){
-
// System.out.println(book.getBookname()+"/"+book.getImage()+"/"+book.getPrice()+"/"+book.getCount()+"/"+key.getTotal());
-
// }
-
// System.out.println();
-
// }
-
resp.sendRedirect("orderlist.jsp");
-
}
-
@Override
-
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
-
throws ServletException, IOException {
-
this.doGet(req, resp);
-
}
-
}
-
package com.cart.service;
-
-
import java.sql.*;
-
import java.util.ArrayList;
-
import java.util.HashMap;
-
import java.util.Map;
-
-
import com.cart.dao.*;
-
import com.cart.dao.impl.*;
-
import com.cart.entity.Book;
-
import com.cart.entity.Order;
-
import com.cart.entity.Userinfo;
-
import com.cart.util.DaoFactory;
-
import com.cart.util.PageControler;
-
-
public class OrderService {
-
//1.定义属性:对dao进行操作;
-
OrderDao orderDao=new OrderDaoImpl();
-
CartDao cartDao=new CartDaoImpl();
-
//2.对订单,进行增删该查
-
public void delCartAndSaveOrder(String sbid,Userinfo user,Order order){
-
//使用事务操作,业务层可以这样操作,对不同的dao操作;
-
Connection con=null;
-
PreparedStatement pstmt=null;
-
try{
-
StringBuilder sbSql=new StringBuilder();
-
sbSql.append("delete from cartitem where uid=? and bid in(");
-
sbSql.append(sbid);
-
sbSql.append(")");
-
String sql=sbSql.toString();
-
-
con=DaoFactory.getConnection();
-
pstmt=con.prepareStatement(sql);
-
con.setAutoCommit(false);//设置自动提交事务,为false
-
orderDao.insertOrder(order);//调用杀入订单方法;,同时删除原来的购物车数据方法;
-
Object[] params={user.getId()};
-
DaoFactory.setParams(pstmt, params);
-
DaoFactory.executeUpdate(sql, params);
-
-
System.out.println(sbSql);
-
-
con.commit();
-
-
}catch(Exception e){
-
e.printStackTrace();
-
}finally{
-
DaoFactory.closeAll(null, pstmt, con);
-
}
-
}
-
public int insertOrder(Order order){
-
//ct.deletecartItem(uid, bid);
-
return orderDao.insertOrder(order);
-
}
-
public int updateOrder(int id){
-
return orderDao.updateOrder(id);
-
}
-
public int deleteOrder(int id){
-
return orderDao.deleteOrder(id);
-
}
-
-
public Order findOrderById(int id){
-
return orderDao.findOrderById(id);
-
}
-
public Map <Order,ArrayList<Book>> getOrders(String sql, PageControler pager) {//获取多条订单并带有详情
-
Connection con = null;
-
Map <Order,ArrayList<Book>> rtn = new HashMap <Order,ArrayList<Book>>();
-
try {
-
con = DaoFactory.getConnection();
-
OrderDao orderDao = new OrderDaoImpl();
-
rtn = orderDao.getOrders(sql, pager);
-
} catch (SQLException e) {
-
e.printStackTrace();
-
} finally {
-
DaoFactory.closeAll(null, null, con);
-
}
-
return rtn;
-
}
-
}
-
package com.cart.dao.impl;
-
-
import java.sql.*;
-
import java.util.ArrayList;
-
import java.util.HashMap;
-
import java.util.List;
-
import java.util.Map;
-
-
import com.cart.dao.OrderDao;
-
import com.cart.entity.Book;
-
import com.cart.entity.Order;
-
import com.cart.util.DaoFactory;
-
import com.cart.util.PageControler;
-
-
public class OrderDaoImpl implements OrderDao{
-
PreparedStatement pstmt=null;
-
Connection con=null;
-
-
public int deleteOrder(int id) {
-
return 0;
-
}
-
-
public Order findOrderById(int id) {
-
return null;
-
}
-
-
public int insertOrder(Order order) {
-
//将你所获得的订单数据,插入到数据库;
-
int result=-1;
-
try {
-
String sql="insert orders(userid,createtime,total,address,phone,status)values(?,?,?,?,?,?)";
-
con=DaoFactory.getConnection();
-
pstmt=con.prepareStatement(sql);
-
Object[] params={order.getUserid(),order.getCreatetime(),order.getTotal(),order.getShipaddress(),order.getContactphone(),order.getStatus()};
-
DaoFactory.setParams(pstmt, params);
-
-
result=pstmt.executeUpdate();
-
-
} catch (SQLException e) {
-
e.printStackTrace();
-
}finally{
-
DaoFactory.closeAll(null, pstmt, con);
-
}
-
return result;
-
}
-
-
public int updateOrder(int id) {
-
return 0;
-
}
-
、、//参数用于以后代码扩展
-
public Map<Order, ArrayList<Book>> getOrders(String sqlCon,
-
PageControler pager) throws SQLException {
-
List<Order> rtn = new ArrayList<Order>(); //可能会存在1个客户有多个订单,对吧;
-
//String sql="select O.*,OD.* from orders O inner join orderdetails OD on O.id=OD.oid";
-
String sql="select O.id,o.createtime,o.total,od.count,od.transprice,b.bookname,b.price,b.image" +
-
" from orders o inner join orderdetails od on od.oid=o.id inner join book b on b.id=od.bid";
-
//新的三表连接,取得book的值;
-
con=DaoFactory.getConnection();
-
pstmt=con.prepareStatement(sql);
-
ResultSet rs=pstmt.executeQuery();//执行结果放rs结果集;
-
Map<Order,ArrayList<Book>> map=new HashMap<Order,ArrayList<Book>>();
-
-
Order order=new Order();//订单对象,用于接收客户的多个订单项;
-
while(rs.next()){
-
int id=rs.getInt("id");//记录上次的一个id;6,如果和现在的id一致,则视为一个order
-
//直接继续,添加书本;
-
-
if(id!=order.getId()){
-
order.setId(rs.getInt("id"));
-
order.setCreatetime(rs.getDate("createtime"));
-
order.setTotal(rs.getDouble("total"));
-
}
-
-
//这个可以做好,不同订单,对应不同商品即1:1关系;不适合1:M关系;
-
//到此为止,已经获得了,订单的关键要素点,下面需要获得相应的订单,对应的商品详情了;
-
Book book=new Book();
-
book.setCount(rs.getInt("count"));
-
book.setPrice(rs.getFloat("transprice"));
-
book.setImage(rs.getString("image"));
-
book.setBookname(rs.getString("bookname"));
-
-
//设置图书表,然后添加到ArrayList里面;
-
if(map.get(order)==null){
-
ArrayList list=new ArrayList();
-
list.add(book);
-
map.put(order, list);
-
}else{
-
//应该在order不为空的情况下判断,是否重复;
-
ArrayList list=map.get(order);
-
list.add(book);
-
map.put(order, list);
-
}
-
}
-
DaoFactory.closeAll(rs, pstmt, con);
-
return map;
-
}
-
-
}
针对出现多个商品进入到一个订单的问题,已经给予解决了,其中的业务代码还可以继续修改,要继续等待哦。
-
OrderDaoImpl
-
public Map<Order, ArrayList<ShoppingItem>> getOrders(String condition,
-
PageControler pager) throws SQLException {
-
shoppingService ss = new shoppingService();
-
List<Order> rtn = new ArrayList<Order>(); //可能会存在1个客户有多个订单
-
String sql="select o.id,o.submittime,o.totalprice,b.bid,b.point,b.stock,di.bcount,b.bookname,b.b_price from d_order o inner join d_items di on di.orderId=o.id inner join d_books b on b.bid=di.bid";
-
//新的三表连接,取得book的值;
-
con=DaoFactory.getConnection();
-
pstmt=con.prepareStatement(sql);
-
ResultSet rs=pstmt.executeQuery();//执行结果放rs结果集;
-
Map<Order,ArrayList<ShoppingItem>> map=new HashMap<Order,ArrayList<ShoppingItem>>();
-
-
Order order=null;//订单对象,用于接收客户的多个订单项;
-
while(rs.next()){
-
if(order==null){
-
order=new Order(); //1.第一次,正常;
-
order.setId(rs.getInt("id"));
-
order.setSubmittime(rs.getString("submittime"));
-
order.setTotalprice(rs.getString("totalprice"));
-
ShoppingItem si = ss.declareShoppingItem(rs);
-
ArrayList list=new ArrayList();
-
list.add(si);
-
map.put(order, list);
-
}else{
-
if(order.getId()!=rs.getInt("id")){
-
order=new Order();
-
order.setId(rs.getInt("id"));
-
order.setSubmittime(rs.getString("submittime"));
-
order.setTotalprice(rs.getString("totalprice"));
-
ShoppingItem si = ss.declareShoppingItem(rs);
-
ArrayList list=new ArrayList();
-
list.add(si);
-
map.put(order, list);
-
}else{//证明是同一个订单;
-
ShoppingItem si = ss.declareShoppingItem(rs);
-
ArrayList list=map.get(order);
-
list.add(si);
-
map.put(order, list);
-
}
-
}
-
}
-
DaoFactory.closeAll(rs, pstmt, con);
-
return map;
-
}
文章来源: aaaedu.blog.csdn.net,作者:tea_year,版权归原作者所有,如需转载,请联系作者。
原文链接:aaaedu.blog.csdn.net/article/details/53706475
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)