用于条件查询数据库。
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class CustomerQueryFormBean {
private String name;
private String cellphone;
private String qq;
private Date startDate;
private Date endDate;
private String infoSource;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCellphone() {
return cellphone;
}
public void setCellphone(String cellphone) {
this.cellphone = cellphone;
}
public String getQq() {
return qq;
}
public void setQq(String qq) {
this.qq = qq;
}
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public Date getEndDate() {
return endDate;
}
public void setEndDate(Date endDate) {
this.endDate = endDate;
}
public String getInfoSource() {
return infoSource;
}
public void setInfoSource(String infoSource) {
this.infoSource = infoSource;
}
public WhereAndParam buildSqlWhere(){
// String where="where 1=1";
StringBuffer sb=new StringBuffer();
List list=new ArrayList();
sb.append(" where 1=1 ");
if(this.name!=null&& !this.name.trim().equals("")){
sb.append("and name like ? ");
list.add("%"+name+"%");
}
if(this.cellphone!=null&& !this.cellphone.trim().equals("")){
sb.append("and cellphone=? ");
list.add(cellphone);
// System.out.println(cellphone+"&&&&&&&&&&&&&&&&&&&&&&&&&");
}
if(this.qq!=null&& !this.qq.trim().equals("")){
sb.append("and qq=? ");
list.add(qq);
}
if(this.startDate!=null){
sb.append("and regTime>=? ");
list.add(startDate);
}
if(this.endDate!=null){
//加上24小时
sb.append("and regTime<=? ");
Calendar calendar=Calendar.getInstance();
calendar.setTime(this.endDate);
calendar.add(Calendar.DATE, 1);
list.add(calendar.getTime());
}
if(this.infoSource!=null&& !this.infoSource.equals("")){
sb.append("and infoSource=? ");
list.add(infoSource);
}
String where=sb.toString();
// System.out.println(where+"\n");
Object params[]=list.toArray();
//使用内部类存储对象
WhereAndParam wp=new WhereAndParam();
wp.setParams(params);
wp.setWhere(where);
return wp;
}
public class WhereAndParam{
private String where;
private Object params[];
public String getWhere() {
return where;
}
public void setWhere(String where) {
this.where = where;
}
public Object[] getParams() {
return params;
}
public void setParams(Object[] params) {
this.params = params;
}
}
}
757

被折叠的 条评论
为什么被折叠?



