/*
* 利用存储过程进行分页
*/
@Override
public Page findCarByProperty(Car car, int curPage) {
log.debug("entering..........findCarByProperty");
Connection conn = null;
Page page = new Page();
List<Car> carList = new ArrayList<Car>();
page.setCurrentPage(curPage);
try {
conn = JdbcUtil.getConnection();
String sql = genSQL(car);
String sql2 = "call car_page.page(?,?,?,?,?)";//或者"call page(?,?,?,?,?)"
CallableStatement cs = conn.prepareCall(sql2);
cs.setInt(1, curPage);
cs.setString(2, sql);
cs.setInt(3,(int)Constants.ITEM_PER_PAGE);
cs.registerOutParameter(4,Types.INTEGER);
cs.registerOutParameter(5,OracleTypes.CURSOR);
cs.execute();
page.setTotalPage(cs.getInt(4));
ResultSet rs = (ResultSet)cs.getObject(5);
while(rs.next()){
Car c = new Car();
c.setCarNumber(rs.getString("carnumber"));
c.setCarType(rs.getString("cartype"));
c.setColor(rs.getString("color"));
c.setPrice(rs.getDouble("price"));
c.setRentPrice(rs.getDouble("rentprice"));
c.setDeposit(rs.getDouble("deposit"));
int carStateId = rs.getInt("ishired");
CarState carState = new CarDaoImpl().findCarStateById(carStateId);
c.setCarState(carState);
c.setIntroduction(rs.getString("introduction"));
carList.add(c);
}
page.setList(carList);
} catch (SQLException e) {
log.error(e);
throw new DataAccessException("global.database.error", e);
} catch (Exception e) {
log.fatal(e);
throw new ApplicationException("global.system.error", e);
} finally {
JdbcUtil.closeConnection(conn);
}
log.debug("leaving..........findCarByProperty");
return page;
}
private String genSQL(Car car) {
StringBuffer sb = new StringBuffer(
"select rownum rn,cc.* from car_cars cc where 1=1");
if (car.getCarNumber() != null && car.getCarNumber().length() > 0) {
sb.append(" and carnumber like '%").append(car.getCarNumber())
.append("%'");
}
if (car.getCarType() != null && car.getCarType().length() > 0) {
sb.append(" and cartype like '%").append(car.getCarType()).append(
"%'");
}
if(car.getColor()!=null&&car.getColor().length()>0){
sb.append(" and color like '%").append(car.getColor()).append("%'");
}
if(car.getPrice()>0){
sb.append(" and price = ").append(car.getPrice());
}
if(car.getRentPrice()>0){
sb.append(" and rentprice = ").append(car.getRentPrice());
}
if(car.getDeposit()>0){
sb.append(" and deposit = ").append(car.getDeposit());
}
if(car.getCarState().getId()>0){
sb.append(" and ishired = ").append(car.getCarState().getId());
}
return sb.toString();
}
发表于 @
2008年10月07日 16:08:00 | | 编辑|
举报| 收藏