动态拼接sql语句工具类

说明:只返回了where后面的语句,没有select,用于单表查询
/**
 * sql查询操作符枚举类
 * */
public enum SqlOpEnum {

    EQUAL("equal"),NOT_EQUAL("notEqual"),
    GE("ge"),GTE("gte"),LE("le"),LTE("lte"),
    IN("in"),NOT_IN("notIn"),
    LIKE("like"),NOT_LIKE("notLike"), LEFT_LIKE("leftLike"),RIGHT_LIKE("rightLike");

    private String name;

    SqlOpEnum(String name){
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public static SqlOpEnum getByOp(String op){
        SqlOpEnum[] values = SqlOpEnum.values();
        for (SqlOpEnum value:values) {
            if( value.getName().equals(op) ){
                return value;
            }
        }
        return null;
    }
}

===================================
import java.io.Serializable;
/**
* sql参数类
*/
public class SqlParam implements Serializable {

    private String key;

    private Object value;

    private SqlOpEnum op;

    public SqlParam(String key,Object value,SqlOpEnum op){
        this.key = key;
        this.value = value;
        this.op = op;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public Object getValue() {
        return value;
    }

    public void setValue(Object value) {
        this.value = value;
    }

    public SqlOpEnum getOp() {
        return op;
    }

    public void setOp(SqlOpEnum op) {
        this.op = op;
    }
}

=================================================
/**
 * @author wangdonghua
 * 动态拼接sql condition工具类
 * */
public class SqlBuilderUtil {

    public static Pair buildCondition(List sqlParamList ){
        if(CollectionUtils.isEmpty(sqlParamList)){
            return new Pair("",null);
        }
        boolean first = true;
        StringBuilder builder = new StringBuilder();
        String key = null;
        Object value = null;
        String op = null;
        List paramList = new ArrayList<>();
        for (SqlParam param:sqlParamList) {
            key = param.getKey();
            value = param.getValue();
            op = param.getOp().getName();
            switch (SqlOpEnum.getByOp(op)){
                case EQUAL:
                    if( first ){
                        builder.append(key).append(" = ? ");
                        first = false;
                    }else{
                        builder.append(" AND ").append(key).append(" = ? ");
                    }
                    paramList.add(value);
                    break;

                case NOT_EQUAL:
                    if( false ){
                        builder.append(key).append(" != ? ");
                        first = false;
                    }else{
                        builder.append(" AND ").append(key).append(" != ? ");
                    }
                    paramList.add(value);
                    break;

                case GE:
                    if( first ){
                        builder.append(key).append(" > ? ");
                        first = false;
                    }else{
                        builder.append(" AND ").append(key).append(" > ? ");
                    }
                    paramList.add(value);
                    break;

                case GTE:
                    if( first ){
                        builder.append(key).append(" >= ? ");
                        first = false;
                    }else{
                        builder.append(" AND ").append(key).append(" >= ? ");
                    }
                    paramList.add(value);
                    break;

                case LE:
                    if( first ){
                        builder.append(key).append(" < ? ");
                        first = false;
                    }else{
                        builder.append(" AND ").append(key).append(" < ? ");
                    }
                    paramList.add(value);
                    break;

                case LTE:
                    if( first ){
                        builder.append(key).append(" <= ? ");
                        first = false;
                    }else{
                        builder.append(" AND ").append(key).append(" <= ? ");
                    }
                    paramList.add(value);
                    break;

                case IN:
                    List list = (List) value;
                    String rangeCondition = buildRangeCondition(list);
                    if( first ){
                        builder.append(key).append(" in (").append(rangeCondition).append(")");
                        first = false;
                    }else{
                        builder.append(" AND ").append(key).append(" in (").append(rangeCondition).append(")");
                    }
                    break;

                case NOT_IN:
                    List list2 = (List) value;
                    String rangeCondition2 = buildRangeCondition(list2);
                    if( first ){
                        builder.append(key).append(" not in (").append(rangeCondition2).append(")");
                        first = false;
                    }else{
                        builder.append(" AND ").append(key).append(" not in (").append(rangeCondition2).append(")");
                    }
                    break;

                case LIKE:
                    if( first ){
                        builder.append(key).append(" like ? ");
                        first = false;
                    }else{
                        builder.append(" AND ").append(key).append(" like ? ");
                    }
                    paramList.add("%" + value + "%");
                    break;

                case NOT_LIKE:
                    if( first ){
                        builder.append(key).append(" not like ? ");
                        first = false;
                    }else{
                        builder.append(" AND ").append(key).append(" not like ? ");
                    }
                    paramList.add("%" + value + "%");
                    break;

                case LEFT_LIKE:
                    if( first ){
                        builder.append(key).append(" like ? ");
                        first = false;
                    }else{
                        builder.append(" AND ").append(key).append(" like ? ");
                    }
                    paramList.add("%" + value);
                    break;

                case RIGHT_LIKE:
                    if( first ){
                        builder.append(key).append(" like ? ");
                        first = false;
                    }else{
                        builder.append(" AND ").append(key).append(" like ? ");
                    }
                    paramList.add(value + "%");
                    break;
            }
        }
        String condition = builder.toString();
        Object[] params = null;
        if( CollectionUtils.isNotEmpty(paramList) ){
            params = paramList.toArray();
        }
        return new Pair<>(condition,params);
    }

    private static String buildRangeCondition(List list){
        if( CollectionUtils.isEmpty(list) ){
            return "";
        }

        boolean first = true;
        StringBuilder builder = new StringBuilder("");
        for (Object obj:list) {
            if( first ){
                if( obj instanceof String ){
                    builder.append("'").append(obj).append("'");
                }else{
                    builder.append(obj);
                }
                first = false;
            }else{
                if( obj instanceof String ){
                    builder.append(",").append("'").append(obj).append("'");
                }else{
                    builder.append(",").append(obj);
                }
            }
        }
        return builder.toString();
    } 
  

 

你可能感兴趣的:(mysql)