4.项目代码

4.1.领域模型(PO)

4.1.1.Business

  1. package com.neusoft.elm.po;
  2. public class Business {
  3. private Integer businessId;
  4. private String businessName;
  5. private String businessAddress;
  6. private String businessExplain;
  7. private String businessImg;
  8. private Integer orderTypeId;
  9. private double starPrice; //起送费
  10. private double deliveryPrice; //配送费
  11. private String remarks;
  12. //get、set ... ...
  13. }

4.1.2.Cart

  1. public class Cart {
  2. private Integer cartId;
  3. private Integer foodId;
  4. private Integer businessId;
  5. private String userId;
  6. private Integer quantity;
  7. //多对一:所属食品
  8. private Food food = new Food();
  9. //多对一:所属商家
  10. private Business business = new Business();
  11. //get、set ... ...
  12. }

4.1.3.DeliveryAddress

  1. public class DeliveryAddress {
  2. private Integer daId;
  3. private String contactName;
  4. private Integer contactSex;
  5. private String contactTel;
  6. private String address;
  7. private String userId;
  8. public DeliveryAddress() {}
  9. public DeliveryAddress(String contactName,Integer contactSex,String contactTel,String address,String userId) {
  10. this.contactName = contactName;
  11. this.contactSex = contactSex;
  12. this.contactTel = contactTel;
  13. this.address = address;
  14. this.userId = userId;
  15. }
  16. //get、set ... ...
  17. }

4.1.4.Food

  1. public class Food {
  2. private Integer foodId;
  3. private String foodName;
  4. private String foodExplain;
  5. private String foodImg;
  6. private Double foodPrice;
  7. private Integer businessId;
  8. private String remarks;
  9. //get、set ... ...
  10. }

4.1.5.OrderDetailet

  1. private Integer odId;
  2. private Integer orderId;
  3. private Integer foodId;
  4. private Integer quantity;
  5. //多对一:所属食品
  6. private Food food;
  7. //get、set ... ...
  8. }

4.1.6.Orders

  1. public class Orders {
  2. private Integer orderId;
  3. private String userId;
  4. private Integer businessId;
  5. private String orderDate;
  6. private Double orderTotal;
  7. private Integer daId; //送货地址编号
  8. private Integer orderState; //订单状态(0:未支付; 1:已支付)
  9. //多对一:所属商家
  10. private Business business = new Business();
  11. //一对多:某订单下的订单明细
  12. private List<OrderDetailet> odList = new ArrayList<>();
  13. //get、set ... ...
  14. }

4.1.7.User

  1. public class User {
  2. private String userId;
  3. private String password;
  4. private String userName;
  5. private Integer userSex;
  6. private String userImg;
  7. private Integer delTag;
  8. public User() {}
  9. public User(String userId,String password,String userName,Integer userSex,String userImg) {
  10. this.userId = userId;
  11. this.password = password;
  12. this.userName = userName;
  13. this.userSex = userSex;
  14. this.userImg = userImg;
  15. this.delTag = 1;
  16. }
  17. //get、set ... ...
  18. }

4.2.服务器端代码

4.2.1.Dao层代码

4.2.1.1.Business

  1. package com.neusoft.elm.dao;
  2. import java.util.List;
  3. import com.neusoft.elm.po.Business;
  4. public interface BusinessDao {
  5. public List<Business> listBusinessByOrderTypeId(Integer orderTypeId) throws Exception;
  6. public Business getBusinessById(Integer businessId) throws Exception;
  7. }
  1. package com.neusoft.elm.dao.impl;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.sql.Connection;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import com.neusoft.elm.dao.BusinessDao;
  8. import com.neusoft.elm.po.Business;
  9. import com.neusoft.elm.util.DBUtil;
  10. public class BusinessDaoImpl implements BusinessDao{
  11. private Connection con = null;
  12. private PreparedStatement pst = null;
  13. private ResultSet rs = null;
  14. @Override
  15. public List<Business> listBusinessByOrderTypeId(Integer orderTypeId) throws Exception{
  16. List<Business> list = new ArrayList<>();
  17. String sql = "select * from business where orderTypeId=? order by businessId";
  18. try {
  19. con = DBUtil.getConnection();
  20. pst = con.prepareStatement(sql);
  21. pst.setInt(1, orderTypeId);
  22. rs = pst.executeQuery();
  23. while(rs.next()) {
  24. Business business = new Business();
  25. business.setBusinessId(rs.getInt("businessId"));
  26. business.setBusinessName(rs.getString("businessName"));
  27. business.setBusinessAddress(rs.getString("businessAddress"));
  28. business.setBusinessExplain(rs.getString("businessExplain"));
  29. business.setBusinessImg(rs.getString("businessImg"));
  30. business.setOrderTypeId(rs.getInt("orderTypeId"));
  31. business.setStarPrice(rs.getDouble("starPrice"));
  32. business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
  33. business.setRemarks(rs.getString("remarks"));
  34. list.add(business);
  35. }
  36. }finally {
  37. DBUtil.close(rs, pst);
  38. }
  39. return list;
  40. }
  41. public Business getBusinessById(Integer businessId) throws Exception{
  42. Business business = null;
  43. String sql = "select * from business where businessId=?";
  44. try {
  45. con = DBUtil.getConnection();
  46. pst = con.prepareStatement(sql);
  47. pst.setInt(1, businessId);
  48. rs = pst.executeQuery();
  49. if(rs.next()) {
  50. business = new Business();
  51. business.setBusinessId(rs.getInt("businessId"));
  52. business.setBusinessName(rs.getString("businessName"));
  53. business.setBusinessAddress(rs.getString("businessAddress"));
  54. business.setBusinessExplain(rs.getString("businessExplain"));
  55. business.setBusinessImg(rs.getString("businessImg"));
  56. business.setOrderTypeId(rs.getInt("orderTypeId"));
  57. business.setStarPrice(rs.getDouble("starPrice"));
  58. business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
  59. business.setRemarks(rs.getString("remarks"));
  60. }
  61. }finally {
  62. DBUtil.close(rs, pst);
  63. }
  64. return business;
  65. }
  66. }

4.2.1.2.Cart

  1. package com.neusoft.elm.dao;
  2. import java.util.List;
  3. import com.neusoft.elm.po.Cart;
  4. public interface CartDao {
  5. public List<Cart> listCart(Cart cart) throws Exception;
  6. public int saveCart(Cart cart) throws Exception;
  7. public int removeCart(Cart cart) throws Exception;
  8. public int updateCart(Cart cart) throws Exception;
  9. }
  1. package com.neusoft.elm.dao.impl;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import com.neusoft.elm.dao.CartDao;
  8. import com.neusoft.elm.po.Business;
  9. import com.neusoft.elm.po.Cart;
  10. import com.neusoft.elm.po.Food;
  11. import com.neusoft.elm.util.DBUtil;
  12. public class CartDaoImpl implements CartDao{
  13. private Connection con = null;
  14. private PreparedStatement pst = null;
  15. private ResultSet rs = null;
  16. @Override
  17. public List<Cart> listCart(Cart cart) throws Exception{
  18. List<Cart> list = new ArrayList<>();
  19. //这里注意:cart表与food表中都有foodId、businessId字段,所以要取不同的别名
  20. StringBuffer sql = new StringBuffer();
  21. sql.append(" select c.*, ");
  22. sql.append(" f.foodId ffoodId, ");
  23. sql.append(" f.foodName ffoodName, ");
  24. sql.append(" f.foodExplain ffoodExplain, ");
  25. sql.append(" f.foodImg ffoodImg, ");
  26. sql.append(" f.foodPrice ffoodPrice, ");
  27. sql.append(" f.businessId fbusinessId, ");
  28. sql.append(" f.remarks fremarks, ");
  29. sql.append(" b.businessId bbusinessId, ");
  30. sql.append(" b.businessName bbusinessName, ");
  31. sql.append(" b.businessAddress bbusinessAddress, ");
  32. sql.append(" b.businessExplain bbusinessExplain, ");
  33. sql.append(" b.businessImg bbusinessImg, ");
  34. sql.append(" b.orderTypeId borderTypeId, ");
  35. sql.append(" b.starPrice bstarPrice, ");
  36. sql.append(" b.deliveryPrice bdeliveryPrice");
  37. sql.append(" from (cart c left join food f on c.foodId=f.foodId) ");
  38. sql.append(" left join business b on c.businessId=b.businessId ");
  39. sql.append(" where 1=1 ");
  40. if(cart.getBusinessId()!=null) {
  41. sql.append(" and c.businessId="+cart.getBusinessId());
  42. }
  43. if(cart.getUserId()!=null) {
  44. sql.append(" and c.userId='"+cart.getUserId()+"'");
  45. }
  46. //System.out.println(sql.toString());
  47. try {
  48. con = DBUtil.getConnection();
  49. pst = con.prepareStatement(sql.toString());
  50. rs = pst.executeQuery();
  51. while(rs.next()) {
  52. Cart c = new Cart();
  53. c.setCartId(rs.getInt("cartId"));
  54. c.setFoodId(rs.getInt("foodId"));
  55. c.setBusinessId(rs.getInt("businessId"));
  56. c.setUserId(rs.getString("userId"));
  57. c.setQuantity(rs.getInt("quantity"));
  58. Food f = new Food();
  59. f.setFoodId(rs.getInt("ffoodId"));
  60. f.setFoodName(rs.getString("ffoodName"));
  61. f.setFoodExplain(rs.getString("ffoodExplain"));
  62. f.setFoodImg(rs.getString("ffoodImg"));
  63. f.setFoodPrice(rs.getDouble("ffoodPrice"));
  64. f.setBusinessId(rs.getInt("fbusinessId"));
  65. f.setRemarks(rs.getString("fremarks"));
  66. c.setFood(f);
  67. Business b = new Business();
  68. b.setBusinessId(rs.getInt("bbusinessId"));
  69. b.setBusinessName(rs.getString("bbusinessName"));
  70. b.setBusinessAddress(rs.getString("bbusinessAddress"));
  71. b.setBusinessExplain(rs.getString("bbusinessExplain"));
  72. b.setBusinessImg(rs.getString("bbusinessImg"));
  73. b.setOrderTypeId(rs.getInt("borderTypeId"));
  74. b.setStarPrice(rs.getDouble("bstarPrice"));
  75. b.setDeliveryPrice(rs.getDouble("bdeliveryPrice"));
  76. c.setBusiness(b);
  77. list.add(c);
  78. }
  79. }finally {
  80. DBUtil.close(rs,pst);
  81. }
  82. return list;
  83. }
  84. @Override
  85. public int saveCart(Cart cart) throws Exception{
  86. int result = 0;
  87. String sql = "insert into cart values(null,?,?,?,1)";
  88. try {
  89. con = DBUtil.getConnection();
  90. pst = con.prepareStatement(sql);
  91. pst.setInt(1, cart.getFoodId());
  92. pst.setInt(2, cart.getBusinessId());
  93. pst.setString(3, cart.getUserId());
  94. result = pst.executeUpdate();
  95. }finally {
  96. DBUtil.close(pst);
  97. }
  98. return result;
  99. }
  100. //清空当前用户购物车中当前商家的所有食品
  101. @Override
  102. public int removeCart(Cart cart) throws Exception{
  103. int result = 0;
  104. StringBuffer sql = new StringBuffer("delete from cart where 1=1 ");
  105. if(cart.getFoodId()!=null) {
  106. sql.append(" and foodId="+cart.getFoodId());
  107. }
  108. if(cart.getBusinessId()!=null) {
  109. sql.append(" and businessId="+cart.getBusinessId());
  110. }
  111. if(cart.getUserId()!=null) {
  112. sql.append(" and userId='"+cart.getUserId()+"'");
  113. }
  114. try {
  115. con = DBUtil.getConnection();
  116. pst = con.prepareStatement(sql.toString());
  117. result = pst.executeUpdate();
  118. }finally {
  119. DBUtil.close(pst);
  120. }
  121. return result;
  122. }
  123. @Override
  124. public int updateCart(Cart cart) throws Exception{
  125. int result = 0;
  126. String sql = "update cart set quantity=? where foodId=? and businessId=? and userId=?";
  127. try {
  128. con = DBUtil.getConnection();
  129. pst = con.prepareStatement(sql);
  130. pst.setInt(1, cart.getQuantity());
  131. pst.setInt(2, cart.getFoodId());
  132. pst.setInt(3, cart.getBusinessId());
  133. pst.setString(4, cart.getUserId());
  134. result = pst.executeUpdate();
  135. }finally {
  136. DBUtil.close(pst);
  137. }
  138. return result;
  139. }
  140. }

4.2.1.3.DeliveryAddress

  1. package com.neusoft.elm.dao;
  2. import java.util.List;
  3. import com.neusoft.elm.po.DeliveryAddress;
  4. import com.neusoft.elm.po.Orders;
  5. public interface DeliveryAddressDao {
  6. public List<DeliveryAddress> listDeliveryAddressByUserId(String userId) throws Exception;
  7. public int saveDeliveryAddress(DeliveryAddress deliveryAddress) throws Exception;
  8. public int removeDeliveryAddress(Integer daId) throws Exception;
  9. public DeliveryAddress getDeliveryAddressById(Integer daId) throws Exception;
  10. public int updateDeliveryAddress(DeliveryAddress deliveryAddress) throws Exception;
  11. }
  1. package com.neusoft.elm.dao.impl;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import com.neusoft.elm.dao.DeliveryAddressDao;
  8. import com.neusoft.elm.po.DeliveryAddress;
  9. import com.neusoft.elm.util.DBUtil;
  10. public class DeliveryAddressDaoImpl implements DeliveryAddressDao{
  11. private Connection con = null;
  12. private PreparedStatement pst = null;
  13. private ResultSet rs = null;
  14. @Override
  15. public List<DeliveryAddress> listDeliveryAddressByUserId(String userId) throws Exception {
  16. List<DeliveryAddress> list = new ArrayList<>();
  17. String sql = "select * from deliveryAddress where userId=? order by daId";
  18. try {
  19. con = DBUtil.getConnection();
  20. pst = con.prepareStatement(sql);
  21. pst.setString(1, userId);
  22. rs = pst.executeQuery();
  23. while(rs.next()) {
  24. DeliveryAddress deliveryAddress = new DeliveryAddress();
  25. deliveryAddress.setDaId(rs.getInt("daId"));
  26. deliveryAddress.setContactName(rs.getString("contactName"));
  27. deliveryAddress.setContactSex(rs.getInt("contactSex"));
  28. deliveryAddress.setContactTel(rs.getString("contactTel"));
  29. deliveryAddress.setAddress(rs.getString("address"));
  30. deliveryAddress.setUserId(rs.getString("userId"));
  31. list.add(deliveryAddress);
  32. }
  33. }finally {
  34. DBUtil.close(rs, pst);
  35. }
  36. return list;
  37. }
  38. @Override
  39. public int saveDeliveryAddress(DeliveryAddress deliveryAddress) throws Exception{
  40. int result = 0;
  41. String sql = "insert into deliveryAddress values(null,?,?,?,?,?)";
  42. try {
  43. con = DBUtil.getConnection();
  44. pst = con.prepareStatement(sql);
  45. pst.setString(1, deliveryAddress.getContactName());
  46. pst.setInt(2, deliveryAddress.getContactSex());
  47. pst.setString(3, deliveryAddress.getContactTel());
  48. pst.setString(4, deliveryAddress.getAddress());
  49. pst.setString(5, deliveryAddress.getUserId());
  50. result = pst.executeUpdate();
  51. }finally {
  52. DBUtil.close(pst);
  53. }
  54. return result;
  55. }
  56. @Override
  57. public int removeDeliveryAddress(Integer daId) throws Exception{
  58. int result = 0;
  59. String sql = "delete from deliveryAddress where daId=?";
  60. try {
  61. con = DBUtil.getConnection();
  62. pst = con.prepareStatement(sql);
  63. pst.setInt(1, daId);
  64. result = pst.executeUpdate();
  65. }finally {
  66. DBUtil.close(pst);
  67. }
  68. return result;
  69. }
  70. public DeliveryAddress getDeliveryAddressById(Integer daId) throws Exception{
  71. DeliveryAddress deliveryAddress = null;
  72. String sql = "select * from deliveryAddress where daId=?";
  73. try {
  74. con = DBUtil.getConnection();
  75. pst = con.prepareStatement(sql);
  76. pst.setInt(1, daId);
  77. rs = pst.executeQuery();
  78. if(rs.next()) {
  79. deliveryAddress = new DeliveryAddress();
  80. deliveryAddress.setDaId(rs.getInt("daId"));
  81. deliveryAddress.setContactName(rs.getString("contactName"));
  82. deliveryAddress.setContactSex(rs.getInt("contactSex"));
  83. deliveryAddress.setContactTel(rs.getString("contactTel"));
  84. deliveryAddress.setAddress(rs.getString("address"));
  85. deliveryAddress.setUserId(rs.getString("userId"));
  86. }
  87. }finally {
  88. DBUtil.close(rs, pst);
  89. }
  90. return deliveryAddress;
  91. }
  92. @Override
  93. public int updateDeliveryAddress(DeliveryAddress deliveryAddress) throws Exception{
  94. int result = 0;
  95. String sql = "update deliveryAddress set contactName=?,contactSex=?,contactTel=?,address=?,userId=? where daId=?";
  96. try {
  97. con = DBUtil.getConnection();
  98. pst = con.prepareStatement(sql);
  99. pst.setString(1, deliveryAddress.getContactName());
  100. pst.setInt(2, deliveryAddress.getContactSex());
  101. pst.setString(3, deliveryAddress.getContactTel());
  102. pst.setString(4, deliveryAddress.getAddress());
  103. pst.setString(5, deliveryAddress.getUserId());
  104. pst.setInt(6, deliveryAddress.getDaId());
  105. result = pst.executeUpdate();
  106. }finally {
  107. DBUtil.close(pst);
  108. }
  109. return result;
  110. }
  111. }

4.2.1.4.Food

  1. package com.neusoft.elm.dao;
  2. import java.util.List;
  3. import com.neusoft.elm.po.Food;
  4. public interface FoodDao {
  5. public List<Food> listFoodByBusinessId(Integer businessId) throws Exception;
  6. }
  1. package com.neusoft.elm.dao.impl;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import com.neusoft.elm.dao.FoodDao;
  8. import com.neusoft.elm.po.Food;
  9. import com.neusoft.elm.util.DBUtil;
  10. public class FoodDaoImpl implements FoodDao{
  11. private Connection con = null;
  12. private PreparedStatement pst = null;
  13. private ResultSet rs = null;
  14. @Override
  15. public List<Food> listFoodByBusinessId(Integer businessId) throws Exception{
  16. List<Food> list = new ArrayList<>();
  17. String sql = "select * from food where businessId=? order by foodId";
  18. try {
  19. con = DBUtil.getConnection();
  20. pst = con.prepareStatement(sql);
  21. pst.setInt(1, businessId);
  22. rs = pst.executeQuery();
  23. while(rs.next()) {
  24. Food food = new Food();
  25. food.setFoodId(rs.getInt("foodId"));
  26. food.setFoodName(rs.getString("foodName"));
  27. food.setFoodExplain(rs.getString("foodExplain"));
  28. food.setFoodImg(rs.getString("foodImg"));
  29. food.setFoodPrice(rs.getDouble("foodPrice"));
  30. food.setBusinessId(rs.getInt("businessId"));
  31. food.setRemarks(rs.getString("remarks"));
  32. list.add(food);
  33. }
  34. }finally {
  35. DBUtil.close(rs, pst);
  36. }
  37. return list;
  38. }
  39. }

4.2.1.5.OrderDetailet

  1. package com.neusoft.elm.dao;
  2. import java.util.List;
  3. import com.neusoft.elm.po.OrderDetailet;
  4. public interface OrderDetailetDao {
  5. public int saveOrderDetailetBatch(List<OrderDetailet> list) throws Exception;
  6. public List<OrderDetailet> listOrderDetailetByOrderId(Integer orderId) throws Exception;
  7. }
  1. package com.neusoft.elm.dao.impl;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import com.neusoft.elm.dao.OrderDetailetDao;
  8. import com.neusoft.elm.po.Food;
  9. import com.neusoft.elm.po.OrderDetailet;
  10. import com.neusoft.elm.util.DBUtil;
  11. public class OrderDetailetDaoImpl implements OrderDetailetDao{
  12. private Connection con = null;
  13. private PreparedStatement pst = null;
  14. private ResultSet rs = null;
  15. @Override
  16. public int saveOrderDetailetBatch(List<OrderDetailet> list) throws Exception {
  17. int result = 0;
  18. StringBuffer stringBuffer = new StringBuffer("insert into orderDetailet(orderId,foodId,quantity) values");
  19. for(OrderDetailet od : list) {
  20. stringBuffer.append("("+od.getOrderId()+","+od.getFoodId()+","+od.getQuantity()+"),");
  21. }
  22. //去掉sql语句中的最后一个逗号
  23. String sql = stringBuffer.toString().substring(0,stringBuffer.toString().length()-1);
  24. //System.out.println(sql);
  25. try {
  26. con = DBUtil.getConnection();
  27. pst = con.prepareStatement(sql);
  28. result = pst.executeUpdate();
  29. }finally {
  30. DBUtil.close(pst);
  31. }
  32. return result;
  33. }
  34. @Override
  35. public List<OrderDetailet> listOrderDetailetByOrderId(Integer orderId) throws Exception{
  36. List<OrderDetailet> list = new ArrayList<>();
  37. StringBuffer sql = new StringBuffer();
  38. sql.append(" select o.*, ");
  39. sql.append(" f.foodId ffoodId, ");
  40. sql.append(" f.foodName ffoodName, ");
  41. sql.append(" f.foodPrice ffoodPrice ");
  42. sql.append(" from orderDetailet o left join food f on o.foodId=f.foodId ");
  43. sql.append(" where o.orderId=? ");
  44. try {
  45. con = DBUtil.getConnection();
  46. pst = con.prepareStatement(sql.toString());
  47. pst.setInt(1, orderId);
  48. rs = pst.executeQuery();
  49. while(rs.next()) {
  50. OrderDetailet od = new OrderDetailet();
  51. od.setOdId(rs.getInt("odId"));
  52. od.setOrderId(rs.getInt("orderId"));
  53. od.setFoodId(rs.getInt("foodId"));
  54. od.setQuantity(rs.getInt("quantity"));
  55. Food food = new Food();
  56. food.setFoodId(rs.getInt("ffoodId"));
  57. food.setFoodName(rs.getString("ffoodName"));
  58. food.setFoodPrice(rs.getDouble("ffoodPrice"));
  59. od.setFood(food);
  60. list.add(od);
  61. }
  62. }finally {
  63. DBUtil.close(rs,pst);
  64. }
  65. return list;
  66. }
  67. }

4.2.1.6.Orders

  1. package com.neusoft.elm.dao;
  2. import java.util.List;
  3. import com.neusoft.elm.po.Orders;
  4. public interface OrdersDao {
  5. //注意:此方法会返回生成的主键
  6. public int saveOrders(Orders orders) throws Exception;
  7. public Orders getOrdersById(Integer orderId) throws Exception;
  8. public List<Orders> listOrdersById(String userId) throws Exception;
  9. }
  1. package com.neusoft.elm.dao.impl;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import com.neusoft.elm.dao.OrdersDao;
  8. import com.neusoft.elm.po.Business;
  9. import com.neusoft.elm.po.Orders;
  10. import com.neusoft.elm.util.CommonUtil;
  11. import com.neusoft.elm.util.DBUtil;
  12. public class OrdersDaoImpl implements OrdersDao{
  13. private Connection con = null;
  14. private PreparedStatement pst = null;
  15. private ResultSet rs = null;
  16. /**
  17. * 注意:此方法会返回生成的主键
  18. */
  19. @Override
  20. public int saveOrders(Orders orders) throws Exception{
  21. int result = 0;
  22. String sql = "insert into orders values(null,?,?,?,?,?,0)";
  23. try {
  24. con = DBUtil.getConnection();
  25. //设置返回自增长列值
  26. pst = con.prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS);
  27. pst.setString(1, orders.getUserId());
  28. pst.setInt(2, orders.getBusinessId());
  29. pst.setString(3, CommonUtil.getCurrentDate());
  30. pst.setDouble(4, orders.getOrderTotal());
  31. pst.setInt(5, orders.getDaId());
  32. pst.executeUpdate();
  33. //获取自增长列值
  34. rs = pst.getGeneratedKeys();
  35. if(rs.next()){
  36. result = rs.getInt(1);
  37. }
  38. } finally {
  39. DBUtil.close(pst);
  40. }
  41. return result;
  42. }
  43. @Override
  44. public Orders getOrdersById(Integer orderId) throws Exception {
  45. Orders orders = null;
  46. StringBuffer sql = new StringBuffer();
  47. sql.append(" select o.*, ");
  48. sql.append(" b.businessId bbusinessId, ");
  49. sql.append(" b.businessName bbusinessName, ");
  50. sql.append(" b.deliveryPrice bdeliveryPrice ");
  51. sql.append(" from orders o left join business b on o.businessId=b.businessId ");
  52. sql.append(" where o.orderId=? ");
  53. try {
  54. con = DBUtil.getConnection();
  55. //设置返回自增长列值
  56. pst = con.prepareStatement(sql.toString());
  57. pst.setInt(1, orderId);
  58. rs = pst.executeQuery();
  59. if(rs.next()){
  60. orders = new Orders();
  61. orders.setOrderId(rs.getInt("orderId"));
  62. orders.setUserId(rs.getString("userId"));
  63. orders.setBusinessId(rs.getInt("businessId"));
  64. orders.setOrderDate(rs.getString("orderDate"));
  65. orders.setOrderTotal(rs.getDouble("orderTotal"));
  66. orders.setOrderState(rs.getInt("orderState"));
  67. Business business = new Business();
  68. business.setBusinessId(rs.getInt("bbusinessId"));
  69. business.setBusinessName(rs.getString("bbusinessName"));
  70. business.setDeliveryPrice(rs.getDouble("bdeliveryPrice"));
  71. orders.setBusiness(business);
  72. }
  73. } finally {
  74. DBUtil.close(rs,pst);
  75. }
  76. return orders;
  77. }
  78. @Override
  79. public List<Orders> listOrdersById(String userId) throws Exception{
  80. List<Orders> list = new ArrayList<>();
  81. StringBuffer sql = new StringBuffer();
  82. sql.append(" select o.*, ");
  83. sql.append(" b.businessId bbusinessId, ");
  84. sql.append(" b.businessName bbusinessName, ");
  85. sql.append(" b.deliveryPrice bdeliveryPrice ");
  86. sql.append(" from orders o left join business b on o.businessId=b.businessId ");
  87. sql.append(" where o.userId=?");
  88. try {
  89. con = DBUtil.getConnection();
  90. //设置返回自增长列值
  91. pst = con.prepareStatement(sql.toString());
  92. pst.setString(1, userId);
  93. rs = pst.executeQuery();
  94. while(rs.next()){
  95. Orders o = new Orders();
  96. o.setOrderId(rs.getInt("orderId"));
  97. o.setUserId(rs.getString("userId"));
  98. o.setBusinessId(rs.getInt("businessId"));
  99. o.setOrderDate(rs.getString("orderDate"));
  100. o.setOrderTotal(rs.getDouble("orderTotal"));
  101. o.setDaId(rs.getInt("daId"));
  102. o.setOrderState(rs.getInt("orderState"));
  103. Business business = new Business();
  104. business.setBusinessId(rs.getInt("bbusinessId"));
  105. business.setBusinessName(rs.getString("bbusinessName"));
  106. business.setDeliveryPrice(rs.getDouble("bdeliveryPrice"));
  107. o.setBusiness(business);
  108. list.add(o);
  109. }
  110. } finally {
  111. DBUtil.close(rs,pst);
  112. }
  113. return list;
  114. }
  115. }

4.2.1.7.User

  1. package com.neusoft.elm.dao;
  2. import com.neusoft.elm.po.User;
  3. public interface UserDao {
  4. public int saveUser(User user) throws Exception;
  5. public User getUserByIdByPass(String userId,String password) throws Exception;
  6. public User getUserById(String userId) throws Exception;
  7. }
  1. package com.neusoft.elm.dao.impl;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import com.neusoft.elm.dao.UserDao;
  6. import com.neusoft.elm.po.Business;
  7. import com.neusoft.elm.po.User;
  8. import com.neusoft.elm.util.DBUtil;
  9. public class UserDaoImpl implements UserDao{
  10. private Connection con = null;
  11. private PreparedStatement pst = null;
  12. private ResultSet rs = null;
  13. /**
  14. * dao层不再处理异常,因为异常要返回给service,用于事务管理
  15. */
  16. @Override
  17. public int saveUser(User user) throws Exception{
  18. int result = 0;
  19. String sql = "insert into user values(?,?,?,?,?,1)";
  20. try {
  21. con = DBUtil.getConnection();
  22. pst = con.prepareStatement(sql);
  23. pst.setString(1, user.getUserId());
  24. pst.setString(2, user.getPassword());
  25. pst.setString(3, user.getUserName());
  26. pst.setInt(4, user.getUserSex());
  27. pst.setString(5, user.getUserImg());
  28. result = pst.executeUpdate();
  29. } finally {
  30. DBUtil.close(pst);
  31. }
  32. return result;
  33. }
  34. public User getUserByIdByPass(String userId,String password) throws Exception{
  35. User userResult = null;
  36. String sql = "select * from user where userId=? and password=?";
  37. try {
  38. con = DBUtil.getConnection();
  39. pst = con.prepareStatement(sql);
  40. pst.setString(1, userId);
  41. pst.setString(2, password);
  42. rs = pst.executeQuery();
  43. if(rs.next()) {
  44. userResult = new User();
  45. userResult.setUserId(rs.getString("userId"));
  46. userResult.setPassword(rs.getString("password"));
  47. userResult.setUserName(rs.getString("userName"));
  48. userResult.setUserSex(rs.getInt("userSex"));
  49. userResult.setUserImg(rs.getString("userImg"));
  50. userResult.setDelTag(rs.getInt("delTag"));
  51. }
  52. }finally {
  53. DBUtil.close(rs, pst);
  54. }
  55. return userResult;
  56. }
  57. public User getUserById(String userId) throws Exception{
  58. User userResult = null;
  59. String sql = "select * from user where userId=?";
  60. try {
  61. con = DBUtil.getConnection();
  62. pst = con.prepareStatement(sql);
  63. pst.setString(1, userId);
  64. rs = pst.executeQuery();
  65. if(rs.next()) {
  66. userResult = new User();
  67. userResult.setUserId(rs.getString("userId"));
  68. userResult.setPassword(rs.getString("password"));
  69. userResult.setUserName(rs.getString("userName"));
  70. userResult.setUserSex(rs.getInt("userSex"));
  71. userResult.setUserImg(rs.getString("userImg"));
  72. userResult.setDelTag(rs.getInt("delTag"));
  73. }
  74. }finally {
  75. DBUtil.close(rs, pst);
  76. }
  77. return userResult;
  78. }
  79. }

4.2.2.Service层代码

4.2.2.1.Business

  1. package com.neusoft.elm.service;
  2. import java.util.List;
  3. import com.neusoft.elm.po.Business;
  4. public interface BusinessService {
  5. public List<Business> listBusinessByOrderTypeId(Integer orderTypeId);
  6. public Business getBusinessById(Integer businessId);
  7. }
  1. package com.neusoft.elm.service.impl;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import com.neusoft.elm.dao.BusinessDao;
  5. import com.neusoft.elm.dao.impl.BusinessDaoImpl;
  6. import com.neusoft.elm.po.Business;
  7. import com.neusoft.elm.service.BusinessService;
  8. import com.neusoft.elm.util.DBUtil;
  9. public class BusinessServiceImpl implements BusinessService{
  10. @Override
  11. public List<Business> listBusinessByOrderTypeId(Integer orderTypeId) {
  12. List<Business> list = new ArrayList<>();
  13. BusinessDao dao = new BusinessDaoImpl();
  14. try {
  15. DBUtil.getConnection();
  16. list = dao.listBusinessByOrderTypeId(orderTypeId);
  17. } catch(Exception e) {
  18. e.printStackTrace();
  19. } finally {
  20. DBUtil.close();
  21. }
  22. return list;
  23. }
  24. @Override
  25. public Business getBusinessById(Integer businessId) {
  26. Business business = null;
  27. BusinessDao dao = new BusinessDaoImpl();
  28. try {
  29. DBUtil.getConnection();
  30. business = dao.getBusinessById(businessId);
  31. } catch(Exception e) {
  32. e.printStackTrace();
  33. } finally {
  34. DBUtil.close();
  35. }
  36. return business;
  37. }
  38. }

4.2.2.2.Cart

  1. package com.neusoft.elm.service;
  2. import java.util.List;
  3. import com.neusoft.elm.po.Cart;
  4. public interface CartService {
  5. public List<Cart> listCart(Cart cart);
  6. public int saveCart(Cart cart);
  7. public int removeCart(Cart cart);
  8. public int updateCart(Cart cart);
  9. }
  1. package com.neusoft.elm.service.impl;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import com.neusoft.elm.dao.CartDao;
  5. import com.neusoft.elm.dao.impl.CartDaoImpl;
  6. import com.neusoft.elm.po.Cart;
  7. import com.neusoft.elm.service.CartService;
  8. import com.neusoft.elm.util.DBUtil;
  9. public class CartServiceImpl implements CartService {
  10. @Override
  11. public List<Cart> listCart(Cart cart) {
  12. List<Cart> list = new ArrayList<>();
  13. CartDao dao = new CartDaoImpl();
  14. try {
  15. DBUtil.getConnection();
  16. list = dao.listCart(cart);
  17. } catch (Exception e) {
  18. e.printStackTrace();
  19. } finally {
  20. DBUtil.close();
  21. }
  22. return list;
  23. }
  24. @Override
  25. public int saveCart(Cart cart) {
  26. int result = 0;
  27. CartDao dao = new CartDaoImpl();
  28. try {
  29. DBUtil.getConnection();
  30. result = dao.saveCart(cart);
  31. } catch (Exception e) {
  32. e.printStackTrace();
  33. } finally {
  34. DBUtil.close();
  35. }
  36. return result;
  37. }
  38. @Override
  39. public int removeCart(Cart cart) {
  40. int result = 0;
  41. CartDao dao = new CartDaoImpl();
  42. try {
  43. DBUtil.getConnection();
  44. result = dao.removeCart(cart);
  45. } catch (Exception e) {
  46. e.printStackTrace();
  47. } finally {
  48. DBUtil.close();
  49. }
  50. return result;
  51. }
  52. @Override
  53. public int updateCart(Cart cart) {
  54. int result = 0;
  55. CartDao dao = new CartDaoImpl();
  56. try {
  57. DBUtil.getConnection();
  58. result = dao.updateCart(cart);
  59. } catch (Exception e) {
  60. e.printStackTrace();
  61. } finally {
  62. DBUtil.close();
  63. }
  64. return result;
  65. }
  66. }

4.2.2.3.DeliveryAddress

  1. package com.neusoft.elm.service;
  2. import java.util.List;
  3. import com.neusoft.elm.po.DeliveryAddress;
  4. public interface DeliveryAddressService {
  5. public List<DeliveryAddress> listDeliveryAddressByUserId(String userId);
  6. public int saveDeliveryAddress(DeliveryAddress deliveryAddress);
  7. public int removeDeliveryAddress(Integer daId);
  8. public DeliveryAddress getDeliveryAddressById(Integer daId);
  9. public int updateDeliveryAddress(DeliveryAddress deliveryAddress);
  10. }
  1. package com.neusoft.elm.service.impl;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import com.neusoft.elm.dao.DeliveryAddressDao;
  5. import com.neusoft.elm.dao.impl.DeliveryAddressDaoImpl;
  6. import com.neusoft.elm.po.DeliveryAddress;
  7. import com.neusoft.elm.service.DeliveryAddressService;
  8. import com.neusoft.elm.util.DBUtil;
  9. public class DeliveryAddressServiceImpl implements DeliveryAddressService{
  10. @Override
  11. public List<DeliveryAddress> listDeliveryAddressByUserId(String userId) {
  12. List<DeliveryAddress> list = new ArrayList<>();
  13. DeliveryAddressDao dao = new DeliveryAddressDaoImpl();
  14. try {
  15. DBUtil.getConnection();
  16. list = dao.listDeliveryAddressByUserId(userId);
  17. } catch(Exception e) {
  18. e.printStackTrace();
  19. } finally {
  20. DBUtil.close();
  21. }
  22. return list;
  23. }
  24. @Override
  25. public int saveDeliveryAddress(DeliveryAddress deliveryAddress) {
  26. int result = 0;
  27. DeliveryAddressDao dao = new DeliveryAddressDaoImpl();
  28. try {
  29. DBUtil.getConnection();
  30. result = dao.saveDeliveryAddress(deliveryAddress);
  31. } catch(Exception e) {
  32. e.printStackTrace();
  33. } finally {
  34. DBUtil.close();
  35. }
  36. return result;
  37. }
  38. @Override
  39. public int removeDeliveryAddress(Integer daId) {
  40. int result = 0;
  41. DeliveryAddressDao dao = new DeliveryAddressDaoImpl();
  42. try {
  43. DBUtil.getConnection();
  44. result = dao.removeDeliveryAddress(daId);
  45. } catch(Exception e) {
  46. e.printStackTrace();
  47. } finally {
  48. DBUtil.close();
  49. }
  50. return result;
  51. }
  52. @Override
  53. public DeliveryAddress getDeliveryAddressById(Integer daId) {
  54. DeliveryAddress deliveryAddress = null;
  55. DeliveryAddressDao dao = new DeliveryAddressDaoImpl();
  56. try {
  57. DBUtil.getConnection();
  58. deliveryAddress = dao.getDeliveryAddressById(daId);
  59. } catch(Exception e) {
  60. e.printStackTrace();
  61. } finally {
  62. DBUtil.close();
  63. }
  64. return deliveryAddress;
  65. }
  66. @Override
  67. public int updateDeliveryAddress(DeliveryAddress deliveryAddress) {
  68. int result = 0;
  69. DeliveryAddressDao dao = new DeliveryAddressDaoImpl();
  70. try {
  71. DBUtil.getConnection();
  72. result = dao.updateDeliveryAddress(deliveryAddress);
  73. } catch(Exception e) {
  74. e.printStackTrace();
  75. } finally {
  76. DBUtil.close();
  77. }
  78. return result;
  79. }
  80. }

4.2.2.4.Food

  1. package com.neusoft.elm.service;
  2. import java.util.List;
  3. import com.neusoft.elm.po.Food;
  4. public interface FoodService {
  5. public List<Food> listFoodByBusinessId(Integer businessId);
  6. }
  1. package com.neusoft.elm.service.impl;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import com.neusoft.elm.dao.BusinessDao;
  5. import com.neusoft.elm.dao.FoodDao;
  6. import com.neusoft.elm.dao.impl.BusinessDaoImpl;
  7. import com.neusoft.elm.dao.impl.FoodDaoImpl;
  8. import com.neusoft.elm.po.Business;
  9. import com.neusoft.elm.po.Food;
  10. import com.neusoft.elm.service.FoodService;
  11. import com.neusoft.elm.util.DBUtil;
  12. public class FoodServiceImpl implements FoodService{
  13. @Override
  14. public List<Food> listFoodByBusinessId(Integer businessId) {
  15. List<Food> list = new ArrayList<>();
  16. FoodDao dao = new FoodDaoImpl();
  17. try {
  18. DBUtil.getConnection();
  19. list = dao.listFoodByBusinessId(businessId);
  20. } catch(Exception e) {
  21. e.printStackTrace();
  22. } finally {
  23. DBUtil.close();
  24. }
  25. return list;
  26. }
  27. }

4.2.2.5.OrderDetailet

  1. package com.neusoft.elm.service;
  2. import java.util.List;
  3. import com.neusoft.elm.po.OrderDetailet;
  4. public interface OrderDetailetService {
  5. public List<OrderDetailet> listOrderDetailetByOrderId(Integer orderId);
  6. }
  1. package com.neusoft.elm.service.impl;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import com.neusoft.elm.dao.OrderDetailetDao;
  5. import com.neusoft.elm.dao.impl.OrderDetailetDaoImpl;
  6. import com.neusoft.elm.po.OrderDetailet;
  7. import com.neusoft.elm.service.OrderDetailetService;
  8. import com.neusoft.elm.util.DBUtil;
  9. public class OrderDetailetServiceImpl implements OrderDetailetService{
  10. @Override
  11. public List<OrderDetailet> listOrderDetailetByOrderId(Integer orderId) {
  12. List<OrderDetailet> list = new ArrayList<>();
  13. OrderDetailetDao dao = new OrderDetailetDaoImpl();
  14. try {
  15. DBUtil.getConnection();
  16. list = dao.listOrderDetailetByOrderId(orderId);
  17. } catch(Exception e) {
  18. e.printStackTrace();
  19. } finally {
  20. DBUtil.close();
  21. }
  22. return list;
  23. }
  24. }

4.2.2.6.Orders

  1. package com.neusoft.elm.service;
  2. import java.util.List;
  3. import com.neusoft.elm.po.Orders;
  4. public interface OrdersService {
  5. //下订单(先清空当前用户购物车中当前商家的所有食品,然后生成订单和订单明细,最后返回订单号)
  6. public int createOrders(String userId,Integer businessId,Integer daId);
  7. public Orders getOrdersById(Integer orderId);
  8. public List<Orders> listOrdersByUserId(String userId);
  9. }
  1. package com.neusoft.elm.service.impl;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import com.neusoft.elm.dao.BusinessDao;
  5. import com.neusoft.elm.dao.CartDao;
  6. import com.neusoft.elm.dao.OrdersDao;
  7. import com.neusoft.elm.dao.OrderDetailetDao;
  8. import com.neusoft.elm.dao.impl.BusinessDaoImpl;
  9. import com.neusoft.elm.dao.impl.CartDaoImpl;
  10. import com.neusoft.elm.dao.impl.OrdersDaoImpl;
  11. import com.neusoft.elm.dao.impl.OrderDetailetDaoImpl;
  12. import com.neusoft.elm.po.Business;
  13. import com.neusoft.elm.po.Cart;
  14. import com.neusoft.elm.po.Orders;
  15. import com.neusoft.elm.po.OrderDetailet;
  16. import com.neusoft.elm.service.OrdersService;
  17. import com.neusoft.elm.util.DBUtil;
  18. public class OrdersServiceImpl implements OrdersService {
  19. //下订单(先清空当前用户购物车中当前商家的所有食品,然后生成订单和订单明细,最后返回订单号)
  20. @Override
  21. public int createOrders(String userId,Integer businessId,Integer daId) {
  22. int orderId = 0;
  23. BusinessDao businessDao = new BusinessDaoImpl();
  24. CartDao cartDao = new CartDaoImpl();
  25. OrdersDao ordersDao = new OrdersDaoImpl();
  26. OrderDetailetDao orderDetailetDao = new OrderDetailetDaoImpl();
  27. try {
  28. DBUtil.beginTransaction();
  29. //1、查询当前用户购物车中当前商家的所有食品
  30. Cart cart = new Cart();
  31. cart.setBusinessId(businessId);
  32. cart.setUserId(userId);
  33. List<Cart> cartList = cartDao.listCart(cart);
  34. //2、查询商家信息(需要使用商家的配送费信息)
  35. Business business = businessDao.getBusinessById(businessId);
  36. //3、获取订单总额
  37. Double ordersTotal = 0.0; //订单总额
  38. for(Cart c : cartList) {
  39. //累计所有食品总价格
  40. ordersTotal += c.getFood().getFoodPrice()*c.getQuantity();
  41. }
  42. //加上配送费
  43. ordersTotal += business.getDeliveryPrice();
  44. //3、创建订单,并获取订单编号
  45. Orders orders = new Orders();
  46. orders.setUserId(userId);
  47. orders.setBusinessId(businessId);
  48. orders.setOrderTotal(ordersTotal);
  49. orders.setDaId(daId);
  50. orderId = ordersDao.saveOrders(orders);
  51. //4、处理相关数据:获取订单明细集合
  52. List<OrderDetailet> orderDetailetList = new ArrayList<>(); //订单明细集合
  53. for(Cart c : cartList) {
  54. OrderDetailet od = new OrderDetailet();
  55. od.setOrderId(orderId);
  56. od.setFoodId(c.getFoodId());
  57. od.setQuantity(c.getQuantity());
  58. orderDetailetList.add(od);
  59. }
  60. //5、批量添加订单明细
  61. orderDetailetDao.saveOrderDetailetBatch(orderDetailetList);
  62. //6、清空当前用户购物车中当前商家的所有食品
  63. cartDao.removeCart(cart);
  64. DBUtil.commitTransaction();
  65. } catch (Exception e) {
  66. orderId = 0;
  67. try {
  68. DBUtil.rollbackTransaction();
  69. } catch (Exception e1) {
  70. e1.printStackTrace();
  71. }
  72. e.printStackTrace();
  73. } finally {
  74. DBUtil.close(); // 关闭Connection
  75. }
  76. return orderId;
  77. }
  78. @Override
  79. public Orders getOrdersById(Integer orderId) {
  80. Orders orders = null;
  81. OrdersDao dao = new OrdersDaoImpl();
  82. try {
  83. DBUtil.getConnection();
  84. orders = dao.getOrdersById(orderId);
  85. } catch(Exception e) {
  86. e.printStackTrace();
  87. } finally {
  88. DBUtil.close();
  89. }
  90. return orders;
  91. }
  92. @Override
  93. public List<Orders> listOrdersByUserId(String userId){
  94. List<Orders> list = new ArrayList<>();
  95. OrdersDao ordersDao = new OrdersDaoImpl();
  96. OrderDetailetDao orderDetailetDao = new OrderDetailetDaoImpl();
  97. try {
  98. DBUtil.beginTransaction();
  99. //先查询当前用户所有订单
  100. list = ordersDao.listOrdersById(userId);
  101. for(Orders o : list) {
  102. //在查询每个订单的明细
  103. List<OrderDetailet> odList = orderDetailetDao.listOrderDetailetByOrderId(o.getOrderId());
  104. o.setOdList(odList);
  105. }
  106. DBUtil.commitTransaction();
  107. } catch (Exception e) {
  108. try {
  109. DBUtil.rollbackTransaction();
  110. } catch (Exception e1) {
  111. e1.printStackTrace();
  112. }
  113. e.printStackTrace();
  114. } finally {
  115. DBUtil.close(); // 关闭Connection
  116. }
  117. return list;
  118. }
  119. }

4.2.2.7.user

  1. package com.neusoft.elm.service;
  2. import com.neusoft.elm.po.User;
  3. public interface UserService {
  4. public int saveUser(User user);
  5. public User getUserByIdByPass(String userId,String password);
  6. public User getUserById(String userId);
  7. }
  1. package com.neusoft.elm.service.impl;
  2. import java.util.List;
  3. import com.neusoft.elm.dao.UserDao;
  4. import com.neusoft.elm.dao.impl.UserDaoImpl;
  5. import com.neusoft.elm.po.User;
  6. import com.neusoft.elm.service.UserService;
  7. import com.neusoft.elm.util.DBUtil;
  8. public class UserServiceImpl implements UserService{
  9. @Override
  10. public int saveUser(User user) {
  11. int result = 1;
  12. UserDao dao = new UserDaoImpl();
  13. try {
  14. DBUtil.getConnection();
  15. dao.saveUser(user);
  16. } catch (Exception e) {
  17. e.printStackTrace();
  18. } finally {
  19. DBUtil.close();
  20. }
  21. return result;
  22. }
  23. public User getUserByIdByPass(String userId,String password){
  24. User userResult = null;
  25. UserDao dao = new UserDaoImpl();
  26. try {
  27. DBUtil.getConnection();
  28. userResult = dao.getUserByIdByPass(userId,password);
  29. } catch(Exception e) {
  30. e.printStackTrace();
  31. } finally {
  32. DBUtil.close();
  33. }
  34. return userResult;
  35. }
  36. public User getUserById(String userId) {
  37. User userResult = null;
  38. UserDao dao = new UserDaoImpl();
  39. try {
  40. DBUtil.getConnection();
  41. userResult = dao.getUserById(userId);
  42. } catch(Exception e) {
  43. e.printStackTrace();
  44. } finally {
  45. DBUtil.close();
  46. }
  47. return userResult;
  48. }
  49. }

4.2.3.Controller层代码

4.2.3.1.Business

  1. package com.neusoft.elm.controller;
  2. import java.util.List;
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletResponse;
  5. import com.neusoft.elm.po.Business;
  6. import com.neusoft.elm.service.BusinessService;
  7. import com.neusoft.elm.service.impl.BusinessServiceImpl;
  8. public class BusinessController{
  9. public Object listBusinessByOrderTypeId(HttpServletRequest request,HttpServletResponse response) throws Exception{
  10. Integer orderTypeId = Integer.valueOf(request.getParameter("orderTypeId"));
  11. BusinessService service = new BusinessServiceImpl();
  12. List<Business> list = service.listBusinessByOrderTypeId(orderTypeId);
  13. return list;
  14. }
  15. public Object getBusinessById(HttpServletRequest request,HttpServletResponse response) throws Exception{
  16. Integer businessId = Integer.valueOf(request.getParameter("businessId"));
  17. BusinessService service = new BusinessServiceImpl();
  18. Business business = service.getBusinessById(businessId);
  19. return business;
  20. }
  21. }

4.2.3.2.Cart

  1. package com.neusoft.elm.controller;
  2. import java.util.List;
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletResponse;
  5. import com.neusoft.elm.po.Cart;
  6. import com.neusoft.elm.service.CartService;
  7. import com.neusoft.elm.service.impl.CartServiceImpl;
  8. public class CartController{
  9. public Object listCart(HttpServletRequest request,HttpServletResponse response) throws Exception{
  10. Cart cart = new Cart();
  11. if(request.getParameter("businessId")!=null) {
  12. cart.setBusinessId(Integer.parseInt(request.getParameter("businessId")));
  13. }
  14. if(request.getParameter("userId")!=null) {
  15. cart.setUserId(request.getParameter("userId"));
  16. }
  17. CartService service = new CartServiceImpl();
  18. List<Cart> cartResult = service.listCart(cart);
  19. return cartResult;
  20. }
  21. public Object saveCart(HttpServletRequest request,HttpServletResponse response) throws Exception{
  22. Cart cart = new Cart();
  23. cart.setFoodId(Integer.valueOf(request.getParameter("foodId")));
  24. cart.setBusinessId(Integer.valueOf(request.getParameter("businessId")));
  25. cart.setUserId(request.getParameter("userId"));
  26. CartService service = new CartServiceImpl();
  27. int result = service.saveCart(cart);
  28. return result;
  29. }
  30. public Object removeCart(HttpServletRequest request,HttpServletResponse response) throws Exception{
  31. Cart cart = new Cart();
  32. cart.setFoodId(Integer.valueOf(request.getParameter("foodId")));
  33. cart.setBusinessId(Integer.valueOf(request.getParameter("businessId")));
  34. cart.setUserId(request.getParameter("userId"));
  35. CartService service = new CartServiceImpl();
  36. int result = service.removeCart(cart);
  37. return result;
  38. }
  39. public Object updateCart(HttpServletRequest request,HttpServletResponse response) throws Exception{
  40. Cart cart = new Cart();
  41. cart.setFoodId(Integer.valueOf(request.getParameter("foodId")));
  42. cart.setBusinessId(Integer.valueOf(request.getParameter("businessId")));
  43. cart.setUserId(request.getParameter("userId"));
  44. cart.setQuantity(Integer.valueOf(request.getParameter("quantity")));
  45. CartService service = new CartServiceImpl();
  46. int result = service.updateCart(cart);
  47. return result;
  48. }
  49. }

4.2.3.3.DeliveryAddress

  1. package com.neusoft.elm.controller;
  2. import java.util.List;
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletResponse;
  5. import com.neusoft.elm.po.DeliveryAddress;
  6. import com.neusoft.elm.service.DeliveryAddressService;
  7. import com.neusoft.elm.service.impl.DeliveryAddressServiceImpl;
  8. public class DeliveryAddressController {
  9. public Object listDeliveryAddressByUserId(HttpServletRequest request,HttpServletResponse response) throws Exception{
  10. String userId = request.getParameter("userId");
  11. DeliveryAddressService service = new DeliveryAddressServiceImpl();
  12. List<DeliveryAddress> list = service.listDeliveryAddressByUserId(userId);
  13. return list;
  14. }
  15. public Object saveDeliveryAddress(HttpServletRequest request,HttpServletResponse response) throws Exception{
  16. DeliveryAddress deliveryAddress = new DeliveryAddress();
  17. deliveryAddress.setContactName(request.getParameter("contactName"));
  18. deliveryAddress.setContactSex(Integer.valueOf(request.getParameter("contactSex")));
  19. deliveryAddress.setContactTel(request.getParameter("contactTel"));
  20. deliveryAddress.setAddress(request.getParameter("address"));
  21. deliveryAddress.setUserId(request.getParameter("userId"));
  22. DeliveryAddressService service = new DeliveryAddressServiceImpl();
  23. int result = service.saveDeliveryAddress(deliveryAddress);
  24. return result;
  25. }
  26. public Object removeDeliveryAddress(HttpServletRequest request,HttpServletResponse response) throws Exception{
  27. Integer daId = Integer.valueOf(request.getParameter("daId"));
  28. DeliveryAddressService service = new DeliveryAddressServiceImpl();
  29. int result = service.removeDeliveryAddress(daId);
  30. return result;
  31. }
  32. public Object getDeliveryAddressById(HttpServletRequest request,HttpServletResponse response) throws Exception{
  33. Integer daId = Integer.valueOf(request.getParameter("daId"));
  34. DeliveryAddressService service = new DeliveryAddressServiceImpl();
  35. DeliveryAddress deliveryAddress = service.getDeliveryAddressById(daId);
  36. return deliveryAddress;
  37. }
  38. public Object updateDeliveryAddress(HttpServletRequest request,HttpServletResponse response) throws Exception{
  39. DeliveryAddress deliveryAddress = new DeliveryAddress();
  40. deliveryAddress.setDaId(Integer.valueOf(request.getParameter("daId")));
  41. deliveryAddress.setContactName(request.getParameter("contactName"));
  42. deliveryAddress.setContactSex(Integer.valueOf(request.getParameter("contactSex")));
  43. deliveryAddress.setContactTel(request.getParameter("contactTel"));
  44. deliveryAddress.setAddress(request.getParameter("address"));
  45. deliveryAddress.setUserId(request.getParameter("userId"));
  46. DeliveryAddressService service = new DeliveryAddressServiceImpl();
  47. int result = service.updateDeliveryAddress(deliveryAddress);
  48. return result;
  49. }
  50. }

4.2.3.4.Food

  1. package com.neusoft.elm.controller;
  2. import java.util.List;
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletResponse;
  5. import com.neusoft.elm.po.Food;
  6. import com.neusoft.elm.service.FoodService;
  7. import com.neusoft.elm.service.impl.FoodServiceImpl;
  8. public class FoodController {
  9. public Object listFoodByBusinessId(HttpServletRequest request,HttpServletResponse response) throws Exception{
  10. Integer businessId = Integer.valueOf(request.getParameter("businessId"));
  11. FoodService service = new FoodServiceImpl();
  12. List<Food> list = service.listFoodByBusinessId(businessId);
  13. return list;
  14. }
  15. }

4.2.3.5.OrderDetailet

  1. package com.neusoft.elm.controller;
  2. import java.util.List;
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletResponse;
  5. import com.neusoft.elm.po.OrderDetailet;
  6. import com.neusoft.elm.service.OrderDetailetService;
  7. import com.neusoft.elm.service.impl.OrderDetailetServiceImpl;
  8. public class OrderDetailetController {
  9. public Object listOrderDetailetByOrderId(HttpServletRequest request,HttpServletResponse response) throws Exception{
  10. Integer orderId = Integer.valueOf(request.getParameter("orderId"));
  11. OrderDetailetService service = new OrderDetailetServiceImpl();
  12. List<OrderDetailet> list = service.listOrderDetailetByOrderId(orderId);
  13. return list;
  14. }
  15. }

4.2.3.6.Orders

  1. package com.neusoft.elm.controller;
  2. import java.util.List;
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletResponse;
  5. import com.neusoft.elm.po.Orders;
  6. import com.neusoft.elm.service.OrdersService;
  7. import com.neusoft.elm.service.impl.OrdersServiceImpl;
  8. public class OrdersController{
  9. //下订单(先清空当前用户购物车中当前商家的所有食品,然后生成订单和订单明细,最后返回订单号)
  10. public Object createOrders(HttpServletRequest request,HttpServletResponse response) throws Exception{
  11. String userId = request.getParameter("userId");
  12. Integer businessId = Integer.valueOf(request.getParameter("businessId"));
  13. Integer daId = Integer.valueOf(request.getParameter("daId"));
  14. OrdersService service = new OrdersServiceImpl();
  15. int orderId = service.createOrders(userId, businessId, daId);
  16. return orderId;
  17. }
  18. public Object getOrdersById(HttpServletRequest request,HttpServletResponse response) throws Exception{
  19. Integer orderId = Integer.valueOf(request.getParameter("orderId"));
  20. OrdersService service = new OrdersServiceImpl();
  21. Orders orders = service.getOrdersById(orderId);
  22. return orders;
  23. }
  24. public Object listOrdersByUserId(HttpServletRequest request,HttpServletResponse response) throws Exception{
  25. String userId = request.getParameter("userId");
  26. OrdersService service = new OrdersServiceImpl();
  27. List<Orders> list = service.listOrdersByUserId(userId);
  28. return list;
  29. }
  30. }

4.2.3.7.user

  1. package com.neusoft.elm.controller;
  2. import java.util.List;
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletResponse;
  5. import com.neusoft.elm.po.User;
  6. import com.neusoft.elm.service.UserService;
  7. import com.neusoft.elm.service.impl.UserServiceImpl;
  8. public class UserController{
  9. public Object saveUser(HttpServletRequest request,HttpServletResponse response) throws Exception{
  10. String userId = request.getParameter("userId");
  11. String password = request.getParameter("password");
  12. String userName = request.getParameter("userName");
  13. Integer userSex = Integer.valueOf(request.getParameter("userSex"));
  14. User user = new User(userId,password,userName,userSex,null);
  15. UserService service = new UserServiceImpl();
  16. int result = service.saveUser(user);
  17. return result;
  18. }
  19. public Object getUserByIdByPass(HttpServletRequest request,HttpServletResponse response) throws Exception{
  20. String userId = request.getParameter("userId");
  21. String password = request.getParameter("password");
  22. UserService service = new UserServiceImpl();
  23. User user = service.getUserByIdByPass(userId, password);
  24. if(user!=null) {
  25. request.getSession().setAttribute("user", user);
  26. }
  27. return user;
  28. }
  29. public Object getUserById(HttpServletRequest request,HttpServletResponse response) throws Exception{
  30. String userId = request.getParameter("userId");
  31. UserService service = new UserServiceImpl();
  32. User user = service.getUserById(userId);
  33. return user;
  34. }
  35. }