自动生成Sql语句

根据JavaBean和表名,以及一些设置查询字段,插入字段,自动生成Sql语句。

//CRUD操作的父类
public abstract class SqlStatement {
    protected Class<?> clazz = null;
    protected String tableName = null;
    //包含所有sql的模块,如:insert 没有set, 则无需重载createSet()
    private String SQL_TEMPLET = "{0} {1} {2} {3} {4} {5} {6};";
    protected String[] fieldArr = null;
    protected String[] conditionArr = null;
    protected Map<String, String> conditionMap = null;
    private boolean usingMap = false;
    private boolean usingCondition = false;

    //设置插入字段,赋值字段,筛选字段,
    public void setField(String[] fieldArr) {
        if(null == fieldArr || 0 == fieldArr.length) {
            throw new IllegalArgumentException("Field Array 不能为空.");
        }
        this.fieldArr = fieldArr;
    }

    public void setField(List<String> fieldList) {
        setField(fieldList.toArray(new String[0]));
    }

    //设置SQL条件
    public void setCondition(String[] conditionArr) {
        if(usingMap) {
            throw new IllegalStateException("查询条件使用Map.");
        } else if(null == conditionArr || 0 == conditionArr.length) {
            throw new IllegalArgumentException("Condition Array 不能为空.");
        }
        usingCondition = true;
        this.conditionArr = conditionArr;
    }

    public void setCondition(List<String> list) {
        setCondition(list.toArray(new String[0]));
    }

    public void setCondition(Map<String, String> conditionMap) {
        if(null == conditionMap || 0 == conditionMap.size()) {
            throw new IllegalArgumentException("Condition Map 不能为空.");
        }
        usingCondition = true;
        usingMap = true;
        this.conditionMap = conditionMap;
    }

    //重置查询条件容器 使用条件
    public void resetCondition() {
        usingCondition = false;
        usingMap = false;
    }

    //清除参数
    public void clear() {
        resetCondition();
        fieldArr = null;
        conditionArr = null;
        conditionMap = null;
    }

    //DDL,DML操作(CRUD)
    protected abstract String createOperation();

    protected String createSelect() {
        return "";
    }

    protected String createTableName() {
        return tableName;
    }

    protected String createInsert() {
        return "";
    }

    protected String createValue() {
        return "";
    }

    protected String createSet() {
        return "";
    }

    //SQL WHERE查询模块
    protected String createQuery() {
        String where = "";
        //Map会覆盖String[]参数
        if(usingCondition) {
            where += "WHERE ";
            if(usingMap) {
                where += SqlBuilder.getMapKeyOperString(conditionMap);
            } else {
                where += SqlBuilder.getPreFieldString("AND", conditionArr);
            }
        }
        return where;
    }

    //创建相应的SQL代码
    public String createStatement() {
        return MessageFormat.format(SQL_TEMPLET, 
            createOperation(),
            createSelect(),
            createTableName(),
            createInsert(),
            createValue(),
            createSet(),
            createQuery()
        );
    }

    //删除一些多余的空格
    @Override
    public String toString() {
        return createStatement().replaceAll("   ", " ").replaceAll("  ", " ").replaceAll(" ;", ";").replaceAll(" ,", ",");

    }
}

GitHub地址:https://github.com/blogshixiaodong/SqlStatementPool.git

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值