SQL查询生成Java类库:SqlBuilder

jopen 12年前

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();

 

项目主页:http://www.open-open.com/lib/view/home/1357804661262