技术选型:
在报表查询时,通常需要做可以动态添加的条件
在老项目中使用的是一种Tcondition的对象分装sql
import java.util.HashMap;
public class TCondition {
private String sql;
private Object[] paraValues;
private String orderBy;
private String groupBy;
private String having;
/**
* key为表名,value为 left join on后的内容(如何关联的)
*/
private HashMap leftJoinMap;
/**
* count方法计数时可指定 count哪个字段,默认为count(1)
*/
private String mainId;
public TCondition() {
}
public TCondition(String sql, Object... paraValues) {
this.setSql(sql);
this.setParaValues(paraValues);
}
public static TCondition of() {
return new TCondition();
}
public static TCondition of(String sql, Object... paraValues) {
return new TCondition(sql, paraValues);
}
public String getSql() {
return sql;
}
public TCondition setSql(String sql) {
this.sql = sql;
return this;
}
public Object[] getParaValues() {
return paraValues;
}
public TCondition setParaValues(Object[] paraValues) {
this.paraValues = paraValues;
return this;
}
public String getOrderBy() {
return orderBy;
}
public TCondition setOrderBy(String orderBy) {
this.orderBy = orderBy;
return this;
}
public String getGroupBy() {
return groupBy;
}
public TCondition setGroupBy(String groupBy) {
this.groupBy = groupBy;
return this;
}
public String getHaving() {
return having;
}
public TCondition setHaving(String having) {
this.having = having;
return this;
}
public String getMainId() {
return mainId;
}
public TCondition setMainId(String mainId) {
this.mainId = mainId;
return this;
}
@Override
public String toString() {
StringBuffer result = new StringBuffer(this.getSql() + ":");
for (int i = 0; i < paraValues.length; i++) {
result.append(paraValues[i]).append(",");
}
result.append(" || ");
return result.toString();
}
public HashMap getLeftJoinMap() {
return leftJoinMap;
}
public TCondition setLeftJoinMap(HashMap leftJoinMap) {
this.leftJoinMap = leftJoinMap;
return this;
}
但是由于与老代码的数据库访问层紧紧的耦合在一起,无法在另外的项目中进行复用,因此,在GitHub中找到了一款类似的封装SQL查询的对象
封装的内容:
package ca.krasnay.sqlbuilder;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
* Tool for programmatically constructing SQL select statements. This class aims
* to simplify the task of juggling commas and SQL keywords when building SQL
* statements from scratch, but doesn't attempt to do much beyond that. Here are
* some relatively complex examples:
*
*
* String sql = new SelectBuilder()
* .column("e.id")
* .column("e.name as empname")
* .column("d.name as deptname")
* .column("e.salary")
* .from(("Employee e")
* .join("Department d on e.dept_id = d.id")
* .where("e.salary > 100000")
* .orderBy("e.salary desc")
* .toString();
*
*
*
* String sql = new SelectBuilder()
* .column("d.id")
* .column("d.name")
* .column("sum(e.salary) as total")
* .from("Department d")
* .join("Employee e on e.dept_id = d.id")
* .groupBy("d.id")
* .groupBy("d.name")
* .having("total > 1000000").toString();
*
*
* Note that the methods can be called in any order. This is handy when a base
* class wants to create a simple query but allow subclasses to augment it.
*
* It's similar to the Squiggle SQL library
* (http://code.google.com/p/squiggle-sql/), but makes fewer assumptions about
* the internal structure of the SQL statement, which I think makes for simpler,
* cleaner code. For example, in Squiggle you would write...
*
*
* select.addCriteria(new MatchCriteria(orders, "status", MatchCriteria.EQUALS, "processed"));
*
*
* With SelectBuilder, we assume you know how to write SQL expressions, so
* instead you would write...
*
*
* select.where("status = 'processed'");
*
*
* To include parameters, it's highly recommended to use the
* {@link ParameterizedPreparedStatementCreatorTest}, like this:
*
*
* String sql = new SelectBuilder("Employee e")
* .where("name like :name")
* .toString();
*
* PreparedStatement ps = new ParameterizedPreparedStatementCreator(sql)
* .setParameter("name", "Bob%")
* .createPreparedStatement(conn);
*
*
*
* @author John Krasnay
*/
public class SelectBuilder extends AbstractSqlBuilder implements Cloneable, Serializable {
private static final long serialVersionUID = 1;
private boolean distinct;
private List
github地址:
https://github.com/jkrasnay/sqlbuilder
博客:
http://john.krasnay.ca/2010/02/15/building-sql-in-java.html