Jsp电子商务之七 订单篇2

举报
tea_year 发表于 2021/12/30 01:01:20 2021/12/30
【摘要】 从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;
 


  
  1. import java.io.IOException;
  2. import java.util.*;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import javax.servlet.http.HttpSession;
  8. import com.cart.entity.*;
  9. import com.cart.service.OrderService;
  10. public class OrderlistServlet extends HttpServlet {
  11. @Override
  12. protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  13. throws ServletException, IOException {
  14. //从View来获取查询订单;
  15. OrderService orderService=new OrderService();
  16. Map<Order,ArrayList<Book>> map=(Map<Order,ArrayList<Book>>)orderService.getOrders(null, null);
  17. HttpSession session=req.getSession();
  18. session.setAttribute("map",map); //将订单map存放到session中;
  19. //Iterator it=map.entrySet().iterator();
  20. //System.out.println(map.entrySet().size());//长度为2是对的;
  21. //下面如何获取订单对象和对应的图书详情信息呢;
  22. //后台测试代码
  23. // Order key;
  24. // ArrayList<Book> value;
  25. // while(it.hasNext()){
  26. // Map.Entry<Order,ArrayList<Book>>entry=(Map.Entry<Order,ArrayList<Book>>)it.next();
  27. // key=entry.getKey();
  28. // value=entry.getValue();
  29. //
  30. // System.out.println("订单"+key.getId());
  31. // System.out.println("书名\t\t价格\t数量\t总价");
  32. // for(Book book:value){
  33. // System.out.println(book.getBookname()+"/"+book.getImage()+"/"+book.getPrice()+"/"+book.getCount()+"/"+key.getTotal());
  34. // }
  35. // System.out.println();
  36. // }
  37. resp.sendRedirect("orderlist.jsp");
  38. }
  39. @Override
  40. protected void doPost(HttpServletRequest req, HttpServletResponse resp)
  41. throws ServletException, IOException {
  42. this.doGet(req, resp);
  43. }
  44. }


  
  1. package com.cart.service;
  2. import java.sql.*;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import com.cart.dao.*;
  7. import com.cart.dao.impl.*;
  8. import com.cart.entity.Book;
  9. import com.cart.entity.Order;
  10. import com.cart.entity.Userinfo;
  11. import com.cart.util.DaoFactory;
  12. import com.cart.util.PageControler;
  13. public class OrderService {
  14. //1.定义属性:对dao进行操作;
  15. OrderDao orderDao=new OrderDaoImpl();
  16. CartDao cartDao=new CartDaoImpl();
  17. //2.对订单,进行增删该查
  18. public void delCartAndSaveOrder(String sbid,Userinfo user,Order order){
  19. //使用事务操作,业务层可以这样操作,对不同的dao操作;
  20. Connection con=null;
  21. PreparedStatement pstmt=null;
  22. try{
  23. StringBuilder sbSql=new StringBuilder();
  24. sbSql.append("delete from cartitem where uid=? and bid in(");
  25. sbSql.append(sbid);
  26. sbSql.append(")");
  27. String sql=sbSql.toString();
  28. con=DaoFactory.getConnection();
  29. pstmt=con.prepareStatement(sql);
  30. con.setAutoCommit(false);//设置自动提交事务,为false
  31. orderDao.insertOrder(order);//调用杀入订单方法;,同时删除原来的购物车数据方法;
  32. Object[] params={user.getId()};
  33. DaoFactory.setParams(pstmt, params);
  34. DaoFactory.executeUpdate(sql, params);
  35. System.out.println(sbSql);
  36. con.commit();
  37. }catch(Exception e){
  38. e.printStackTrace();
  39. }finally{
  40. DaoFactory.closeAll(null, pstmt, con);
  41. }
  42. }
  43. public int insertOrder(Order order){
  44. //ct.deletecartItem(uid, bid);
  45. return orderDao.insertOrder(order);
  46. }
  47. public int updateOrder(int id){
  48. return orderDao.updateOrder(id);
  49. }
  50. public int deleteOrder(int id){
  51. return orderDao.deleteOrder(id);
  52. }
  53. public Order findOrderById(int id){
  54. return orderDao.findOrderById(id);
  55. }
  56. public Map <Order,ArrayList<Book>> getOrders(String sql, PageControler pager) {//获取多条订单并带有详情
  57. Connection con = null;
  58. Map <Order,ArrayList<Book>> rtn = new HashMap <Order,ArrayList<Book>>();
  59. try {
  60. con = DaoFactory.getConnection();
  61. OrderDao orderDao = new OrderDaoImpl();
  62. rtn = orderDao.getOrders(sql, pager);
  63. } catch (SQLException e) {
  64. e.printStackTrace();
  65. } finally {
  66. DaoFactory.closeAll(null, null, con);
  67. }
  68. return rtn;
  69. }
  70. }


  
  1. package com.cart.dao.impl;
  2. import java.sql.*;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import com.cart.dao.OrderDao;
  8. import com.cart.entity.Book;
  9. import com.cart.entity.Order;
  10. import com.cart.util.DaoFactory;
  11. import com.cart.util.PageControler;
  12. public class OrderDaoImpl implements OrderDao{
  13. PreparedStatement pstmt=null;
  14. Connection con=null;
  15. public int deleteOrder(int id) {
  16. return 0;
  17. }
  18. public Order findOrderById(int id) {
  19. return null;
  20. }
  21. public int insertOrder(Order order) {
  22. //将你所获得的订单数据,插入到数据库;
  23. int result=-1;
  24. try {
  25. String sql="insert orders(userid,createtime,total,address,phone,status)values(?,?,?,?,?,?)";
  26. con=DaoFactory.getConnection();
  27. pstmt=con.prepareStatement(sql);
  28. Object[] params={order.getUserid(),order.getCreatetime(),order.getTotal(),order.getShipaddress(),order.getContactphone(),order.getStatus()};
  29. DaoFactory.setParams(pstmt, params);
  30. result=pstmt.executeUpdate();
  31. } catch (SQLException e) {
  32. e.printStackTrace();
  33. }finally{
  34. DaoFactory.closeAll(null, pstmt, con);
  35. }
  36. return result;
  37. }
  38. public int updateOrder(int id) {
  39. return 0;
  40. }
  41. 、、//参数用于以后代码扩展
  42. public Map<Order, ArrayList<Book>> getOrders(String sqlCon,
  43. PageControler pager) throws SQLException {
  44. List<Order> rtn = new ArrayList<Order>(); //可能会存在1个客户有多个订单,对吧;
  45. //String sql="select O.*,OD.* from orders O inner join orderdetails OD on O.id=OD.oid";
  46. String sql="select O.id,o.createtime,o.total,od.count,od.transprice,b.bookname,b.price,b.image" +
  47. " from orders o inner join orderdetails od on od.oid=o.id inner join book b on b.id=od.bid";
  48. //新的三表连接,取得book的值;
  49. con=DaoFactory.getConnection();
  50. pstmt=con.prepareStatement(sql);
  51. ResultSet rs=pstmt.executeQuery();//执行结果放rs结果集;
  52. Map<Order,ArrayList<Book>> map=new HashMap<Order,ArrayList<Book>>();
  53. Order order=new Order();//订单对象,用于接收客户的多个订单项;
  54. while(rs.next()){
  55. int id=rs.getInt("id");//记录上次的一个id;6,如果和现在的id一致,则视为一个order
  56. //直接继续,添加书本;
  57. if(id!=order.getId()){
  58. order.setId(rs.getInt("id"));
  59. order.setCreatetime(rs.getDate("createtime"));
  60. order.setTotal(rs.getDouble("total"));
  61. }
  62. //这个可以做好,不同订单,对应不同商品即1:1关系;不适合1:M关系;
  63. //到此为止,已经获得了,订单的关键要素点,下面需要获得相应的订单,对应的商品详情了;
  64. Book book=new Book();
  65. book.setCount(rs.getInt("count"));
  66. book.setPrice(rs.getFloat("transprice"));
  67. book.setImage(rs.getString("image"));
  68. book.setBookname(rs.getString("bookname"));
  69. //设置图书表,然后添加到ArrayList里面;
  70. if(map.get(order)==null){
  71. ArrayList list=new ArrayList();
  72. list.add(book);
  73. map.put(order, list);
  74. }else{
  75. //应该在order不为空的情况下判断,是否重复;
  76. ArrayList list=map.get(order);
  77. list.add(book);
  78. map.put(order, list);
  79. }
  80. }
  81. DaoFactory.closeAll(rs, pstmt, con);
  82. return map;
  83. }
  84. }

针对出现多个商品进入到一个订单的问题,已经给予解决了,其中的业务代码还可以继续修改,要继续等待哦。


  
  1. OrderDaoImpl
  2. public Map<Order, ArrayList<ShoppingItem>> getOrders(String condition,
  3. PageControler pager) throws SQLException {
  4. shoppingService ss = new shoppingService();
  5. List<Order> rtn = new ArrayList<Order>(); //可能会存在1个客户有多个订单
  6. 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";
  7. //新的三表连接,取得book的值;
  8. con=DaoFactory.getConnection();
  9. pstmt=con.prepareStatement(sql);
  10. ResultSet rs=pstmt.executeQuery();//执行结果放rs结果集;
  11. Map<Order,ArrayList<ShoppingItem>> map=new HashMap<Order,ArrayList<ShoppingItem>>();
  12. Order order=null;//订单对象,用于接收客户的多个订单项;
  13. while(rs.next()){
  14. if(order==null){
  15. order=new Order(); //1.第一次,正常;
  16. order.setId(rs.getInt("id"));
  17. order.setSubmittime(rs.getString("submittime"));
  18. order.setTotalprice(rs.getString("totalprice"));
  19. ShoppingItem si = ss.declareShoppingItem(rs);
  20. ArrayList list=new ArrayList();
  21. list.add(si);
  22. map.put(order, list);
  23. }else{
  24. if(order.getId()!=rs.getInt("id")){
  25. order=new Order();
  26. order.setId(rs.getInt("id"));
  27. order.setSubmittime(rs.getString("submittime"));
  28. order.setTotalprice(rs.getString("totalprice"));
  29. ShoppingItem si = ss.declareShoppingItem(rs);
  30. ArrayList list=new ArrayList();
  31. list.add(si);
  32. map.put(order, list);
  33. }else{//证明是同一个订单;
  34. ShoppingItem si = ss.declareShoppingItem(rs);
  35. ArrayList list=map.get(order);
  36. list.add(si);
  37. map.put(order, list);
  38. }
  39. }
  40. }
  41. DaoFactory.closeAll(rs, pstmt, con);
  42. return map;
  43. }


文章来源: aaaedu.blog.csdn.net,作者:tea_year,版权归原作者所有,如需转载,请联系作者。

原文链接:aaaedu.blog.csdn.net/article/details/53706475

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。