以根据查询条件动态查询customer对象为例子
public List<Customer> loadList(Customer customer, Pagination pagination) {
// String hql = "from User u where 1=1 order by userId ";
StringBuffer hql=new StringBuffer("from Customer c where 1=1 ");
try {
//获取Customer的所有属性
Field[] fields = customer.getClass().getDeclaredFields();
for(Field field : fields) {
//每个属性都能获取其值
field.setAccessible(true);
if(field.get(customer) !=null && (field.get(customer) instanceof String)){
// 对于每个属性,获取属性名
String fieldName = field.getName();
hql.append(" and c."+fieldName+" like '%"+field.get(customer)+"%'");
}
}
} catch (Exception e) {
e.printStackTrace();
}
hql.append(" order by c.id desc");
return super.findPage(pagination, hql.toString(), mapParams);
}