Java Code Examples for org.apache.ibatis.jdbc.SQL#WHERE

The following examples show how to use org.apache.ibatis.jdbc.SQL#WHERE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UserSqlProvider.java    From SpringbootMybatis with Apache License 2.0 6 votes vote down vote up
/**
 * This method was generated by MyBatis Generator.
 * This method corresponds to the database table user
 *
 * @mbg.generated
 */
public String updateByPrimaryKeySelective(User record) {
    SQL sql = new SQL();
    sql.UPDATE("user");
    
    if (record.getUsername() != null) {
        sql.SET("username = #{username,jdbcType=VARCHAR}");
    }
    
    if (record.getPsw() != null) {
        sql.SET("psw = #{psw,jdbcType=VARCHAR}");
    }
    
    sql.WHERE("id = #{id,jdbcType=INTEGER}");
    
    return sql.toString();
}
 
Example 2
Source File: CountByExampleProvider.java    From jeesuite-libs with Apache License 2.0 6 votes vote down vote up
public String countByExample(Object example) throws Exception {
	EntityMapper entityMapper = EntityHelper.getEntityMapper(example.getClass());
	Set<ColumnMapper> columns = entityMapper.getColumnsMapper();
	SQL sql = new SQL().SELECT("COUNT(1)").FROM(entityMapper.getTableMapper().getName());
	Object value;
	StringBuilder whereBuilder = new StringBuilder();
	for (ColumnMapper column : columns) {
		value = EntityHelper.getEntityField(column.getProperty()).get(example);
		if(value == null)continue;
		appendWhere(whereBuilder,column,value);
	}
	if(whereBuilder.length() == 0)throw new IllegalArgumentException("至少包含一个查询条件");
	
	sql.WHERE(whereBuilder.toString());
	return sql.toString();
}
 
Example 3
Source File: SelectByExampleProvider.java    From jeesuite-libs with Apache License 2.0 6 votes vote down vote up
public String selectByExample(Object example) throws Exception {
		EntityMapper entityMapper = EntityHelper.getEntityMapper(example.getClass());
		Set<ColumnMapper> columns = entityMapper.getColumnsMapper();
		SQL sql = new SQL().SELECT("*").FROM(entityMapper.getTableMapper().getName());
		Object value;
		StringBuilder whereBuilder = new StringBuilder();
		for (ColumnMapper column : columns) {
			value = EntityHelper.getEntityField(column.getProperty()).get(example);
			if(value == null)continue;
			appendWhere(whereBuilder,column,value);
		}
		if(whereBuilder.length() == 0)throw new IllegalArgumentException("至少包含一个查询条件");
		//
//		if(DbType.MYSQL.name().equalsIgnoreCase(MybatisConfigs.getDbType("default"))){
//			whereBuilder.append(" LIMIT 20000");
//		}
		sql.WHERE(whereBuilder.toString());
		return sql.toString();
	}
 
Example 4
Source File: OracleTool.java    From maintain with MIT License 5 votes vote down vote up
public static void where(SQL sql, String column, String value, String operator) {
	if (!StringUtils.isEmpty(value)) {
		if (">=".equals(operator) || ">".equals(operator)) {
			sql.WHERE(column + " " + operator + " to_date('" + value + "', 'yyyy-MM-dd')");
		} else if ("=".equals(operator)) {
			sql.WHERE(column + " >= to_date('" + value + "', 'yyyy-MM-dd')");
			sql.WHERE(column + " <= to_date('" + value + " 23:59:59', 'yyyy-MM-dd hh24:mi:ss')");
		} else {
			sql.WHERE(column + " " + operator + " to_date('" + value + " 23:59:59', 'yyyy-MM-dd hh24:mi:ss')");
		}
	}
}
 
Example 5
Source File: BrowseNodeDAOQueryBuilder.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
public String findChildrenQuery(
    @Param("path") final String path,
    @Param("maxNodes") final int maxNodes,
    @Param("contentSelectors") final List<String> contentSelectors,
    final ProviderContext providerContext)
{
  final String tableName = getFormatName(providerContext) + "_browse_node B";

  SQL sql = new SQL()
      .SELECT("B.*, L.parent_id IS NULL as leaf")
      .FROM(tableName)
      .LEFT_OUTER_JOIN("(SELECT DISTINCT parent_id FROM " + tableName + ") L ON B.browse_node_id = L.parent_id");

  if (path != null) {
    String innerTable = new SQL()
        .SELECT_DISTINCT("browse_node_id")
        .FROM(tableName)
        .WHERE("repository_id = #{repository.repositoryId}")
        .WHERE("path = #{path}")
        .toString();

    sql.WHERE("B.parent_id = (" + innerTable + ")");
  }
  else {
    sql.WHERE(" B.parent_id IS NULL")
       .WHERE(" B.repository_id = #{repository.repositoryId}");
  }

  if (contentSelectors != null && !contentSelectors.isEmpty()) {
    sql.AND();
    sql.WHERE(contentSelectors.stream().collect(joining(") or (", "(", ")")));
  }

  sql.LIMIT(maxNodes);

  return sql.toString();
}
 
Example 6
Source File: BaseSQLProvider.java    From QuickProject with Apache License 2.0 5 votes vote down vote up
private SQL WHERE(SQL sql, T findParams, String operator) {
    if (findParams == null) {
        return sql;
    }

    Map<String, Property> properties = ModelUtils.getProperties(findParams, ColumnTarget.WHERE);

    for (Property property : properties.values()) {
        if (operator.equalsIgnoreCase(OPERATOR_LIKE)) {
        }
        sql.WHERE(property.getColumnName() + operator + "#{findParams." + property.getName() + "}");
    }
    return sql;
}
 
Example 7
Source File: BaseSQLProvider.java    From QuickProject with Apache License 2.0 5 votes vote down vote up
public static SQL WHERE_CUSTOM(SQL sql, Class modelClass, Map<String, Object> dataMap, List<CustomQueryParam> customQueryParams, String tableAlias) {
        Map<String, Property> properties = ModelUtils.getProperties(modelClass, null);

//        int i = 0;
        for (CustomQueryParam customQueryParam : customQueryParams) {
            String key = customQueryParam.getProperty();
            Property property = properties.get(key);
            if (property == null) {
                continue;
            }
            String condition = "";
            if (StringUtils.isNotEmpty(tableAlias)) {
                condition = tableAlias + "." + condition;
            }
            if (customQueryParam instanceof WithValueQueryParam) {
                WithValueQueryParam withValueQueryParam = (WithValueQueryParam) customQueryParam;
                dataMap.put(dataMap.size() + "", withValueQueryParam.getValue());
                // 不能以property为key放入dataMap,因为可能有相同的property,如> & <
                condition = condition + property.getColumnName() + " " + withValueQueryParam.getOperator() + " #{" + (dataMap.size() - 1) + "}";
//                i++;
            } else if (customQueryParam instanceof NoValueQueryParam) {
                NoValueQueryParam noValueQueryParam = (NoValueQueryParam) customQueryParam;
                condition = condition + property.getColumnName() + " " + noValueQueryParam.getCondition();
            }
            sql.WHERE(condition);
        }
        return sql;
    }
 
Example 8
Source File: UserSqlProvider.java    From SpringbootMybatis with Apache License 2.0 4 votes vote down vote up
/**
 * This method was generated by MyBatis Generator.
 * This method corresponds to the database table user
 *
 * @mbg.generated
 */
protected void applyWhere(SQL sql, UserCriteria example, boolean includeExamplePhrase) {
    if (example == null) {
        return;
    }
    
    String parmPhrase1;
    String parmPhrase1_th;
    String parmPhrase2;
    String parmPhrase2_th;
    String parmPhrase3;
    String parmPhrase3_th;
    if (includeExamplePhrase) {
        parmPhrase1 = "%s #{example.oredCriteria[%d].allCriteria[%d].value}";
        parmPhrase1_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
        parmPhrase2 = "%s #{example.oredCriteria[%d].allCriteria[%d].value} and #{example.oredCriteria[%d].criteria[%d].secondValue}";
        parmPhrase2_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{example.oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
        parmPhrase3 = "#{example.oredCriteria[%d].allCriteria[%d].value[%d]}";
        parmPhrase3_th = "#{example.oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
    } else {
        parmPhrase1 = "%s #{oredCriteria[%d].allCriteria[%d].value}";
        parmPhrase1_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
        parmPhrase2 = "%s #{oredCriteria[%d].allCriteria[%d].value} and #{oredCriteria[%d].criteria[%d].secondValue}";
        parmPhrase2_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
        parmPhrase3 = "#{oredCriteria[%d].allCriteria[%d].value[%d]}";
        parmPhrase3_th = "#{oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
    }
    
    StringBuilder sb = new StringBuilder();
    List<Criteria> oredCriteria = example.getOredCriteria();
    boolean firstCriteria = true;
    for (int i = 0; i < oredCriteria.size(); i++) {
        Criteria criteria = oredCriteria.get(i);
        if (criteria.isValid()) {
            if (firstCriteria) {
                firstCriteria = false;
            } else {
                sb.append(" or ");
            }
            
            sb.append('(');
            List<Criterion> criterions = criteria.getAllCriteria();
            boolean firstCriterion = true;
            for (int j = 0; j < criterions.size(); j++) {
                Criterion criterion = criterions.get(j);
                if (firstCriterion) {
                    firstCriterion = false;
                } else {
                    sb.append(" and ");
                }
                
                if (criterion.isNoValue()) {
                    sb.append(criterion.getCondition());
                } else if (criterion.isSingleValue()) {
                    if (criterion.getTypeHandler() == null) {
                        sb.append(String.format(parmPhrase1, criterion.getCondition(), i, j));
                    } else {
                        sb.append(String.format(parmPhrase1_th, criterion.getCondition(), i, j,criterion.getTypeHandler()));
                    }
                } else if (criterion.isBetweenValue()) {
                    if (criterion.getTypeHandler() == null) {
                        sb.append(String.format(parmPhrase2, criterion.getCondition(), i, j, i, j));
                    } else {
                        sb.append(String.format(parmPhrase2_th, criterion.getCondition(), i, j, criterion.getTypeHandler(), i, j, criterion.getTypeHandler()));
                    }
                } else if (criterion.isListValue()) {
                    sb.append(criterion.getCondition());
                    sb.append(" (");
                    List<?> listItems = (List<?>) criterion.getValue();
                    boolean comma = false;
                    for (int k = 0; k < listItems.size(); k++) {
                        if (comma) {
                            sb.append(", ");
                        } else {
                            comma = true;
                        }
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase3, i, j, k));
                        } else {
                            sb.append(String.format(parmPhrase3_th, i, j, k, criterion.getTypeHandler()));
                        }
                    }
                    sb.append(')');
                }
            }
            sb.append(')');
        }
    }
    
    if (sb.length() > 0) {
        sql.WHERE(sb.toString());
    }
}
 
Example 9
Source File: OracleTool.java    From maintain with MIT License 4 votes vote down vote up
public static void where(SQL sql, String column, String value, String operator, boolean isVague) {
	if (!StringUtils.isEmpty(value)) {
		sql.WHERE(column + (isVague ? " like " : " " + operator + " ") + toString(value, isVague));
	}
}
 
Example 10
Source File: OracleTool.java    From maintain with MIT License 4 votes vote down vote up
public static void where(SQL sql, String column, Double value) {
	if (null != value) {
		sql.WHERE(column + " = " + value);
	}
}
 
Example 11
Source File: OracleTool.java    From maintain with MIT License 4 votes vote down vote up
public static void where(SQL sql, String column, Integer value) {
	if (null != value) {
		sql.WHERE(column + " = " + value);
	}
}
 
Example 12
Source File: OracleTool.java    From maintain with MIT License 4 votes vote down vote up
public static void where(SQL sql, String column, Long value) {
	if (null != value) {
		sql.WHERE(column + " = " + value);
	}
}