javaweb购物商城系统订单代码
时间: 2025-01-03 15:38:39 浏览: 8
### Java Web 购物商城系统订单管理源码实现
#### 订单实体类定义
为了表示订单信息,创建 `Order` 类作为订单的数据模型。
```java
public class Order {
private int orderId;
private String orderNumber;
private double totalAmount;
private Date createTime;
private int userId;
// Getters and Setters
}
```
#### 数据访问层 (DAO)
通过 DAO 层与数据库交互,执行增删改查操作。这里展示如何获取所有订单以及根据 ID 获取特定订单的方法。
```java
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class OrderDao {
public List<Order> getAllOrders() throws Exception {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
String sql = "SELECT * FROM orders";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
ArrayList<Order> list = new ArrayList<>();
while(rs.next()){
Order o = new Order();
o.setOrderId(rs.getInt("order_id"));
o.setOrderNumber(rs.getString("order_number"));
o.setTotalAmount(rs.getDouble("total_amount"));
o.setCreateTime(rs.getTimestamp("create_time"));
o.setUserId(rs.getInt("user_id"));
list.add(o);
}
return list;
} finally {
close(conn,ps,rs);
}
}
public Order getOrderById(int id) throws Exception{
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
String sql = "select * from orders where order_id=?";
ps = conn.prepareStatement(sql);
ps.setInt(1,id);
rs=ps.executeQuery();
if(rs.next()) {
Order o = new Order();
o.setOrderId(id);
o.setOrderNumber(rs.getString("order_number"));
o.setTotalAmount(rs.getDouble("total_amount"));
o.setCreateTime(rs.getDate("create_time"));
o.setUserId(rs.getInt("user_id"));
return o;
}
return null;
}finally {
close(conn,ps,rs);
}
}
private void close(Connection c, PreparedStatement p, ResultSet r){
try {if(r!=null)r.close();} catch(Exception e){e.printStackTrace();}
try {if(p!=null)p.close();} catch(Exception e){e.printStackTrace();}
try {if(c!=null)c.close();} catch(Exception e){e.printStackTrace();}
}
}
```
#### 业务逻辑层 (Service Layer)
服务层负责处理业务逻辑并调用相应的 DAO 方法完成具体功能。
```java
public class OrderService {
private final OrderDao dao = new OrderDao();
/**
* 查询全部订单
*/
public List<Order> findAll(){
try {
return dao.getAllOrders();
}catch (Exception ex){
throw new RuntimeException(ex.getMessage());
}
}
/**
* 根据ID查询单个订单详情
*/
public Order findById(Integer id){
try {
return dao.getOrderById(id);
}catch (Exception ex){
throw new RuntimeException(ex.getMessage());
}
}
}
```
#### 控制器层 (Controller Layer)
控制器接收来自前端页面的请求参数,并返回响应给客户端浏览器。
```java
@WebServlet("/orders")
public class OrderServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException {
String action=request.getParameter("action");
switch(action){
case "list":
showAll(request,response);
break;
case "detail":
detail(request,response);
break;
default:
break;
}
}
private void showAll(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
OrderService service=new OrderService();
List<Order> all=service.findAll();
request.setAttribute("all",all);
request.getRequestDispatcher("/WEB-INF/jsp/order_list.jsp").forward(request,response);
}
private void detail(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
Integer id=Integer.parseInt(request.getParameter("id"));
OrderService service=new OrderService();
Order one=service.findById(id);
request.setAttribute("one",one);
request.getRequestDispatcher("/WEB-INF/jsp/order_detail.jsp").forward(request,response);
}
}
```
阅读全文