jsp多条件查询问题,请高手指点迷津!
来源:百度知道 编辑:UC知道 时间:2024/05/27 13:23:29
本人帐号就那么多积分了,没办法啊,分少点
一般是用select * from table where 1=1 and ……
1=1是一个条件为真的值 and后面就加上你要查询的条件,如果是多个就就多and几次
比如这是我以前写的一段
String sql="select * from book where 1=1;
if(bookID!=0)
sql=sql+" and bookID="+bookID;
if(!bookName.equals(""))
sql=sql+ " and bookname like '%"+bookName+"%'";
if(!author.equals(""))
sql=sql+" and author like '%"+author+"%'";
if(bookStatus!=2)
sql = sql +" and status="+bookStatus;
封装一个SearchCondition 类,用来传递查询值,要不然写传递的参数,都很复杂。而且不利于维护。
public class SearchCondition{
private String oneCondition;
private String twoCondition;
....
public void setOneCondition(String oneCondition){
this.oneCondition = oneCondition;
}
....
}
在dao查询数据的时候用楼上的方法
String