java 多条件查询
程序员文章站
2024-03-02 18:44:10
...
public List<Customer> query(Customer criteria) {
try{
//给出sql模板,为了便于后面添加sql语句
StringBuilder sql =new StringBuilder("select * from t_customer where 1=1");
//给出parmas
List<Object> parmas = new ArrayList<Object>();
String cname = criteria.getCname();
if(cname != null && !cname.trim().isEmpty()){
sql.append(" and cname like ?");
parmas.add("%" +cname+ "%");
}
String gender= criteria.getGender();
if(gender != null && !gender.trim().isEmpty()){
sql.append(" and gender=?");
parmas.add(gender);
}
String cellphone= criteria.getCellphone();
if(cellphone != null && !cellphone.trim().isEmpty()){
sql.append(" and cellphone like ?");
parmas.add("%" +cellphone+ "%");
}
String email= criteria.getEmail();
if(email != null && !email.trim().isEmpty()){
sql.append(" and email like ?");
parmas.add("%" +email+ "%");
}
return qr.query(sql.toString(), new BeanListHandler<Customer>(Customer.class),parmas.toArray());
}catch(SQLException e){
throw new RuntimeException(e);
}
}
上一篇: Java中集合和数组的排序方式小结