SqlBuilder 是一个Java的类库,它试图帮你避免在Java程序内直接书写SQL查询的痛苦。你只需要使用 SqlBuilder 的方法,它就可以帮你生成对应的 SQL 数据库查询语句,例如下面一个SQL语句:
String selectQuery = "SELECT " + T1_COL1 + "," + T1_COL2 + "," + T2_COL1 + " FROM " + TABLE1 + " " + T1 + " INNER JOIN " + TABLE2 + " " + T2 + " ON (" + T1_IDCOL + " = " + T2_IDCOL + ") ORDER BY " + T1_COL1;
对应的 SqlBuilder 的代码是:
// assuming these objects have already been created Table table1, table2; Column t1Col1, t1Col2, t2Col1; Join joinOfT1AndT2; String selectQuery = (new SelectQuery()) .addColumns(t1Col1, t1Col2, t2Col1) .addJoin(SelectQuery.JoinType.INNER_JOIN, joinOfT1AndT2) .addOrderings(t1Col1) .validate().toString();
更多的示例代码请看这里。
编者:这样的做法使得程序难以阅读,不直观。