日期:2014-05-16  浏览次数:20544 次

private QueryRunner qr=new QueryRunner(JdbcUtil.getDataSource());
package com.itheima.tfy.dao.impl;


import java.util.List;


import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;


import com.itheima.tfy.domain.Orders;
import com.itheima.tfy.domain.User;
import com.itheima.tfy.exception.DaoException;
import com.itheima.tfy.util.BookStoreLogger;
import com.itheima.tfy.util.JdbcUtil;


public class OrdersDaoImpl {
private QueryRunner qr=new QueryRunner(JdbcUtil.getDataSource());
public void addOrders(Orders orders,User user){
try{
String sql="insert into orders (id,ordernum,num,totalprice,state,user_id) values(?,?,?,?,?,?)";
Object[]params={orders.getId(),orders.getOrdersNum(),orders.getNum(),orders.getTotalPrice(),orders.isState(),user.getId()};
qr.update(sql, params);

}catch(Exception e){
BookStoreLogger.error("添加订单失败");
throw new DaoException(e);
}
}
public List<Orders> findAllOrders(Orders orders){
try{
String sql="select * from orders ";
return qr.query(sql, new BeanListHandler<Orders>(Orders.class));

}catch(Exception e){
BookStoreLogger.error("查询所有订单失败");
throw new DaoException(e);
}
}
public Orders findMyOrders(User user){
try{
String sql="select * from orders where user_id=?";
return qr.query(sql, new BeanHandler<Orders>(Orders.class),user.getId());

}catch(Exception e){
BookStoreLogger.error("查询我的订单失败");
throw new DaoException(e);
}
}
public List<Orders> findOrdersByUserId(String userId) {
try{
String sql="select * from orders where user_id=?";
return qr.query(sql, new BeanListHandler<Orders>(Orders.class),userId);

}catch(Exception e){
BookStoreLogger.error("查询我的订单失败");
throw new DaoException(e);
}
}
public List<Orders> findOrdersByState(boolean state) {
try{
String sql="select * from orders where state=?";
return qr.query(sql, new BeanListHandler<Orders>