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;
}
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;
}
看了上面的代码或许能明白什么,上面的代码以可以改进的