提供显示购物车商品的Servlet
//先判断该用户是否登陆了
User user = (User) request.getSession().getAttribute("user");
if (user == null) {
request.setAttribute("message", "您还没有登陆呢!");
request.getRequestDispatcher("/message.jsp").forward(request, response);
return;
}
//如果登陆了.....
Cart cart = (Cart) request.getSession().getAttribute("cart");
//把该用户的购物车给JSP页面显示
request.setAttribute("cart", cart);
request.getRequestDispatcher("/client/listCart.jsp").forward(request, response);
显示购物车的JSP页面
<c:if test="${empty(cart.map)}">
您还没有购买过任何商品哦!!!
</c:if>
<table border="1px">
<c:if test="${!empty(cart.map)}">
<h1>您购物车下有如下的商品:</h1><br>
<tr>
<td>书名:</td>
<td>作者:</td>
<td>数量:</td>
<td>价钱:</td>
</tr>
<c:forEach items="${cart.map}" var="cartItme">
<tr>
<td>${cartItme.value.book.name}</td>
<td>${cartItme.value.book.author}</td>
<td>${cartItme.value.quantity}</td>
<td>${cartItme.value.price}</td>
</tr>
</c:forEach>
</c:if>
</table>
效果:
订单模块
在前台用户界面中,当用户要把购物车付款时,应该提供生成订单的超链接....
设计订单实体
订单应该包含id,收货人信息,下单的时间,订单的总价,订单的状态【有无发货】..而不应该包含商品的信息的。商品的信息用一个专门的”订单项“来表示
一个订单对应多个订单项,这是一对多的关系!
private String id;
//下单的时间、日期、状态
private Date date;
private double price;
private boolean state;
//一个用户可以有多个订单,把用户记住
private String user_id;
//一个订单中有多个订单项
private Set<OrderItem> items = new HashSet<>();
//各种的setter和getter
设计订单项实体
private String id;
//一本书对应多个订单项,订单项一定是由书组成,记住书
private String book_id;
private double price;
private int quantity;
//各种的setter和getter
设计数据库表
- 订单表
mysql不能创建名为”order”的表,后边加个s就可以
CREATE TABLE orders (
id VARCHAR(40) PRIMARY KEY,
date DATE NOT NULL,
user_id VARCHAR(40) NOT NULL,
state BOOLEAN,
price DOUBLE,
CONSTRAINT user_id_FK FOREIGN KEY (user_id) REFERENCES user (id)
);
- 订单项表:
CREATE TABLE orderItem (
id VARCHAR(40) PRIMARY KEY,
price DOUBLE,
quantity INT,
order_id VARCHAR(40) ,
book_id VARCHAR(40) ,
CONSTRAINT order_id_FK FOREIGN KEY (order_id) REFERENCES orders (id),
CONSTRAINT book_id_FK FOREIGN KEY (book_id) REFERENCES book (id)
);
- 表之间的结构:
设计Dao
public class OrderDaoImpl implements zhongfucheng.dao.OrderDao {
@Override
public void addOrder(Order order) {
QueryRunner queryRunner = new QueryRunner(Utils2DB.getDataSource());
String sql1 = "INSERT INTO orders(id,ordertime,user_id,state,price) VALUES(?,?,?,?,?)";
try {
//订单的基本信息
queryRunner.update(sql1, new Object[]{order.getId(), order.getOrdertime(), order.getUser_id(), order.isState(), order.getPrice()});
//订单项的信息
String sql2 = "INSERT INTO orderItem(id,price,quantity,order_id,book_id) VALUES(?,?,?,?,?)";
Set<OrderItem> items = order.getItems();
for (OrderItem item : items) {
queryRunner.update(sql2, new Object[]{item.getId(), item.getPrice(), item.getQuantity(), item.getOrder_id(), item.getBook_id()});
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public Order findOrder(String id) {
QueryRunner queryRunner = new QueryRunner(Utils2DB.getDataSource());
Order order;
try {
//找出订单的基本信息
String sql = "SELECT * FROM orders WHERE id=?";
order = (Order) queryRunner.query(sql, new BeanHandler(Order.class), new Object[]{id});
//找出订单的所有订单项
String sql2 = "SELECT * FROM orderItem WHERE order_id=?";
List<OrderItem> list = (List<OrderItem>) queryRunner.query(sql2, new BeanListHandler(OrderItem.class), new Object[]{order.getId()});
System.out.println("这是数据库拿到的list集合:"+list.size());
//将所有订单项装到订单里边
order.getItems().addAll(list);
System.out.println("这是数据库拿到的"+order.getItems().size());
//找出该订单是属于哪一个用户的
String sql3 = "SELECT * FROM orders o,user u WHERE o.user_id=u.id AND o.id=? ";
User user = (User) queryRunner.query(sql3, new BeanHandler(User.class), new Object[]{order.getId()});
order.setUser_id(user.getId());
return order;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
//更新订单的状态
public void updateState(String id) {
QueryRunner queryRunner = new QueryRunner(Utils2DB.getDataSource());
String sql = "UPDATE orders SET state=? WHERE id=?";
try {
queryRunner.update(sql, new Object[]{true, id});
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
//查看已经发货或没发货的订单信息
public List<Order> getAllOrder(boolean state) {
QueryRunner queryRunner = new QueryRunner(Utils2DB.getDataSource());
String sql = "SELECT * FROM orders WHERE state=? ";
try {
return (List<Order>) queryRunner.query(sql, new BeanListHandler(Order.class), new Object[]{state});
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
//通过用户的id查找用户的订单,可能不止一个
public List<Order> findUserOrder(String user_id) {
QueryRunner queryRunner = new QueryRunner(Utils2DB.getDataSource());
String sql = "SELECT * FROM orders WHERE user_id=? ";
try {
return List<Order> queryRunner.query(sql, new BeanHandler(Order.class), new Object[]{user_id});
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
二次更新
在编写dao的时候,尤其是Add方法。它是将所有数据都封装到Order对象上,然后取出数据,把数据插入到数据表中
- 其实,我们的Order和OrderItem的操作可以分开。OrderItem也可以另外编写一个Dao,那么我们在插入完Order对象之后,得到Order对象返回的主键,再调用OrderItemDao的方法来插入OrderItem的数据,这样我觉得会让代码清晰一些。
- 在OrderItemDao中接收的是一个List,因为我们一个订单会对应多个订单项。
抽取成DAO接口
public interface OrderDao {
void addOrder(Order order);
Order findOrder(String id);
List<Order> getAllOrder(boolean state);
void updateState(String user_id);
List<Order> findUserOrder(String user_id);
}
BussinessService
private OrderDao orderDao = DaoFactory.getInstance().createDao("zhongfucheng.dao.impl.OrderDaoImpl", OrderDao.class);
public void createOrder(Cart cart, User user) {
//订单的基本信息
String order_id = WebUtils.makeId();
Order order = new Order();
order.setId(order_id);
order.setPrice(cart.getPrice());
order.setOrdertime(new Date());
order.setState(false);
order.setUser_id(user.getId());
//订单项的基本信息
//得到每个购物项,购物项就作为订单项
for (Map.Entry<String, CartItem> me : cart.getMap().entrySet()) {
OrderItem orderItem = new OrderItem();
CartItem cartItem = me.getValue();
orderItem.setId(WebUtils.makeId());
orderItem.setPrice(cartItem.getPrice());
orderItem.setBook_id(cartItem.getBook().getId());
orderItem.setQuantity(cartItem.getQuantity());
orderItem.setOrder_id(order_id);
order.getItems().add(orderItem);
}
orderDao.addOrder(order);
}
public Order findOrder(String user_id) {
return orderDao.findOrder(user_id);
}
public List<Order> getAllOrder(boolean state) {
return orderDao.getAllOrder(state);
}
public void sendOutOrder(String id) {
orderDao.updateState(id);
}
public List<Order> findUserOrder(String user_id) {
return orderDao.findUserOrder(user_id);
}