仅适用于v3.2.0+

QueryFilter如何构造复杂的where条件子句?

// 分组用法
DefaultQueryFilter queryFilter = new DefaultQueryFilter();
DefaultFieldLogic fieldLogic = new DefaultFieldLogic(FieldRelation.AND);
fieldLogic.addFilter("NAME_","NAME_1",  "%内置%", QueryOP.LIKE);// 添加一个字段
fieldLogic.addFilter("ALIAS_", "ALIAS_1", "sys", QueryOP.NOT_EQUAL);// 添加一个字段
queryFilter.addGroup(fieldLogic);// 添加一个分组
fieldLogic = new DefaultFieldLogic(FieldRelation.AND);
fieldLogic.addFilter("NAME_", "NAME_2", "%系统%", QueryOP.LIKE);// 添加一个字段
fieldLogic.addFilter("ALIAS_", "ALIAS_2", "system", QueryOP.NOT_EQUAL);// 添加一个字段
queryFilter.addGroup(fieldLogic);// 添加一个分组
// 打印sql
System.out.println("sql ==> " + queryFilter.getFieldLogic().getSql());
System.out.println("param ==> " + queryFilter.getParams());
// 结果:((NAME_ like  #{NAME_1} AND ALIAS_ != #{ALIAS_1}) AND (NAME_ like  #{NAME_2} AND ALIAS_ != #{ALIAS_2}))
// 参数:{NAME_1=%内置%, ALIAS_1=sys, NAME_2=%系统%, ALIAS_2=system}

// ------------------------------------------
// 多值匹配用法
DefaultFieldLogic fieldLogicOr = new DefaultFieldLogic(FieldRelation.OR);
queryFilter = new DefaultQueryFilter();
queryFilter.setFieldLogic(fieldLogicOr);
queryFilter.addFilter("NAME_", "NAME_1", "sys", QueryOP.EQUAL);// 添加一个字段
queryFilter.addFilter("NAME_", "NAME_2", "system", QueryOP.EQUAL);// 添加一个字段
// 打印sql
System.out.println("sql ==> " + queryFilter.getFieldLogic().getSql());
System.out.println("param ==> " + queryFilter.getParams());
// 结果:(NAME_ = #{NAME_1} OR NAME_ = #{NAME_2})
// 参数:{NAME_1=sys, NAME_2=system}

// ------------------------------------------
// 其他用法自行组合
// QueryFilter是外层子句
// FieldLogic是内层子句,可实现多条件复杂子句构造
// 可支持无限组合嵌套
文档更新时间: 2020-07-07 17:27   作者:Eddy