JAVA自定义查询策略
此文章为个人笔记,考虑之后工作用到,博客方便于查找,
如果可以给他人提供参考价值,那再好不过
1.定义查询接口参数
package com.qy.code.generator.query; import java.util.ArrayList; import java.util.List; import com.qy.code.generator.constant.OperatorConstants; import com.qy.code.generator.utils.CodeArrayUtils; /** * 描述:查询类 * @author 七脉 */ public class Query { /** * 当前页码 */ private Integer pageNum; /** * 每页条数 */ private Integer pageSize; /** * 查询包含并且 查询条件 */ private List<Condition> andList; /** * 查询包含或查询条件 */ private List<Condition> orList; /** * 查询包含分组查询条件 */ private List<String> groupKeys; /** * 查询包含排序条件项 */ private List<OrderBy> sorts; /** * and条件结合 and(a=1 and b=2 or c=1) */ private List<MultiCondition> andMultiConditionList; /** * or条件结合 or(a=1 and b=2 or c=1) */ private List<MultiCondition> orMultiConditionList; /** * map集合里的and和or两个key值,用于区分( a=1 ..or或and.. c=1) */ public static final String AND_CONDITIONS_KEY = "ands"; public static final String OR_CONDITIONS_KEY = "ors"; private String[] selectColumns; private String table; private List<Join> joinList; /** * 查询列 (表连接查询时,必填) * @param columns * @return */ public Query select(String... selectColumns) { if(null!=selectColumns && selectColumns.length>0) { this.selectColumns = selectColumns; } return this; } /** * 查询表 (表连接查询时,必填) * @param columns * @return */ public Query from(String table) { this.table = table; return this; } /** * 查询列 * @param columns * @return */ public Query join(Join join) { if(null==joinList) { this.joinList = new ArrayList<>(); } if(null!=join) { this.joinList.add(join); } return this; } /** * 私有化构造器 */ public Query() { } /** * 描述:静态构建对象方便方法连续调用 * @author 七脉 * @return */ public static Query create(){ return new Query(); } /** * 等于操作 * @param value * @return */ public Query eq(String key, Object value){ return and(key, OperatorConstants.TYPE_EQ, value); } /** * 不等于操作 * @param value * @return */ public Query neq(String key, Object value){ return and(key, OperatorConstants.TYPE_NEQ, value); } /** * 大于操作 * @param value * @return */ public Query gt(String key, Object value){ return and(key, OperatorConstants.TYPE_GT, value); } /** * 大于等于操作 * @param value * @return */ public Query ge(String key, Object value){ return and(key, OperatorConstants.TYPE_GE, value); } /** * 小于操作 * @param value * @return */ public Query lt(String key, Object value){ return and(key, OperatorConstants.TYPE_LT, value); } /** * 小于等于操作 * @param value * @return */ public Query le(String key, Object value){ return and(key, OperatorConstants.TYPE_LE, value); } /** * 模糊查询操作 * @param value * @return */ public Query like(String key, Object value){ return and(key, OperatorConstants.TYPE_LIKE, value); } /** * 左侧模糊查询操作 * @param value * @return */ public Query leftlike(String key, Object value){ return and(key, OperatorConstants.TYPE_LLIKE, value); } /** * 右侧模糊查询操作 * @param value * @return */ public Query rightlike(String key, Object value){ return and(key, OperatorConstants.TYPE_RLIKE, value); } /** * in()包含操作(之所以不用list,首先list反射去class泛型名字很慢,其次数组比集合要快) * @param array 数组 * @return */ public Query in(String key, Object[] array){ if(null==array || array.length==0){ return this; } return and(key, OperatorConstants.TYPE_IN,array); } /** * not in()不包含操作 * @param array 数组 * @return */ public Query notIn(String key, Object[] array) { if(null==array || array.length==0){ return this; } return and(key, OperatorConstants.TYPE_NOT_IN,array); } /** * 判空操作 * @return */ public Query isNull(String key){ return and(key, OperatorConstants.TYPE_IS_NULL, null); } /** * 判非空操作 * @return */ public Query isNotNull(String key){ return and(key, OperatorConstants.TYPE_IS_NOT_NULL, null); } /** * 判空字符串操作 * @return */ public Query isBlank(String key){ return and(key, OperatorConstants.TYPE_EQ, ""); } /** * 判非空字符串操作 * @return */ public Query isNotBlank(String key){ return and(key, OperatorConstants.TYPE_NEQ, ""); } /** * 描述:增加与条件 * @author 七脉 * @param key * @param operator * @param value * @return */ public Query and(String key, String operator, Object value){ Condition condition = Condition.create(key, operator, value,true); return and(condition); } /** * 等于操作 * @param value * @return */ public Query orEq(String key, Object value){ return or(key, OperatorConstants.TYPE_EQ, value); } /** * 不等于操作 * @param value * @return */ public Query orNeq(String key, Object value){ return or(key, OperatorConstants.TYPE_NEQ, value); } /** * 大于操作 * @param value * @return */ public Query orGt(String key, Object value){ return or(key, OperatorConstants.TYPE_GT, value); } /** * 大于等于操作 * @param value * @return */ public Query orGe(String key, Object value){ return or(key, OperatorConstants.TYPE_GE, value); } /** * 小于操作 * @param value * @return */ public Query orLt(String key, Object value){ return or(key, OperatorConstants.TYPE_LT, value); } /** * 小于等于操作 * @param value * @return */ public Query orLe(String key, Object value){ return or(key, OperatorConstants.TYPE_LE, value); } /** * 模糊查询操作 * @param value * @return */ public Query orLike(String key, Object value){ return or(key, OperatorConstants.TYPE_LIKE, value); } /** * 左侧模糊查询操作 * @param value * @return */ public Query orLeftlike(String key, Object value){ return or(key, OperatorConstants.TYPE_LLIKE, value); } /** * 右侧模糊查询操作 * @param value * @return */ public Query orRightlike(String key, Object value){ return or(key, OperatorConstants.TYPE_RLIKE, value); } /** * in()包含操作(之所以不用list,首先list反射去class泛型名字很慢,其次数组比集合要快) * @param array 数组 * @return */ public Query orIn(String key, Object[] array){ if(null==array || array.length==0){ return this; } return or(key, OperatorConstants.TYPE_IN,array); } /** * not in()不包含操作 * @param array 数组 * @return */ public Query orNotIn(String key, Object[] array) { if(null==array || array.length==0){ return this; } return or(key, OperatorConstants.TYPE_NOT_IN,array); } /** * 判空操作 * @return */ public Query orIsNull(String key){ return or(key, OperatorConstants.TYPE_IS_NULL, null); } /** * 判非空操作 * @return */ public Query orIsNotNull(String key){ return or(key, OperatorConstants.TYPE_IS_NOT_NULL, null); } /** * 判空字符串操作 * @return */ public Query orIsBlank(String key){ return or(key, OperatorConstants.TYPE_EQ, ""); } /** * 判非空字符串操作 * @return */ public Query orIsNotBlank(String key){ return or(key, OperatorConstants.TYPE_NEQ, ""); } /** * and(a=1 and b=2 or c=1)形式查询 * @param condition * @return */ public Query and(MultiCondition multiCondition) { if(null!=multiCondition) { if(null == this.andMultiConditionList) { this.andMultiConditionList = new ArrayList<>(); } this.andMultiConditionList.add(multiCondition); } return this; } /** * and a=1 和 and(a=1 and b=2 or c=1)形式查询 * @param condition * @return */ public Query and(Condition... condition) { if(CodeArrayUtils.isNotEmpty(condition)) { if(condition.length>1){ if(null == this.andMultiConditionList) { this.andMultiConditionList = new ArrayList<>(); } MultiCondition multiCondition = MultiCondition.create().add(condition); this.andMultiConditionList.add(multiCondition); }else{ if(this.andList == null){ this.andList = new ArrayList<Condition>(); } this.andList.add(condition[0]); return this; } } return this; } /** * or(a=1 and b=2 or c=1)形式查询 * @param condition * @return */ public Query or(MultiCondition multiCondition) { if(null!=multiCondition) { if(null == this.orMultiConditionList) { this.orMultiConditionList = new ArrayList<>(); } this.orMultiConditionList.add(multiCondition); } return this; } /** * 增加或条件 or a=1 和 or(a=1 and b=2 or c=1)形式查询 * @param conditions * @return */ public Query or(Condition... condition) { if(CodeArrayUtils.isNotEmpty(condition)) { if(condition.length>1){ if(null == this.orMultiConditionList) { this.orMultiConditionList = new ArrayList<>(); } MultiCondition multiCondition = MultiCondition.create().add(condition); this.orMultiConditionList.add(multiCondition); }else{ if(this.orList == null){ this.orList = new ArrayList<Condition>(); } this.orList.add(condition[0]); return this; } } return this; } /** * 描述:where条件,支持连续调用 * @author 七脉 * @param condition * @return */ public Query where(Condition condition){ if(null != condition){ if(condition.isAnd()){ if(this.andList == null){ this.andList = new ArrayList<Condition>(); } this.andList.add(condition); }else{ if(this.orList == null){ this.orList = new ArrayList<Condition>(); } this.orList.add(condition); } } return this; } /** * 描述:增加或条件 * @author 七脉 * @param key * @param operator * @param value * @return */ public Query or(String key, String operator, Object value){ Condition condition = Condition.create(key, operator, value, false); return or(condition); } /** * 描述:升序排序 * @param key 字段名称 * @return */ public Query asc(String key){ key = getColumn(key); if(null != key){ if(this.sorts == null){ this.sorts = new ArrayList<OrderBy>(); } this.sorts.add(OrderBy.asc(key)); } return this; } /** * 描述:降序排序 * @param key 字段名称 * @return */ public Query desc(String key){ key = getColumn(key); if(null != key){ if(this.sorts == null){ this.sorts = new ArrayList<OrderBy>(); } this.sorts.add(OrderBy.asc(key)); } return this; } /** * 增加分组条件 * @param groups * @return */ public Query group(String... groupKeys){ if(null != groupKeys){ for(String key : groupKeys){ key = getColumn(key); if(this.groupKeys == null){ this.groupKeys = new ArrayList<>(); } this.groupKeys.add(key); } } return this; } public Query limit(Integer pageNum, Integer pageSize) { if(null==pageNum || 0==pageNum) { this.pageNum = 1; }else { this.pageNum = pageNum; } if(null==pageSize || 0==pageSize) { this.pageSize = 1; }else { this.pageSize = pageNum; } return this; } private String getColumn(String key) { if(!key.contains("_")){ String newKey = ""; for(int i=0; i<key.length(); i++){ char ch = key.charAt(i); if(Character.isUpperCase(ch)){ newKey += "_"+ch; }else{ newKey += ch; } } key = newKey; } return key; } public List<Condition> getAndList() { return andList; } public List<Condition> getOrList() { return orList; } public List<String> getGroupKeys() { return groupKeys; } public List<OrderBy> getSorts() { return sorts; } public List<MultiCondition> getAndMultiConditionList() { return andMultiConditionList; } public List<MultiCondition> getOrMultiConditionList() { return orMultiConditionList; } public static String getAndConditionsKey() { return AND_CONDITIONS_KEY; } public static String getOrConditionsKey() { return OR_CONDITIONS_KEY; } public String[] getSelectColumns() { return selectColumns; } public String getTable() { return table; } public List<Join> getJoinList() { return joinList; } public Integer getPageNum() { return pageNum; } public Integer getPageSize() { return pageSize; } }
2.查询条件
package com.qy.code.generator.query; import com.qy.code.generator.constant.OperatorConstants; /** * 描述:查询条件 * @author 七脉 */ public class Condition { /** * 查询键(数据库表字段名称) */ private String key; /** * 操作符 */ private String operator; /** * 查询条件值 */ private Object value; /** * 非 and 即 or */ private boolean isAnd; /** * 构造器私有,使用方法创建 */ private Condition(){} /** * 创建查询条件 * @param key 查询键 * @param operator 查询条件值 * @param value 查询值 */ private Condition(String key, String operator, Object value, boolean isAnd) { this.key = key; this.operator = operator; this.value = value; this.isAnd = isAnd; } /** * 供query类使用 * @param key * @param operator * @param value * @return */ public static Condition create(String key, String operator, Object value, boolean isAnd) { return operate(key, operator, value, isAnd); } /** * 等于操作 * @param value * @return */ public static Condition eq(String key, Object value){ return and(key, OperatorConstants.TYPE_EQ, value); } /** * 不等于操作 * @param value * @return */ public static Condition neq(String key, Object value){ return and(key, OperatorConstants.TYPE_NEQ, value); } /** * 大于操作 * @param value * @return */ public static Condition gt(String key, Object value){ return and(key, OperatorConstants.TYPE_GT, value); } /** * 大于等于操作 * @param value * @return */ public static Condition ge(String key, Object value){ return and(key, OperatorConstants.TYPE_GE, value); } /** * 小于操作 * @param value * @return */ public static Condition lt(String key, Object value){ return and(key, OperatorConstants.TYPE_LT, value); } /** * 小于等于操作 * @param value * @return */ public static Condition le(String key, Object value){ return and(key, OperatorConstants.TYPE_LE, value); } /** * 模糊查询操作 * @param value * @return */ public static Condition like(String key, Object value){ return and(key, OperatorConstants.TYPE_LIKE, value); } /** * 左侧模糊查询操作 * @param value * @return */ public static Condition leftlike(String key, Object value){ return and(key, OperatorConstants.TYPE_LLIKE, value); } /** * 右侧模糊查询操作 * @param value * @return */ public static Condition rightlike(String key, Object value){ return and(key, OperatorConstants.TYPE_RLIKE, value); } /** * in()包含操作(之所以不用list,首先list反射去class泛型名字很慢,其次数组比集合要快) * @param array 数组 * @return */ public static Condition in(String key, Object[] array){ if(null==array || array.length==0){ return null; } return and(key, OperatorConstants.TYPE_IN,array); } /** * not in()不包含操作 * @param array 数组 * @return */ public static Condition notIn(String key, Object[] array) { if(null==array || array.length==0){ return null; } return and(key, OperatorConstants.TYPE_NOT_IN,array); } /** * 判空操作 * @return */ public static Condition isNull(String key){ return and(key, OperatorConstants.TYPE_IS_NULL, null); } /** * 判非空操作 * @return */ public static Condition isNotNull(String key){ return and(key, OperatorConstants.TYPE_IS_NOT_NULL, null); } /** * 判空字符串操作 * @return */ public static Condition isBlank(String key){ return and(key, OperatorConstants.TYPE_EQ, ""); } /** * 判非空字符串操作 * @return */ public static Condition isNotBlank(String key){ return and(key, OperatorConstants.TYPE_NEQ, ""); } /** * 等于操作 * @param value * @return */ public static Condition orEq(String key, Object value){ return or(key, OperatorConstants.TYPE_EQ, value); } /** * 不等于操作 * @param value * @return */ public static Condition orNeq(String key, Object value){ return or(key, OperatorConstants.TYPE_NEQ, value); } /** * 大于操作 * @param value * @return */ public static Condition orGt(String key, Object value){ return or(key, OperatorConstants.TYPE_GT, value); } /** * 大于等于操作 * @param value * @return */ public static Condition orGe(String key, Object value){ return or(key, OperatorConstants.TYPE_GE, value); } /** * 小于操作 * @param value * @return */ public static Condition orLt(String key, Object value){ return or(key, OperatorConstants.TYPE_LT, value); } /** * 小于等于操作 * @param value * @return */ public static Condition orLe(String key, Object value){ return or(key, OperatorConstants.TYPE_LE, value); } /** * 模糊查询操作 * @param value * @return */ public static Condition orLike(String key, Object value){ return or(key, OperatorConstants.TYPE_LIKE, value); } /** * 左侧模糊查询操作 * @param value * @return */ public static Condition orLeftlike(String key, Object value){ return or(key, OperatorConstants.TYPE_LLIKE, value); } /** * 右侧模糊查询操作 * @param value * @return */ public static Condition orRightlike(String key, Object value){ return or(key, OperatorConstants.TYPE_RLIKE, value); } /** * in()包含操作(之所以不用list,首先list反射去class泛型名字很慢,其次数组比集合要快) * @param array 数组 * @return */ public static Condition orIn(String key, Object[] array){ if(null==array || array.length==0){ return null; } return or(key, OperatorConstants.TYPE_IN,array); } /** * not in()不包含操作 * @param array 数组 * @return */ public static Condition orNotIn(String key, Object[] array) { if(null==array || array.length==0){ return null; } return or(key, OperatorConstants.TYPE_NOT_IN,array); } /** * 判空操作 * @return */ public static Condition orIsNull(String key){ return or(key, OperatorConstants.TYPE_IS_NULL, null); } /** * 判非空操作 * @return */ public static Condition orIsNotNull(String key){ return or(key, OperatorConstants.TYPE_IS_NOT_NULL, null); } /** * 判空字符串操作 * @return */ public static Condition orIsBlank(String key){ return or(key, OperatorConstants.TYPE_EQ, ""); } /** * 判不为空字符串操作 * @return */ public static Condition orIsNotBlank(String key){ return or(key, OperatorConstants.TYPE_EQ, ""); } /** * 描述:and条件 * @author 七脉 * @param key * @param operator * @param value * @return */ public static Condition and(String key, String operator, Object value){ return operate(key, operator, value, true); } /** * 描述:or条件 * @author 七脉 * @param key * @param operator * @param value * @return */ public static Condition or(String key, String operator, Object value){ return operate(key, operator, value, false); } /** * 创建连接及查询 条件值 * @param operate 连接符 * @param value 查询条件值 * @return */ private static Condition operate(String key, String operator, Object value, boolean isAnd){ if(null==key || null==operator || null==value || ""==key || ""==operator || ""==value){ return null; } if(!key.contains("_")){ String newKey = ""; for(int i=0; i<key.length(); i++){ char ch = key.charAt(i); if(Character.isUpperCase(ch)){ newKey += "_"+ch; }else{ newKey += ch; } } key = newKey; } Condition condition = new Condition(key, operator, value, isAnd); return condition; } public String getKey() { return key; } public void setKey(String key) { this.key = key; } public String getOperator() { return operator; } public void setOperator(String operator) { this.operator = operator; } public Object getValue() { return value; } public void setValue(Object value) { this.value = value; } public boolean isAnd() { return isAnd; } public void setAnd(boolean isAnd) { this.isAnd = isAnd; } }
3.多条件查询
package com.qy.code.generator.query; import java.util.ArrayList; import java.util.List; import com.qy.code.generator.constant.OperatorConstants; /** * 描述:查询条件 * @author 七脉 */ public class MultiCondition { /** * and条件集合 */ private List<Condition> andList; /** * and条件集合 */ private List<Condition> orList; /** * 构造器私有,使用方法创建 */ private MultiCondition(){} /** * 实例化对象 * @return */ public static MultiCondition create(){ return new MultiCondition(); } /** * 等于操作 * @param value * @return */ public MultiCondition eq(String key, Object value){ return and(key, OperatorConstants.TYPE_EQ, value); } /** * 不等于操作 * @param value * @return */ public MultiCondition neq(String key, Object value){ return and(key, OperatorConstants.TYPE_NEQ, value); } /** * 大于操作 * @param value * @return */ public MultiCondition gt(String key, Object value){ return and(key, OperatorConstants.TYPE_GT, value); } /** * 大于等于操作 * @param value * @return */ public MultiCondition ge(String key, Object value){ return and(key, OperatorConstants.TYPE_GE, value); } /** * 小于操作 * @param value * @return */ public MultiCondition lt(String key, Object value){ return and(key, OperatorConstants.TYPE_LT, value); } /** * 小于等于操作 * @param value * @return */ public MultiCondition le(String key, Object value){ return and(key, OperatorConstants.TYPE_LE, value); } /** * 模糊查询操作 * @param value * @return */ public MultiCondition like(String key, Object value){ return and(key, OperatorConstants.TYPE_LIKE, value); } /** * 左侧模糊查询操作 * @param value * @return */ public MultiCondition leftlike(String key, Object value){ return and(key, OperatorConstants.TYPE_LLIKE, value); } /** * 右侧模糊查询操作 * @param value * @return */ public MultiCondition rightlike(String key, Object value){ return and(key, OperatorConstants.TYPE_RLIKE, value); } /** * in()包含操作(之所以不用list,首先list反射去class泛型名字很慢,其次数组比集合要快) * @param array 数组 * @return */ public MultiCondition in(String key, Object[] array){ if(null==array || array.length==0){ return this; } return and(key, OperatorConstants.TYPE_IN,array); } /** * not in()不包含操作 * @param array 数组 * @return */ public MultiCondition notIn(String key, Object[] array) { if(null==array || array.length==0){ return this; } return and(key, OperatorConstants.TYPE_NOT_IN,array); } /** * 判空操作 * @return */ public MultiCondition isNull(String key){ return and(key, OperatorConstants.TYPE_IS_NULL, null); } /** * 判非空操作 * @return */ public MultiCondition isNotNull(String key){ return and(key, OperatorConstants.TYPE_IS_NOT_NULL, null); } /** * 判空字符串操作 * @return */ public MultiCondition isBlank(String key){ return and(key, OperatorConstants.TYPE_EQ, ""); } /** * 判非空字符串操作 * @return */ public MultiCondition isNotBlank(String key){ return and(key, OperatorConstants.TYPE_NEQ, ""); } /** * 等于操作 * @param value * @return */ public MultiCondition orEq(String key, Object value){ return or(key, OperatorConstants.TYPE_EQ, value); } /** * 不等于操作 * @param value * @return */ public MultiCondition orNeq(String key, Object value){ return or(key, OperatorConstants.TYPE_NEQ, value); } /** * 大于操作 * @param value * @return */ public MultiCondition orGt(String key, Object value){ return or(key, OperatorConstants.TYPE_GT, value); } /** * 大于等于操作 * @param value * @return */ public MultiCondition orGe(String key, Object value){ return or(key, OperatorConstants.TYPE_GE, value); } /** * 小于操作 * @param value * @return */ public MultiCondition orLt(String key, Object value){ return or(key, OperatorConstants.TYPE_LT, value); } /** * 小于等于操作 * @param value * @return */ public MultiCondition orLe(String key, Object value){ return or(key, OperatorConstants.TYPE_LE, value); } /** * 模糊查询操作 * @param value * @return */ public MultiCondition orLike(String key, Object value){ return or(key, OperatorConstants.TYPE_LIKE, value); } /** * 左侧模糊查询操作 * @param value * @return */ public MultiCondition orLeftlike(String key, Object value){ return or(key, OperatorConstants.TYPE_LLIKE, value); } /** * 右侧模糊查询操作 * @param value * @return */ public MultiCondition orRightlike(String key, Object value){ return or(key, OperatorConstants.TYPE_RLIKE, value); } /** * in()包含操作(之所以不用list,首先list反射去class泛型名字很慢,其次数组比集合要快) * @param array 数组 * @return */ public MultiCondition orIn(String key, Object[] array){ if(null==array || array.length==0){ return this; } return or(key, OperatorConstants.TYPE_IN,array); } /** * not in()不包含操作 * @param array 数组 * @return */ public MultiCondition orNotIn(String key, Object[] array) { if(null==array || array.length==0){ return this; } return or(key, OperatorConstants.TYPE_NOT_IN,array); } /** * 判空操作 * @return */ public MultiCondition orIsNull(String key){ return or(key, OperatorConstants.TYPE_IS_NULL, null); } /** * 判非空操作 * @return */ public MultiCondition orIsNotNull(String key){ return or(key, OperatorConstants.TYPE_IS_NOT_NULL, null); } /** * 判空字符串操作 * @return */ public MultiCondition orIsBlank(String key){ return or(key, OperatorConstants.TYPE_EQ, ""); } /** * 判非空字符串操作 * @return */ public MultiCondition orIsNotBlank(String key){ return or(key, OperatorConstants.TYPE_NEQ, ""); } /** * 描述:增加与条件 * @author 七脉 * @param key * @param operator * @param value * @return */ private MultiCondition and(String key, String operator, Object value){ Condition condition = Condition.create(key, operator, value,true); return and(condition); } /** * 描述:增加或条件 * @author 七脉 * @param key * @param operator * @param value * @return */ private MultiCondition or(String key, String operator, Object value){ Condition condition = Condition.create(key, operator, value,false); return or(condition); } /** * 描述:增加与条件 * @author 七脉 * @param key * @param operator * @param value * @return */ public MultiCondition and(Condition condition){ return add(condition); } /** * 描述:增加或条件 * @author 七脉 * @param key * @param operator * @param value * @return */ public MultiCondition or(Condition condition){ return add(condition); } /** * 描述:增加与条件 * @author 七脉 * @param condition * @return */ public MultiCondition add(Condition... condition){ for(Condition cdt : condition){ if(null==condition){ continue; } if(cdt.isAnd()){ if(this.andList == null){ this.andList = new ArrayList<Condition>(); } this.andList.add(cdt); }else{ if(this.orList == null){ this.orList = new ArrayList<Condition>(); } this.orList.add(cdt); } } return this; } public List<Condition> getAndList() { return andList; } public void setAndList(List<Condition> andList) { this.andList = andList; } public List<Condition> getOrList() { return orList; } public void setOrList(List<Condition> orList) { this.orList = orList; } }
4.连接表
package com.qy.code.generator.query; import java.util.ArrayList; import java.util.List; import com.qy.code.generator.enums.JoinType; import com.qy.code.generator.utils.CodeArrayUtils; /** * 连接类型 父类(不可定义为接口,因为重写方法都一样) * @author 七脉 */ public class Join { /** * 左连接表 查询列 */ private String[] selectColumns; /** * 左连接表 */ private String table; /** * on条件 主表列 */ private String onMasterColumn; /** * on条件 左连接表列 */ private String onJoinColumn; /** * where过滤掉件 and a = 1 */ List<Condition> whereAndConditionList; /** * where过滤掉件 or a = 1 */ List<Condition> whereOrConditionList; /** * where过滤掉件 and(a = 1 and b=2 or c=1) */ List<MultiCondition> whereAndMultiConditionList; /** * where过滤掉件 or(a = 1 and b=2 or c=1) */ List<MultiCondition> whereOrMultiConditionList; /** * 连接类型 */ private JoinType joinType; /** * 私有化构造器 */ private Join() { } /** * 描述:静态构建对象实例 * @author 七脉 * @return */ public static Join createInnerJoin(String table){ Join join = new Join(); join.table = table; join.joinType = JoinType.INNER_JOIN; return join; } /** * 描述:静态构建对象实例 * @author 七脉 * @return */ public static Join createLiftJoin(String table){ Join join = new Join(); join.table = table; join.joinType = JoinType.LEFT_JOIN; return join; } /** * 连接表的查询列 * @param joinColumns * @return */ public Join select(String... selectColumns) { this.selectColumns = selectColumns; return this; } /** * 左连接 on 条件字段 * @param onMasterColumn * @param onJoinColumn * @return */ public Join on(String onMasterColumn, String onJoinColumn) { this.onMasterColumn = onMasterColumn; this.onJoinColumn = onJoinColumn; return this; } /** * where 后的 and条件 * @param condition * @return */ public Join and(Condition... conditions) { if(CodeArrayUtils.isNotEmpty(conditions)) { if(this.whereAndConditionList == null) { this.whereAndConditionList = new ArrayList<>(); } for(Condition cdt : conditions) { this.whereAndConditionList.add(cdt); } } return this; } /** * where 后的 or条件 * @param condition * @return */ public Join or(Condition... conditions) { if(CodeArrayUtils.isNotEmpty(conditions)) { if(this.whereOrConditionList == null) { this.whereOrConditionList = new ArrayList<>(); } for(Condition cdt : conditions) { this.whereOrConditionList.add(cdt); } } return this; } /** * where过滤掉件 and(a = 1 and b=2 or c=1) * @param conditions * @return */ public Join and(MultiCondition... conditions) { if(CodeArrayUtils.isNotEmpty(conditions)) { if(this.whereAndMultiConditionList == null) { this.whereAndMultiConditionList = new ArrayList<>(); } for(MultiCondition condition : conditions) { this.whereAndMultiConditionList.add(condition); } } return this; } /** * where过滤掉件 or(a = 1 and b=2 or c=1) * @param conditions * @return */ public Join or(MultiCondition... conditions) { if(CodeArrayUtils.isNotEmpty(conditions)) { if(this.whereOrMultiConditionList == null) { this.whereOrMultiConditionList = new ArrayList<>(); } for(MultiCondition condition : conditions) { this.whereOrMultiConditionList.add(condition); } } return this; } public String[] getSelectColumns() { return selectColumns; } public void setSelectColumns(String[] selectColumns) { this.selectColumns = selectColumns; } public String getTable() { return table; } public void setTable(String table) { this.table = table; } public String getOnMasterColumn() { return onMasterColumn; } public void setOnMasterColumn(String onMasterColumn) { this.onMasterColumn = onMasterColumn; } public String getOnJoinColumn() { return onJoinColumn; } public void setOnJoinColumn(String onJoinColumn) { this.onJoinColumn = onJoinColumn; } public List<Condition> getWhereAndConditionList() { return whereAndConditionList; } public List<Condition> getWhereOrConditionList() { return whereOrConditionList; } public List<MultiCondition> getWhereAndMultiConditionList() { return whereAndMultiConditionList; } public List<MultiCondition> getWhereOrMultiConditionList() { return whereOrMultiConditionList; } public JoinType getJoinType() { return joinType; } }
4.排序类
package com.qy.code.generator.query; /** * 描述:查询排序条件 * @author 七脉 */ public class OrderBy { /** 顺序排序 */ public final static String ASC = "asc"; /** 逆序排序 */ public final static String DESC = "desc"; /** * 排序字段名称 */ private String key; /** * 排序方式 * {@link #ASC} * {@link #DESC} */ private String asc; private OrderBy() { } /** * 创建顺序排序条件项 * @param name * @return */ public static OrderBy asc(String key){ OrderBy sort = new OrderBy(); sort.setKey(key); sort.setAsc(ASC); return sort; } /** * 创建逆序排序条件项 * @param name * @return */ public static OrderBy desc(String key){ OrderBy sort = new OrderBy(); sort.setKey(key); sort.setAsc(DESC); return sort; } public String getKey() { return key; } public void setKey(String key) { this.key = key; } public String getAsc() { return asc; } public void setAsc(String asc) { this.asc = asc; } @Override public String toString() { return getKey() + " " + getAsc(); } }
使用场景:通过Query类定义查询条件,根据最终的Query条件,完成查询需求。
如:使用query生成JdbcTemplate所需SQL
或Mybatis Mapper定义好SQL模板,Query直接作为Mapper接口参数。