spring+hibernate中的DAOImp的综合查询实现

 
public List findAllBook(Book b,PageBean p) throws Exception {
  String sql = "select b,bt,u.userName,d.deptName "
    + " from Book b,BookType bt,UserBean u,DeptBean d"
    + " where b.typeId=bt.id and b.userId=u.OID and u.deptOID=d.OID ";
  int i = 1;
  if (!b.getQueryType().equals("")) {
   i++;
  }
  if (!b.getCode().equals("")) {
   i++;
  }
  if (!b.getName().equals("")) {
   i++;
  }
  if (b.getTypeId().intValue() > 0) {
   i++;
  }
  if (!b.getBookSelf().equals("")) {
   i++;
  }
  String str[] = new String[i];
  Object ob2[] = new Object[i];
  i = 0;
  String sql2 = "select count(*) from Book b where b.status in('1','0')";
  if (b.getQueryType().equals("status")) {
   b.setStatus("0");
   sql += " and b.status=:status";
   sql2 += " and b.status=:status";
   str[i] = "status";
   ob2[i] = b.getStatus();
   i++;
  } else if (b.getQueryType().equals("losed")) {
   b.setIsLosed("0");
   sql += " and b.isLosed=:isLosed";
   sql2 += " and b.isLosed=:isLosed";
   str[i] = "isLosed";
   ob2[i] = b.getIsLosed();
   i++;
  } else if (b.getQueryType().equals("borrowed")) {
   b.setIsBorrowed("0");
   sql += " and b.isBorrowed=:isBorrowed";
   sql2 += " and b.isBorrowed=:isBorrowed";
   str[i] = "isBorrowed";
   ob2[i] = b.getIsBorrowed();
   i++;
  } else {
   b.setStatus("1");
   sql += " and b.status=:status";
   sql2 += " and b.status=:status";
   str[i] = "status";
   ob2[i] = b.getStatus();
   i++;
  }
  if (!b.getCode().equals("")) {
   sql += " and b.code=:code";
   sql2 += " and b.code=:code";
   str[i] = "code";
   ob2[i] = b.getCode();
   i++;
  }
  if (!b.getName().equals("")) {
   sql += " and b.name=:name";
   sql2 += " and b.name=:name";
   str[i] = "name";
   ob2[i] = b.getName();
   i++;
  }
  if (b.getTypeId().intValue() > 0) {
   sql += " and b.typeId=:typeId";
   sql2 += " and b.typeId=:typeId";
   str[i] = "typeId";
   ob2[i] = b.getTypeId();
   i++;
  }
  if (!b.getBookSelf().equals("")) {
   sql += " and b.bookSelf=:bookSelf";
   sql2 += " and b.bookSelf=:bookSelf";
   str[i] = "bookSelf";
   ob2[i] = b.getBookSelf();
  }
  
  p.setListSQL(sql);
  p.setTotalCountSQL(sql2);
  p.setCount(this.getPaginate().getTotalCount(p,str,ob2));
  this.setPageBean(p);
  List list = this.getPaginate().getList(p, str, ob2);
  Iterator it = list.iterator();
  List books = new ArrayList();
  while (it.hasNext()) {
   Object o[] = (Object[]) it.next();
   Book book = null;
   book = (Book) o[0];
   BookType bt = (BookType) o[1];
   UserBean u = new UserBean();
   u.setUserName((String) o[2]);
   DeptBean d = new DeptBean();
   d.setDeptName((String) o[3]);
   u.setDept(d);
   book.setBookType(bt);
   if (book.getStatus().equals("0")) {
    book.setStatusName("<font color='red'>
注销 </font>");
   } else {
    book.setStatusName("
正常 ");
   }
   if (book.getIsBorrowed().equals("0")) {
    book.setIsBorrowedName("<font color='red'>
借出 </font>");
   } else {
    book.setIsBorrowedName("
未借 ");
   }
   if (book.getIsLosed().equals("0")) {
    book.setIsLosedName("<font color='red'>
丢失 </font>");
   } else {
    book.setIsLosedName("
正常 ");
   }
   book.setUser(u);
   books.add(book);
  }
  return books;
 }
看了上面的代码或许能明白什么,上面的代码以可以改进的
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值