本人用的是sg-uap虚拟环境
//查询方法
//参数 RequestCondition 配合 controller 的 @QueryRequestParam注解可以将前台传入整个对象进行接收
//参数 UserSession 当前登录的用户信息
//obj 实体类
public QueryResultObject query(RequestCondition queryCondition,UserSession userSession){
QueryCriteria qc = new QueryCriteria;
//查询集合
List<obj> result = null;
//查询记录总数
int count = 0;
//查询条件
qc.addWhere("","数据库字段名","=","值");
//查询表
qc.addFrom(obj.class)
//其他的判断条件
if(queryCondition != null){
//添加其他查询条件
qc = wrapQuery(queryCondition,qc);
//获取总记录数
count = getRecordCount(qc);
//添加分页信息
qc = wrapPage(queryCondition,qc);
result = hibernateDao.findAllByCriteria(qc);
}else{
result = hibernateDao.findAllByCriteria(qc);
count = getRecordCount(qc);
}
return 封装对象返回
}
public QueryCriteria wrapQuery(QueryCriteria qc,RequestCondition queryCondition ){
List<QueryFilter> wheres = queryCondition.getQueryFilter(obj.class);
if(wheres != null && wheres.size() > 0){
//模糊查询
for(int i = 0;i<wheres.size();i++){
QueryFilter q = wheres.get(i);
if("对象属性".equals(q.getFieldName)){
//设置此属性的模糊查询
q.setOperator("^");
}
//排序
qc.addOrder("排序字段","排序规则");
//构造查询where语句
CrudUtils.addQCWhere(qc,Wheres,Obj.class.getName());
}
String orders = queryCondition.getSorter();
//替换分隔符构建排序语句
if(orders != null){
qc.addOrder(Order.replaceAll("&",","))
}
return qc;
}
public QueryCriteria wrapPage(QueryCriteria qc,RequestCondition queryCondition ){
int pageIndex = 1,pageSize = 1;
if(queryCondition.getPageIndex() != null&&queryCondition.getPageSize() != null){
pageIndex=queryCondition.getPageIndex();
pageSize=queryCondition.getPageSize();
qc.addPage(pageIndex,pageSize);
}
return qc;
}
一个简单的模糊+分页查询