我正在使用 spring 3.0,并且正在使用 JqGrid 插件。我正在研究发送带有所有搜索条件的 json 字符串的搜索功能。这是字符串的外观。
{"groupOp":"AND","rules":[{"field":"firstName","op":"bw","data":"John"},{"field":"lastName","op":"cn","data":"Doe"},{"field":"gender","op":"eq","data":"Male"}]}
如果您查看规则数组中的“op”属性,您将看到必须执行的操作。Jq-grid有以下操作
['eq','ne','lt','le','gt','ge','bw','bn','in','ni','ew','en',' cn','nc']
对应于
['等于','不等于','小于','小于等于','大于','大于等于','开头','不开头','在','不是in','以'结尾','不以','包含','不包含']
我计划使用休眠条件搜索来启用搜索功能。为此,我使用 Jackson 的ObjectMapper将传入的 JSON 转换为 Java。这一切都很好。这是我转换json的代码。
public class JsonJqgridSearchModel {
public String groupOp;
public ArrayList<JqgridSearchCriteria> rules;
}
public class JqgridSearchCriteria {
public String field;
public String op;
public String data;
public SimpleExpression getRestriction(){
if(op.equals("cn")){
return Restrictions.like(field, data);
}else if(op.equals("eq")){
return Restrictions.eq(field, data);
}else if(op.equals("ne")){
return Restrictions.ne(field, data);
}else if(op.equals("lt")){
return Restrictions.lt(field, data);
}else if(op.equals("le")){
return Restrictions.le(field, data);
}else if(op.equals("gt")){
return Restrictions.gt(field, data);
}else if(op.equals("ge")){
return Restrictions.ge(field, data);
}else{
return null;
}
}
}
@RequestMapping(value = "studentjsondata", method = RequestMethod.GET)
public @ResponseBody String studentjsondata(@RequestParam("_search") Boolean search ,HttpServletRequest httpServletRequest) {
StringBuilder sb = new StringBuilder();
Format formatter = new SimpleDateFormat("MMMM dd, yyyy");
if(search){
ObjectMapper mapper = new ObjectMapper();
try {
JsonJqgridSearchModel searchModel= mapper.readValue(httpServletRequest.getParameter("filters"), JsonJqgridSearchModel.class);
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
Criteria criteria = session.createCriteria(Person.class);
Iterator<JqgridSearchCriteria> iterator = searchModel.rules.iterator();
while(iterator.hasNext()){
System.out.println("before");
criteria.add(iterator.next().getRestriction());
System.out.println("after");
}
} catch (JsonParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}else{//do other stuff here}
这就是问题所在。如何将 jqGrid 操作转换为等效的休眠命令?例如 “cn”应该对应于
criteria.add(Restrictions.like("firstName", myJsonJqgridSearchModel.data));