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

public class Cart {
    private Integer cartId;
    private Integer foodId;
    private Integer businessId;
    private String userId;
    private Integer quantity;
    //多对一:所属食品
    private Food food = new Food();
    //多对一:所属商家
    private Business business = new Business();
     //get、set ... ...
}

4.1.3.DeliveryAddress

public class DeliveryAddress {
    private Integer daId;
    private String contactName;
    private Integer contactSex;
    private String contactTel;
    private String address;
    private String userId;
    public DeliveryAddress() {}
    public DeliveryAddress(String contactName,Integer contactSex,String contactTel,String address,String userId) {
        this.contactName = contactName;
        this.contactSex = contactSex;
        this.contactTel = contactTel;
        this.address = address;
        this.userId = userId;
    }
     //get、set ... ...
}

4.1.4.Food

public class Food {
    private Integer foodId;
    private String foodName;
    private String foodExplain;
    private String foodImg;
    private Double foodPrice;
    private Integer businessId;
    private String remarks;
    //get、set ... ...
}

4.1.5.OrderDetailet

private Integer odId;
    private Integer orderId;
    private Integer foodId;
    private Integer quantity;
    //多对一:所属食品
    private Food food;
    //get、set ... ...
}

4.1.6.Orders

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

4.1.7.User

public class User {
    private String userId;
    private String password;
    private String userName;
    private Integer userSex;
    private String userImg;
    private Integer delTag;
    public User() {}
    public User(String userId,String password,String userName,Integer userSex,String userImg) {
        this.userId = userId;
        this.password = password;
        this.userName = userName;
        this.userSex = userSex;
        this.userImg = userImg;
        this.delTag = 1;
    }
    //get、set ... ...
}

4.2.服务器端代码

4.2.1.Dao层代码

4.2.1.1.Business

package com.neusoft.elm.dao;
import java.util.List;
import com.neusoft.elm.po.Business;
public interface BusinessDao {
    public List<Business> listBusinessByOrderTypeId(Integer orderTypeId) throws Exception;
    public Business getBusinessById(Integer businessId) throws Exception;
}
package com.neusoft.elm.dao.impl;
import java.util.ArrayList;
import java.util.List;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.neusoft.elm.dao.BusinessDao;
import com.neusoft.elm.po.Business;
import com.neusoft.elm.util.DBUtil;
public class BusinessDaoImpl implements BusinessDao{
    private Connection con = null;
    private PreparedStatement pst = null;
    private ResultSet rs = null;
    @Override
    public List<Business> listBusinessByOrderTypeId(Integer orderTypeId) throws Exception{
        List<Business> list = new ArrayList<>();
        String sql = "select * from business where orderTypeId=? order by businessId";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1, orderTypeId);
            rs = pst.executeQuery();
            while(rs.next()) {
                Business business = new Business();
                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));
                list.add(business);
            }
        }finally {
            DBUtil.close(rs, pst);
        }
        return list;
    }
    public Business getBusinessById(Integer businessId) throws Exception{
        Business business = null;
        String sql = "select * from business where businessId=?";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1, businessId);
            rs = pst.executeQuery();
            if(rs.next()) {
                business = new Business();
                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));
            }
        }finally {
            DBUtil.close(rs, pst);
        }
        return business;
    }
}

4.2.1.2.Cart

package com.neusoft.elm.dao;
import java.util.List;
import com.neusoft.elm.po.Cart;
public interface CartDao {
    public List<Cart> listCart(Cart cart) throws Exception;
    public int saveCart(Cart cart) throws Exception;
    public int removeCart(Cart cart) throws Exception;
    public int updateCart(Cart cart) throws Exception;
}
package com.neusoft.elm.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.neusoft.elm.dao.CartDao;
import com.neusoft.elm.po.Business;
import com.neusoft.elm.po.Cart;
import com.neusoft.elm.po.Food;
import com.neusoft.elm.util.DBUtil;
public class CartDaoImpl implements CartDao{
    private Connection con = null;
    private PreparedStatement pst = null;
    private ResultSet rs = null;
    @Override
    public List<Cart> listCart(Cart cart) throws Exception{
        List<Cart> list = new ArrayList<>();
        //这里注意:cart表与food表中都有foodId、businessId字段,所以要取不同的别名
        StringBuffer sql = new StringBuffer();
        sql.append(" select c.*, ");
        sql.append("        f.foodId ffoodId, ");
        sql.append("        f.foodName ffoodName, ");
        sql.append("        f.foodExplain ffoodExplain, ");
        sql.append("        f.foodImg ffoodImg, ");
        sql.append("        f.foodPrice ffoodPrice, ");
        sql.append("        f.businessId fbusinessId, ");
        sql.append("        f.remarks fremarks, ");
        sql.append("        b.businessId bbusinessId, ");
        sql.append("        b.businessName bbusinessName, ");
        sql.append("        b.businessAddress bbusinessAddress, ");
        sql.append("        b.businessExplain bbusinessExplain, ");
        sql.append("        b.businessImg bbusinessImg, ");
        sql.append("        b.orderTypeId borderTypeId, ");
        sql.append("        b.starPrice bstarPrice, ");
        sql.append("        b.deliveryPrice bdeliveryPrice");
        sql.append(" from (cart c left join food f on c.foodId=f.foodId) ");
        sql.append("      left join business b on c.businessId=b.businessId ");
        sql.append(" where 1=1 ");
        if(cart.getBusinessId()!=null) {
            sql.append(" and c.businessId="+cart.getBusinessId());
        }
        if(cart.getUserId()!=null) {
            sql.append(" and c.userId='"+cart.getUserId()+"'");
        }
        //System.out.println(sql.toString());
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql.toString());
            rs = pst.executeQuery();
            while(rs.next()) {
                Cart c = new Cart();
                c.setCartId(rs.getInt("cartId"));
                c.setFoodId(rs.getInt("foodId"));
                c.setBusinessId(rs.getInt("businessId"));
                c.setUserId(rs.getString("userId"));
                c.setQuantity(rs.getInt("quantity"));
                Food f = new Food();
                f.setFoodId(rs.getInt("ffoodId"));
                f.setFoodName(rs.getString("ffoodName"));
                f.setFoodExplain(rs.getString("ffoodExplain"));
                f.setFoodImg(rs.getString("ffoodImg"));
                f.setFoodPrice(rs.getDouble("ffoodPrice"));
                f.setBusinessId(rs.getInt("fbusinessId"));
                f.setRemarks(rs.getString("fremarks"));
                c.setFood(f);
                Business b = new Business();
                b.setBusinessId(rs.getInt("bbusinessId"));
                b.setBusinessName(rs.getString("bbusinessName"));
                b.setBusinessAddress(rs.getString("bbusinessAddress"));
                b.setBusinessExplain(rs.getString("bbusinessExplain"));
                b.setBusinessImg(rs.getString("bbusinessImg"));
                b.setOrderTypeId(rs.getInt("borderTypeId"));
                b.setStarPrice(rs.getDouble("bstarPrice"));
                b.setDeliveryPrice(rs.getDouble("bdeliveryPrice"));
                c.setBusiness(b);
                list.add(c);
            }
        }finally {
            DBUtil.close(rs,pst);
        }
        return list;
    }
    @Override
    public int saveCart(Cart cart) throws Exception{
        int result = 0;
        String sql = "insert into cart values(null,?,?,?,1)";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1, cart.getFoodId());
            pst.setInt(2, cart.getBusinessId());
            pst.setString(3, cart.getUserId());
            result = pst.executeUpdate();
        }finally {
            DBUtil.close(pst);
        }
        return result;
    }
    //清空当前用户购物车中当前商家的所有食品
    @Override
    public int removeCart(Cart cart) throws Exception{
        int result = 0;
        StringBuffer sql = new StringBuffer("delete from cart where 1=1 ");
        if(cart.getFoodId()!=null) {
            sql.append(" and foodId="+cart.getFoodId());
        }
        if(cart.getBusinessId()!=null) {
            sql.append(" and businessId="+cart.getBusinessId());
        }
        if(cart.getUserId()!=null) {
            sql.append(" and userId='"+cart.getUserId()+"'");
        }
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql.toString());
            result = pst.executeUpdate();
        }finally {
            DBUtil.close(pst);
        }
        return result;
    }
    @Override
    public int updateCart(Cart cart) throws Exception{
        int result = 0;
        String sql = "update cart set quantity=? where foodId=? and businessId=? and userId=?";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1, cart.getQuantity());
            pst.setInt(2, cart.getFoodId());
            pst.setInt(3, cart.getBusinessId());
            pst.setString(4, cart.getUserId());
            result = pst.executeUpdate();
        }finally {
            DBUtil.close(pst);
        }
        return result;
    }
}

4.2.1.3.DeliveryAddress

package com.neusoft.elm.dao;
import java.util.List;
import com.neusoft.elm.po.DeliveryAddress;
import com.neusoft.elm.po.Orders;
public interface DeliveryAddressDao {
    public List<DeliveryAddress> listDeliveryAddressByUserId(String userId) throws Exception;
    public int saveDeliveryAddress(DeliveryAddress deliveryAddress) throws Exception;
    public int removeDeliveryAddress(Integer daId) throws Exception;
    public DeliveryAddress getDeliveryAddressById(Integer daId) throws Exception;
    public int updateDeliveryAddress(DeliveryAddress deliveryAddress) throws Exception;
}
package com.neusoft.elm.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.neusoft.elm.dao.DeliveryAddressDao;
import com.neusoft.elm.po.DeliveryAddress;
import com.neusoft.elm.util.DBUtil;
public class DeliveryAddressDaoImpl implements DeliveryAddressDao{
    private Connection con = null;
    private PreparedStatement pst = null;
    private ResultSet rs = null;
    @Override
    public List<DeliveryAddress> listDeliveryAddressByUserId(String userId) throws Exception {
        List<DeliveryAddress> list = new ArrayList<>();
        String sql = "select * from deliveryAddress where userId=? order by daId";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setString(1, userId);
            rs = pst.executeQuery();
            while(rs.next()) {
                DeliveryAddress deliveryAddress = new DeliveryAddress();
                deliveryAddress.setDaId(rs.getInt("daId"));
                deliveryAddress.setContactName(rs.getString("contactName"));
                deliveryAddress.setContactSex(rs.getInt("contactSex"));
                deliveryAddress.setContactTel(rs.getString("contactTel"));
                deliveryAddress.setAddress(rs.getString("address"));
                deliveryAddress.setUserId(rs.getString("userId"));
                list.add(deliveryAddress);
            }
        }finally {
            DBUtil.close(rs, pst);
        }
        return list;
    }
    @Override
    public int saveDeliveryAddress(DeliveryAddress deliveryAddress) throws Exception{
        int result = 0;
        String sql = "insert into deliveryAddress values(null,?,?,?,?,?)";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setString(1, deliveryAddress.getContactName());
            pst.setInt(2, deliveryAddress.getContactSex());
            pst.setString(3, deliveryAddress.getContactTel());
            pst.setString(4, deliveryAddress.getAddress());
            pst.setString(5, deliveryAddress.getUserId());
            result = pst.executeUpdate();
        }finally {
            DBUtil.close(pst);
        }
        return result;
    }
    @Override
    public int removeDeliveryAddress(Integer daId) throws Exception{
        int result = 0;
        String sql = "delete from deliveryAddress where daId=?";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1, daId);
            result = pst.executeUpdate();
        }finally {
            DBUtil.close(pst);
        }
        return result;
    }
    public DeliveryAddress getDeliveryAddressById(Integer daId) throws Exception{
        DeliveryAddress deliveryAddress = null;
        String sql = "select * from deliveryAddress where daId=?";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1, daId);
            rs = pst.executeQuery();
            if(rs.next()) {
                deliveryAddress = new DeliveryAddress();
                deliveryAddress.setDaId(rs.getInt("daId"));
                deliveryAddress.setContactName(rs.getString("contactName"));
                deliveryAddress.setContactSex(rs.getInt("contactSex"));
                deliveryAddress.setContactTel(rs.getString("contactTel"));
                deliveryAddress.setAddress(rs.getString("address"));
                deliveryAddress.setUserId(rs.getString("userId"));
            }
        }finally {
            DBUtil.close(rs, pst);
        }
        return deliveryAddress;
    }
    @Override
    public int updateDeliveryAddress(DeliveryAddress deliveryAddress) throws Exception{
        int result = 0;
        String sql = "update deliveryAddress set contactName=?,contactSex=?,contactTel=?,address=?,userId=? where daId=?";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setString(1, deliveryAddress.getContactName());
            pst.setInt(2, deliveryAddress.getContactSex());
            pst.setString(3, deliveryAddress.getContactTel());
            pst.setString(4, deliveryAddress.getAddress());
            pst.setString(5, deliveryAddress.getUserId());
            pst.setInt(6, deliveryAddress.getDaId());
            result = pst.executeUpdate();
        }finally {
            DBUtil.close(pst);
        }
        return result;
    }
}

4.2.1.4.Food

package com.neusoft.elm.dao;
import java.util.List;
import com.neusoft.elm.po.Food;
public interface FoodDao {
    public List<Food> listFoodByBusinessId(Integer businessId) throws Exception;
}
package com.neusoft.elm.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.neusoft.elm.dao.FoodDao;
import com.neusoft.elm.po.Food;
import com.neusoft.elm.util.DBUtil;
public class FoodDaoImpl implements FoodDao{
    private Connection con = null;
    private PreparedStatement pst = null;
    private ResultSet rs = null;
    @Override
    public List<Food> listFoodByBusinessId(Integer businessId) throws Exception{
        List<Food> list = new ArrayList<>();
        String sql = "select * from food where businessId=? order by foodId";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1, businessId);
            rs = pst.executeQuery();
            while(rs.next()) {
                Food food = new Food();
                food.setFoodId(rs.getInt("foodId"));
                food.setFoodName(rs.getString("foodName"));
                food.setFoodExplain(rs.getString("foodExplain"));
                food.setFoodImg(rs.getString("foodImg"));
                food.setFoodPrice(rs.getDouble("foodPrice"));
                food.setBusinessId(rs.getInt("businessId"));
                food.setRemarks(rs.getString("remarks"));
                list.add(food);
            }
        }finally {
            DBUtil.close(rs, pst);
        }
        return list;
    }
}

4.2.1.5.OrderDetailet

package com.neusoft.elm.dao;
import java.util.List;
import com.neusoft.elm.po.OrderDetailet;
public interface OrderDetailetDao {
    public int saveOrderDetailetBatch(List<OrderDetailet> list) throws Exception;
    public List<OrderDetailet> listOrderDetailetByOrderId(Integer orderId) throws Exception;
}
package com.neusoft.elm.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.neusoft.elm.dao.OrderDetailetDao;
import com.neusoft.elm.po.Food;
import com.neusoft.elm.po.OrderDetailet;
import com.neusoft.elm.util.DBUtil;
public class OrderDetailetDaoImpl implements OrderDetailetDao{
    private Connection con = null;
    private PreparedStatement pst = null;
    private ResultSet rs = null;
    @Override
    public int saveOrderDetailetBatch(List<OrderDetailet> list) throws Exception {
        int result = 0;
        StringBuffer stringBuffer = new StringBuffer("insert into orderDetailet(orderId,foodId,quantity) values");
        for(OrderDetailet od : list) {
            stringBuffer.append("("+od.getOrderId()+","+od.getFoodId()+","+od.getQuantity()+"),");
        }
        //去掉sql语句中的最后一个逗号
        String sql = stringBuffer.toString().substring(0,stringBuffer.toString().length()-1);
        //System.out.println(sql);
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            result = pst.executeUpdate();
        }finally {
            DBUtil.close(pst);
        }
        return result;
    }
    @Override
    public List<OrderDetailet> listOrderDetailetByOrderId(Integer orderId) throws Exception{
        List<OrderDetailet> list = new ArrayList<>();
        StringBuffer sql = new StringBuffer();
        sql.append(" select o.*, ");
        sql.append("        f.foodId ffoodId, ");
        sql.append("        f.foodName ffoodName, ");
        sql.append("        f.foodPrice ffoodPrice ");
        sql.append(" from orderDetailet o left join food f on o.foodId=f.foodId ");
        sql.append(" where o.orderId=? ");
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql.toString());
            pst.setInt(1, orderId);
            rs = pst.executeQuery();
            while(rs.next()) {
                OrderDetailet od = new OrderDetailet();
                od.setOdId(rs.getInt("odId"));
                od.setOrderId(rs.getInt("orderId"));
                od.setFoodId(rs.getInt("foodId"));
                od.setQuantity(rs.getInt("quantity"));
                Food food = new Food();
                food.setFoodId(rs.getInt("ffoodId"));
                food.setFoodName(rs.getString("ffoodName"));
                food.setFoodPrice(rs.getDouble("ffoodPrice"));
                od.setFood(food);
                list.add(od);
            }
        }finally {
            DBUtil.close(rs,pst);
        }
        return list;
    }
}

4.2.1.6.Orders

package com.neusoft.elm.dao;
import java.util.List;
import com.neusoft.elm.po.Orders;
public interface OrdersDao {
    //注意:此方法会返回生成的主键
    public int saveOrders(Orders orders) throws Exception;
    public Orders getOrdersById(Integer orderId) throws Exception;
    public List<Orders> listOrdersById(String userId) throws Exception;
}
package com.neusoft.elm.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.neusoft.elm.dao.OrdersDao;
import com.neusoft.elm.po.Business;
import com.neusoft.elm.po.Orders;
import com.neusoft.elm.util.CommonUtil;
import com.neusoft.elm.util.DBUtil;
public class OrdersDaoImpl implements OrdersDao{
    private Connection con = null;
    private PreparedStatement pst = null;
    private ResultSet rs = null;
    /**
     * 注意:此方法会返回生成的主键
     */
    @Override
    public int saveOrders(Orders orders) throws Exception{
        int result = 0;
        String sql = "insert into orders values(null,?,?,?,?,?,0)";
        try {
            con = DBUtil.getConnection();
            //设置返回自增长列值
            pst = con.prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS);
            pst.setString(1, orders.getUserId());
            pst.setInt(2, orders.getBusinessId());
            pst.setString(3, CommonUtil.getCurrentDate());
            pst.setDouble(4, orders.getOrderTotal());
            pst.setInt(5, orders.getDaId());
            pst.executeUpdate();
            //获取自增长列值
            rs = pst.getGeneratedKeys();
            if(rs.next()){
                result = rs.getInt(1);
            } 
        } finally {
            DBUtil.close(pst);
        }
        return result;
    }
    @Override
    public Orders getOrdersById(Integer orderId) throws Exception {
        Orders orders = null;
        StringBuffer sql = new StringBuffer();
        sql.append(" select o.*, ");
        sql.append("        b.businessId bbusinessId, ");
        sql.append("        b.businessName bbusinessName, ");
        sql.append("        b.deliveryPrice bdeliveryPrice ");
        sql.append(" from orders o left join business b on o.businessId=b.businessId ");
        sql.append(" where o.orderId=? ");
        try {
            con = DBUtil.getConnection();
            //设置返回自增长列值
            pst = con.prepareStatement(sql.toString());
            pst.setInt(1, orderId);
            rs = pst.executeQuery();
            if(rs.next()){
                orders = new Orders();
                orders.setOrderId(rs.getInt("orderId"));
                orders.setUserId(rs.getString("userId"));
                orders.setBusinessId(rs.getInt("businessId"));
                orders.setOrderDate(rs.getString("orderDate"));
                orders.setOrderTotal(rs.getDouble("orderTotal"));
                orders.setOrderState(rs.getInt("orderState"));
                Business business = new Business();
                business.setBusinessId(rs.getInt("bbusinessId"));
                business.setBusinessName(rs.getString("bbusinessName"));
                business.setDeliveryPrice(rs.getDouble("bdeliveryPrice"));
                orders.setBusiness(business);
            } 
        } finally {
            DBUtil.close(rs,pst);
        }
        return orders;
    }
    @Override
    public List<Orders> listOrdersById(String userId) throws Exception{
        List<Orders> list = new ArrayList<>();
        StringBuffer sql = new StringBuffer();
        sql.append(" select o.*, ");
        sql.append("        b.businessId bbusinessId, ");
        sql.append("        b.businessName bbusinessName, ");
        sql.append("        b.deliveryPrice bdeliveryPrice ");
        sql.append(" from orders o left join business b on o.businessId=b.businessId ");
        sql.append(" where o.userId=?");
        try {
            con = DBUtil.getConnection();
            //设置返回自增长列值
            pst = con.prepareStatement(sql.toString());
            pst.setString(1, userId);
            rs = pst.executeQuery();
            while(rs.next()){
                Orders o = new Orders();
                o.setOrderId(rs.getInt("orderId"));
                o.setUserId(rs.getString("userId"));
                o.setBusinessId(rs.getInt("businessId"));
                o.setOrderDate(rs.getString("orderDate"));
                o.setOrderTotal(rs.getDouble("orderTotal"));
                o.setDaId(rs.getInt("daId"));
                o.setOrderState(rs.getInt("orderState"));
                Business business = new Business();
                business.setBusinessId(rs.getInt("bbusinessId"));
                business.setBusinessName(rs.getString("bbusinessName"));
                business.setDeliveryPrice(rs.getDouble("bdeliveryPrice"));
                o.setBusiness(business);
                list.add(o);
            } 
        } finally {
            DBUtil.close(rs,pst);
        }
        return list;
    }
}

4.2.1.7.User

package com.neusoft.elm.dao;
import com.neusoft.elm.po.User;
public interface UserDao {
    public int saveUser(User user) throws Exception;
    public User getUserByIdByPass(String userId,String password) throws Exception;
    public User getUserById(String userId) throws Exception;
}
package com.neusoft.elm.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.neusoft.elm.dao.UserDao;
import com.neusoft.elm.po.Business;
import com.neusoft.elm.po.User;
import com.neusoft.elm.util.DBUtil;
public class UserDaoImpl implements UserDao{
    private Connection con = null;
    private PreparedStatement pst = null;
    private ResultSet rs = null;
    /**
     * dao层不再处理异常,因为异常要返回给service,用于事务管理
     */
    @Override
    public int saveUser(User user) throws Exception{
        int result = 0;
        String sql = "insert into user values(?,?,?,?,?,1)";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setString(1, user.getUserId());
            pst.setString(2, user.getPassword());
            pst.setString(3, user.getUserName());
            pst.setInt(4, user.getUserSex());
            pst.setString(5, user.getUserImg());
            result = pst.executeUpdate();
        } finally {
            DBUtil.close(pst);
        }
        return result;
    }
    public User getUserByIdByPass(String userId,String password) throws Exception{
        User userResult = null;
        String sql = "select * from user where userId=? and password=?";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setString(1, userId);
            pst.setString(2, password);
            rs = pst.executeQuery();
            if(rs.next()) {
                userResult = new User();
                userResult.setUserId(rs.getString("userId"));
                userResult.setPassword(rs.getString("password"));
                userResult.setUserName(rs.getString("userName"));
                userResult.setUserSex(rs.getInt("userSex"));
                userResult.setUserImg(rs.getString("userImg"));
                userResult.setDelTag(rs.getInt("delTag"));
            }
        }finally {
            DBUtil.close(rs, pst);
        }
        return userResult;
    }
    public User getUserById(String userId) throws Exception{
        User userResult = null;
        String sql = "select * from user where userId=?";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setString(1, userId);
            rs = pst.executeQuery();
            if(rs.next()) {
                userResult = new User();
                userResult.setUserId(rs.getString("userId"));
                userResult.setPassword(rs.getString("password"));
                userResult.setUserName(rs.getString("userName"));
                userResult.setUserSex(rs.getInt("userSex"));
                userResult.setUserImg(rs.getString("userImg"));
                userResult.setDelTag(rs.getInt("delTag"));
            }
        }finally {
            DBUtil.close(rs, pst);
        }
        return userResult;
    }
}

4.2.2.Service层代码

4.2.2.1.Business

package com.neusoft.elm.service;
import java.util.List;
import com.neusoft.elm.po.Business;
public interface BusinessService {
    public List<Business> listBusinessByOrderTypeId(Integer orderTypeId);
    public Business getBusinessById(Integer businessId);
}
package com.neusoft.elm.service.impl;
import java.util.ArrayList;
import java.util.List;
import com.neusoft.elm.dao.BusinessDao;
import com.neusoft.elm.dao.impl.BusinessDaoImpl;
import com.neusoft.elm.po.Business;
import com.neusoft.elm.service.BusinessService;
import com.neusoft.elm.util.DBUtil;
public class BusinessServiceImpl implements BusinessService{
    @Override
    public List<Business> listBusinessByOrderTypeId(Integer orderTypeId) {
        List<Business> list = new ArrayList<>();
        BusinessDao dao = new BusinessDaoImpl();
        try {
            DBUtil.getConnection();
            list = dao.listBusinessByOrderTypeId(orderTypeId);
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return list;
    }
    @Override
    public Business getBusinessById(Integer businessId) {
        Business business = null;
        BusinessDao dao = new BusinessDaoImpl();
        try {
            DBUtil.getConnection();
            business = dao.getBusinessById(businessId);
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return business;
    }
}

4.2.2.2.Cart

package com.neusoft.elm.service;
import java.util.List;
import com.neusoft.elm.po.Cart;
public interface CartService {
    public List<Cart> listCart(Cart cart);
    public int saveCart(Cart cart);
    public int removeCart(Cart cart);
    public int updateCart(Cart cart);
}
package com.neusoft.elm.service.impl;
import java.util.ArrayList;
import java.util.List;
import com.neusoft.elm.dao.CartDao;
import com.neusoft.elm.dao.impl.CartDaoImpl;
import com.neusoft.elm.po.Cart;
import com.neusoft.elm.service.CartService;
import com.neusoft.elm.util.DBUtil;
public class CartServiceImpl implements CartService {
    @Override
    public List<Cart> listCart(Cart cart) {
        List<Cart> list = new ArrayList<>();
        CartDao dao = new CartDaoImpl();
        try {
            DBUtil.getConnection();
            list = dao.listCart(cart);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return list;
    }
    @Override
    public int saveCart(Cart cart) {
        int result = 0;
        CartDao dao = new CartDaoImpl();
        try {
            DBUtil.getConnection();
            result = dao.saveCart(cart);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return result;
    }
    @Override
    public int removeCart(Cart cart) {
        int result = 0;
        CartDao dao = new CartDaoImpl();
        try {
            DBUtil.getConnection();
            result = dao.removeCart(cart);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return result;
    }
    @Override
    public int updateCart(Cart cart) {
        int result = 0;
        CartDao dao = new CartDaoImpl();
        try {
            DBUtil.getConnection();
            result = dao.updateCart(cart);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return result;
    }
}

4.2.2.3.DeliveryAddress

package com.neusoft.elm.service;
import java.util.List;
import com.neusoft.elm.po.DeliveryAddress;
public interface DeliveryAddressService {
    public List<DeliveryAddress> listDeliveryAddressByUserId(String userId);
    public int saveDeliveryAddress(DeliveryAddress deliveryAddress);
    public int removeDeliveryAddress(Integer daId);
    public DeliveryAddress getDeliveryAddressById(Integer daId);
    public int updateDeliveryAddress(DeliveryAddress deliveryAddress);
}
package com.neusoft.elm.service.impl;
import java.util.ArrayList;
import java.util.List;
import com.neusoft.elm.dao.DeliveryAddressDao;
import com.neusoft.elm.dao.impl.DeliveryAddressDaoImpl;
import com.neusoft.elm.po.DeliveryAddress;
import com.neusoft.elm.service.DeliveryAddressService;
import com.neusoft.elm.util.DBUtil;
public class DeliveryAddressServiceImpl implements DeliveryAddressService{
    @Override
    public List<DeliveryAddress> listDeliveryAddressByUserId(String userId) {
        List<DeliveryAddress> list = new ArrayList<>();
        DeliveryAddressDao dao = new DeliveryAddressDaoImpl();
        try {
            DBUtil.getConnection();
            list = dao.listDeliveryAddressByUserId(userId);
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return list;
    }
    @Override
    public int saveDeliveryAddress(DeliveryAddress deliveryAddress) {
        int result = 0;
        DeliveryAddressDao dao = new DeliveryAddressDaoImpl();
        try {
            DBUtil.getConnection();
            result = dao.saveDeliveryAddress(deliveryAddress);
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return result;
    }
    @Override
    public int removeDeliveryAddress(Integer daId) {
        int result = 0;
        DeliveryAddressDao dao = new DeliveryAddressDaoImpl();
        try {
            DBUtil.getConnection();
            result = dao.removeDeliveryAddress(daId);
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return result;
    }
    @Override
    public DeliveryAddress getDeliveryAddressById(Integer daId) {
        DeliveryAddress deliveryAddress = null;
        DeliveryAddressDao dao = new DeliveryAddressDaoImpl();
        try {
            DBUtil.getConnection();
            deliveryAddress = dao.getDeliveryAddressById(daId);
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return deliveryAddress;
    }
    @Override
    public int updateDeliveryAddress(DeliveryAddress deliveryAddress) {
        int result = 0;
        DeliveryAddressDao dao = new DeliveryAddressDaoImpl();
        try {
            DBUtil.getConnection();
            result = dao.updateDeliveryAddress(deliveryAddress);
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return result;
    }
}

4.2.2.4.Food

package com.neusoft.elm.service;
import java.util.List;
import com.neusoft.elm.po.Food;
public interface FoodService {
    public List<Food> listFoodByBusinessId(Integer businessId);
}
package com.neusoft.elm.service.impl;
import java.util.ArrayList;
import java.util.List;
import com.neusoft.elm.dao.BusinessDao;
import com.neusoft.elm.dao.FoodDao;
import com.neusoft.elm.dao.impl.BusinessDaoImpl;
import com.neusoft.elm.dao.impl.FoodDaoImpl;
import com.neusoft.elm.po.Business;
import com.neusoft.elm.po.Food;
import com.neusoft.elm.service.FoodService;
import com.neusoft.elm.util.DBUtil;
public class FoodServiceImpl implements FoodService{
    @Override
    public List<Food> listFoodByBusinessId(Integer businessId) {
        List<Food> list = new ArrayList<>();
        FoodDao dao = new FoodDaoImpl();
        try {
            DBUtil.getConnection();
            list = dao.listFoodByBusinessId(businessId);
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return list;
    }
}

4.2.2.5.OrderDetailet

package com.neusoft.elm.service;
import java.util.List;
import com.neusoft.elm.po.OrderDetailet;
public interface OrderDetailetService {
    public List<OrderDetailet> listOrderDetailetByOrderId(Integer orderId);
}
package com.neusoft.elm.service.impl;
import java.util.ArrayList;
import java.util.List;
import com.neusoft.elm.dao.OrderDetailetDao;
import com.neusoft.elm.dao.impl.OrderDetailetDaoImpl;
import com.neusoft.elm.po.OrderDetailet;
import com.neusoft.elm.service.OrderDetailetService;
import com.neusoft.elm.util.DBUtil;
public class OrderDetailetServiceImpl implements OrderDetailetService{
    @Override
    public List<OrderDetailet> listOrderDetailetByOrderId(Integer orderId) {
        List<OrderDetailet> list = new ArrayList<>();
        OrderDetailetDao dao = new OrderDetailetDaoImpl();
        try {
            DBUtil.getConnection();
            list = dao.listOrderDetailetByOrderId(orderId);
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return list;
    }
}

4.2.2.6.Orders

package com.neusoft.elm.service;
import java.util.List;
import com.neusoft.elm.po.Orders;
public interface OrdersService {
    //下订单(先清空当前用户购物车中当前商家的所有食品,然后生成订单和订单明细,最后返回订单号)
    public int createOrders(String userId,Integer businessId,Integer daId);
    public Orders getOrdersById(Integer orderId);
    public List<Orders> listOrdersByUserId(String userId);
}
package com.neusoft.elm.service.impl;
import java.util.ArrayList;
import java.util.List;
import com.neusoft.elm.dao.BusinessDao;
import com.neusoft.elm.dao.CartDao;
import com.neusoft.elm.dao.OrdersDao;
import com.neusoft.elm.dao.OrderDetailetDao;
import com.neusoft.elm.dao.impl.BusinessDaoImpl;
import com.neusoft.elm.dao.impl.CartDaoImpl;
import com.neusoft.elm.dao.impl.OrdersDaoImpl;
import com.neusoft.elm.dao.impl.OrderDetailetDaoImpl;
import com.neusoft.elm.po.Business;
import com.neusoft.elm.po.Cart;
import com.neusoft.elm.po.Orders;
import com.neusoft.elm.po.OrderDetailet;
import com.neusoft.elm.service.OrdersService;
import com.neusoft.elm.util.DBUtil;
public class OrdersServiceImpl implements OrdersService {
    //下订单(先清空当前用户购物车中当前商家的所有食品,然后生成订单和订单明细,最后返回订单号)
    @Override
    public int createOrders(String userId,Integer businessId,Integer daId) {
        int orderId = 0;
        BusinessDao businessDao = new BusinessDaoImpl();
        CartDao cartDao = new CartDaoImpl();
        OrdersDao ordersDao = new OrdersDaoImpl();
        OrderDetailetDao orderDetailetDao = new OrderDetailetDaoImpl();
        try {
            DBUtil.beginTransaction();
            //1、查询当前用户购物车中当前商家的所有食品
            Cart cart = new Cart();
            cart.setBusinessId(businessId);
            cart.setUserId(userId);
            List<Cart> cartList = cartDao.listCart(cart);
            //2、查询商家信息(需要使用商家的配送费信息)
            Business business = businessDao.getBusinessById(businessId);
            //3、获取订单总额
            Double ordersTotal = 0.0;  //订单总额
            for(Cart c : cartList) {
                //累计所有食品总价格
                ordersTotal += c.getFood().getFoodPrice()*c.getQuantity();
            }
            //加上配送费
            ordersTotal += business.getDeliveryPrice();
            //3、创建订单,并获取订单编号
            Orders orders = new Orders();
            orders.setUserId(userId);
            orders.setBusinessId(businessId);
            orders.setOrderTotal(ordersTotal);
            orders.setDaId(daId);
            orderId = ordersDao.saveOrders(orders);
            //4、处理相关数据:获取订单明细集合
            List<OrderDetailet> orderDetailetList = new ArrayList<>();  //订单明细集合
            for(Cart c : cartList) {
                OrderDetailet od = new OrderDetailet();
                od.setOrderId(orderId);
                od.setFoodId(c.getFoodId());
                od.setQuantity(c.getQuantity());
                orderDetailetList.add(od);
            }
            //5、批量添加订单明细
            orderDetailetDao.saveOrderDetailetBatch(orderDetailetList);
            //6、清空当前用户购物车中当前商家的所有食品
            cartDao.removeCart(cart);
            DBUtil.commitTransaction();
        } catch (Exception e) {
            orderId = 0;
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            DBUtil.close(); // 关闭Connection
        }
        return orderId;
    }
    @Override
    public Orders getOrdersById(Integer orderId) {
        Orders orders = null;
        OrdersDao dao = new OrdersDaoImpl();
        try {
            DBUtil.getConnection();
            orders = dao.getOrdersById(orderId);
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return orders;
    }
    @Override
    public List<Orders> listOrdersByUserId(String userId){
        List<Orders> list = new ArrayList<>();
        OrdersDao ordersDao = new OrdersDaoImpl();
        OrderDetailetDao orderDetailetDao = new OrderDetailetDaoImpl();
        try {
            DBUtil.beginTransaction();
            //先查询当前用户所有订单
            list = ordersDao.listOrdersById(userId);
            for(Orders o : list) {
                //在查询每个订单的明细
                List<OrderDetailet> odList = orderDetailetDao.listOrderDetailetByOrderId(o.getOrderId());
                o.setOdList(odList);
            }
            DBUtil.commitTransaction();
        } catch (Exception e) {
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            DBUtil.close(); // 关闭Connection
        }
        return list;
    }
}

4.2.2.7.user

package com.neusoft.elm.service;
import com.neusoft.elm.po.User;
public interface UserService {
    public int saveUser(User user);
    public User getUserByIdByPass(String userId,String password);
    public User getUserById(String userId);
}
package com.neusoft.elm.service.impl;
import java.util.List;
import com.neusoft.elm.dao.UserDao;
import com.neusoft.elm.dao.impl.UserDaoImpl;
import com.neusoft.elm.po.User;
import com.neusoft.elm.service.UserService;
import com.neusoft.elm.util.DBUtil;
public class UserServiceImpl implements UserService{
    @Override
    public int saveUser(User user) {
        int result = 1;
        UserDao dao = new UserDaoImpl();
        try {
            DBUtil.getConnection();
            dao.saveUser(user);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return result;
    }
    public User getUserByIdByPass(String userId,String password){
        User userResult = null;
        UserDao dao = new UserDaoImpl();
        try {
            DBUtil.getConnection();
            userResult = dao.getUserByIdByPass(userId,password);
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return userResult;
    }
    public User getUserById(String userId) {
        User userResult = null;
        UserDao dao = new UserDaoImpl();
        try {
            DBUtil.getConnection();
            userResult = dao.getUserById(userId);
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return userResult;
    }
}

4.2.3.Controller层代码

4.2.3.1.Business

package com.neusoft.elm.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.neusoft.elm.po.Business;
import com.neusoft.elm.service.BusinessService;
import com.neusoft.elm.service.impl.BusinessServiceImpl;
public class BusinessController{
    public Object listBusinessByOrderTypeId(HttpServletRequest request,HttpServletResponse response) throws Exception{
        Integer orderTypeId = Integer.valueOf(request.getParameter("orderTypeId"));
        BusinessService service = new BusinessServiceImpl();
        List<Business> list = service.listBusinessByOrderTypeId(orderTypeId);
        return list;
    }
    public Object getBusinessById(HttpServletRequest request,HttpServletResponse response) throws Exception{
        Integer businessId = Integer.valueOf(request.getParameter("businessId"));
        BusinessService service = new BusinessServiceImpl();
        Business business = service.getBusinessById(businessId);
        return business;
    }
}

4.2.3.2.Cart

package com.neusoft.elm.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.neusoft.elm.po.Cart;
import com.neusoft.elm.service.CartService;
import com.neusoft.elm.service.impl.CartServiceImpl;
public class CartController{
    public Object listCart(HttpServletRequest request,HttpServletResponse response) throws Exception{
        Cart cart = new Cart();
        if(request.getParameter("businessId")!=null) {
            cart.setBusinessId(Integer.parseInt(request.getParameter("businessId")));
        }
        if(request.getParameter("userId")!=null) {
            cart.setUserId(request.getParameter("userId"));
        }
        CartService service = new CartServiceImpl();
        List<Cart> cartResult = service.listCart(cart);
        return cartResult;
    }
    public Object saveCart(HttpServletRequest request,HttpServletResponse response) throws Exception{
        Cart cart = new Cart();
        cart.setFoodId(Integer.valueOf(request.getParameter("foodId")));
        cart.setBusinessId(Integer.valueOf(request.getParameter("businessId")));
        cart.setUserId(request.getParameter("userId"));
        CartService service = new CartServiceImpl();
        int result = service.saveCart(cart);
        return result;
    }
    public Object removeCart(HttpServletRequest request,HttpServletResponse response) throws Exception{
        Cart cart = new Cart();
        cart.setFoodId(Integer.valueOf(request.getParameter("foodId")));
        cart.setBusinessId(Integer.valueOf(request.getParameter("businessId")));
        cart.setUserId(request.getParameter("userId"));
        CartService service = new CartServiceImpl();
        int result = service.removeCart(cart);
        return result;
    }
    public Object updateCart(HttpServletRequest request,HttpServletResponse response) throws Exception{
        Cart cart = new Cart();
        cart.setFoodId(Integer.valueOf(request.getParameter("foodId")));
        cart.setBusinessId(Integer.valueOf(request.getParameter("businessId")));
        cart.setUserId(request.getParameter("userId"));
        cart.setQuantity(Integer.valueOf(request.getParameter("quantity")));
        CartService service = new CartServiceImpl();
        int result = service.updateCart(cart);
        return result;
    }
}

4.2.3.3.DeliveryAddress

package com.neusoft.elm.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.neusoft.elm.po.DeliveryAddress;
import com.neusoft.elm.service.DeliveryAddressService;
import com.neusoft.elm.service.impl.DeliveryAddressServiceImpl;
public class DeliveryAddressController {
    public Object listDeliveryAddressByUserId(HttpServletRequest request,HttpServletResponse response) throws Exception{
        String userId = request.getParameter("userId");
        DeliveryAddressService service = new DeliveryAddressServiceImpl();
        List<DeliveryAddress> list = service.listDeliveryAddressByUserId(userId);
        return list;
    }
    public Object saveDeliveryAddress(HttpServletRequest request,HttpServletResponse response) throws Exception{
        DeliveryAddress deliveryAddress = new DeliveryAddress();
        deliveryAddress.setContactName(request.getParameter("contactName"));
        deliveryAddress.setContactSex(Integer.valueOf(request.getParameter("contactSex")));
        deliveryAddress.setContactTel(request.getParameter("contactTel"));
        deliveryAddress.setAddress(request.getParameter("address"));
        deliveryAddress.setUserId(request.getParameter("userId"));
        DeliveryAddressService service = new DeliveryAddressServiceImpl();
        int result = service.saveDeliveryAddress(deliveryAddress);
        return result;
    }
    public Object removeDeliveryAddress(HttpServletRequest request,HttpServletResponse response) throws Exception{
        Integer daId = Integer.valueOf(request.getParameter("daId"));
        DeliveryAddressService service = new DeliveryAddressServiceImpl();
        int result = service.removeDeliveryAddress(daId);
        return result;
    }
    public Object getDeliveryAddressById(HttpServletRequest request,HttpServletResponse response) throws Exception{
        Integer daId = Integer.valueOf(request.getParameter("daId"));
        DeliveryAddressService service = new DeliveryAddressServiceImpl();
        DeliveryAddress deliveryAddress = service.getDeliveryAddressById(daId);
        return deliveryAddress;
    }
    public Object updateDeliveryAddress(HttpServletRequest request,HttpServletResponse response) throws Exception{
        DeliveryAddress deliveryAddress = new DeliveryAddress();
        deliveryAddress.setDaId(Integer.valueOf(request.getParameter("daId")));
        deliveryAddress.setContactName(request.getParameter("contactName"));
        deliveryAddress.setContactSex(Integer.valueOf(request.getParameter("contactSex")));
        deliveryAddress.setContactTel(request.getParameter("contactTel"));
        deliveryAddress.setAddress(request.getParameter("address"));
        deliveryAddress.setUserId(request.getParameter("userId"));
        DeliveryAddressService service = new DeliveryAddressServiceImpl();
        int result = service.updateDeliveryAddress(deliveryAddress);
        return result;
    }
}

4.2.3.4.Food

package com.neusoft.elm.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.neusoft.elm.po.Food;
import com.neusoft.elm.service.FoodService;
import com.neusoft.elm.service.impl.FoodServiceImpl;
public class FoodController {
    public Object listFoodByBusinessId(HttpServletRequest request,HttpServletResponse response) throws Exception{
        Integer businessId = Integer.valueOf(request.getParameter("businessId"));
        FoodService service = new FoodServiceImpl();
        List<Food> list = service.listFoodByBusinessId(businessId);
        return list;
    }
}

4.2.3.5.OrderDetailet

package com.neusoft.elm.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.neusoft.elm.po.OrderDetailet;
import com.neusoft.elm.service.OrderDetailetService;
import com.neusoft.elm.service.impl.OrderDetailetServiceImpl;
public class OrderDetailetController {
    public Object listOrderDetailetByOrderId(HttpServletRequest request,HttpServletResponse response) throws Exception{
        Integer orderId = Integer.valueOf(request.getParameter("orderId"));
        OrderDetailetService service = new OrderDetailetServiceImpl();
        List<OrderDetailet> list = service.listOrderDetailetByOrderId(orderId);
        return list;
    }
}

4.2.3.6.Orders

package com.neusoft.elm.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.neusoft.elm.po.Orders;
import com.neusoft.elm.service.OrdersService;
import com.neusoft.elm.service.impl.OrdersServiceImpl;
public class OrdersController{
    //下订单(先清空当前用户购物车中当前商家的所有食品,然后生成订单和订单明细,最后返回订单号)
    public Object createOrders(HttpServletRequest request,HttpServletResponse response) throws Exception{
        String userId = request.getParameter("userId");
        Integer businessId = Integer.valueOf(request.getParameter("businessId"));
        Integer daId = Integer.valueOf(request.getParameter("daId"));
        OrdersService service = new OrdersServiceImpl();
        int orderId = service.createOrders(userId, businessId, daId);
        return orderId;
    }
    public Object getOrdersById(HttpServletRequest request,HttpServletResponse response) throws Exception{
        Integer orderId = Integer.valueOf(request.getParameter("orderId"));
        OrdersService service = new OrdersServiceImpl();
        Orders orders = service.getOrdersById(orderId);
        return orders;
    }
    public Object listOrdersByUserId(HttpServletRequest request,HttpServletResponse response) throws Exception{
        String userId = request.getParameter("userId");
        OrdersService service = new OrdersServiceImpl();
        List<Orders> list = service.listOrdersByUserId(userId);
        return list;
    }
}

4.2.3.7.user

package com.neusoft.elm.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.neusoft.elm.po.User;
import com.neusoft.elm.service.UserService;
import com.neusoft.elm.service.impl.UserServiceImpl;
public class UserController{
    public Object saveUser(HttpServletRequest request,HttpServletResponse response) throws Exception{
        String userId = request.getParameter("userId");
        String password = request.getParameter("password");
        String userName = request.getParameter("userName");
        Integer userSex = Integer.valueOf(request.getParameter("userSex"));
        User user = new User(userId,password,userName,userSex,null);
        UserService service = new UserServiceImpl();
        int result = service.saveUser(user);
        return result;
    }
    public Object getUserByIdByPass(HttpServletRequest request,HttpServletResponse response) throws Exception{
        String userId = request.getParameter("userId");
        String password = request.getParameter("password");
        UserService service = new UserServiceImpl();
        User user = service.getUserByIdByPass(userId, password);
        if(user!=null) {
            request.getSession().setAttribute("user", user);
        }
        return user;
    }
    public Object getUserById(HttpServletRequest request,HttpServletResponse response) throws Exception{
        String userId = request.getParameter("userId");
        UserService service = new UserServiceImpl();
        User user = service.getUserById(userId);
        return user;
    }
}