/** * Return the disjuction of two expressions * * @param lhs * @param rhs * @return Criterion */ public static CriteriaGroup or(Criteria lhs, Criteria rhs) { try { CriteriaGroup cg = new CriteriaGroup(CriteriaGroup.or); cg.add(lhs).add(rhs); return cg; } catch (CriteriaException e) { log.error(e.getMessage(), e); } return null; }
public static void main(String[] args) { // gender = 'F' and (status=1 or (status=2 and name like '%王')) and type // in (2,3) // Criterion c1 = Expression.eq("gender","F"); // // Criterion c2 = // Expression.and(Expression.eq("status","2"),Expression.like("status","%王")); // // Expression.or(c1,c2); // // Expression.eq("status",1).or(Expression.eq("status",2).and(Expression.like("status","%王"))) // try { // // 对名称以"永远的UNIX"开头的且价格>=70.0元的书进行更新 // Criteria c1 = Exp.like("title", "永远的UNIX%"); // Criteria c2 = Exp.eq("cost", new Double(70)); // Criteria c3 = Exp.and(c1,c2); // // CriteriaGroup cg = new CriteriaGroup(CriteriaGroup.and); // try { // cg.add(c1).add(c2); // } catch (CriteriaException e1) { // e1.printStackTrace(); // } // // System.out.println(c3.toString()); // System.out.println(cg.toString()); // System.out.println(">>"+c3.getValueByLeft("cost")); // System.out.println(">>"+cg.getValueByLeft("cost")); Criteria c5 = Exp.eq("status", 1); Criteria c6 = Exp.eq("status", 2); Criteria c7 = Exp.eq("name", "%王"); Criteria c8 = Exp.eq("to_char(a.createTime,'yyyy-MM-dd')", 2); Criteria c10 = Exp.isNotNull("gender"); CriteriaGroup cg2 = new CriteriaGroup(CriteriaGroup.and); try { cg2.add(c10).add(Exp.or(c5, Exp.and(c6, c7))).add(c8); } catch (CriteriaException e2) { e2.printStackTrace(); } System.out.println(Exp.and(Exp.and(c10, Exp.or(c5, Exp.and(c6, c7))), c8)); System.out.println( Exp.and(Exp.and(c10, Exp.or(c5, Exp.and(c6, c7))), c8).getValueByLeft("status")); System.out.println(Exp.and(Exp.or(c5, Exp.and(c6, c7)), c8)); System.out.println(Exp.and(Exp.or(c5, Exp.and(c6, c7)), c8).getValueByLeft("status")); // // System.out.println(cg2.toSqlString()); // // System.out.println("S"+Exp.and(c6,c7).getValueByLeft("name")); // System.out.println("S"+Exp.and(c5,Exp.and(c6,c7)).getValueByLeft("name")); String a = "~!@#$%^&*()_+<'>?/\''.,"; a = a.replace("\'", "''"); System.out.println(a); } catch (IllegalParamException e) { e.printStackTrace(); } }