SearchConditionDo implements Serializable { public void buildQuery(String entityName) { StringBuilder search = new StringBuilder(); search.append("SELECT e FROM ").append(entityName).append(" e"); String where = buildWhere("e"); if (where.length() > 0) { search.append(" WHERE ").append(where); } search.append(buildOrderBy("e")); setSearchQuery(search.toString()); StringBuilder count = new StringBuilder(); count.append("SELECT COUNT(e) FROM ").append(entityName).append(" e"); if (where.length() > 0) { count.append(" WHERE ").append(where); } setCountQuery(count.toString()); }  void init(); void buildQuery(String entityName); void setNewSortField(String field); Object addParam(String key, Object value); PaginationDo getPagination(); void setPagination(PaginationDo pagination); Map<String, Object> getParams(); void setParams(Map<String, Object> params); int getSortHistory(); void setSortHistory(int sortHistory); String getCountQuery(); void setCountQuery(String countQuery); String getSearchQuery(); void setSearchQuery(String searchQuery); Map<String, Object> getQueryParams(); void setQueryParams(Map<String, Object> queryParams);  }
@Test public void testBuildQuery() { SearchConditionDo condition = new SearchConditionDo(); condition.buildQuery("Entity"); assertThat("0条件 count", condition.getCountQuery(), is("SELECT COUNT(e) FROM Entity e")); assertThat("0条件 select", condition.getSearchQuery(), is("SELECT e FROM Entity e")); condition.addParam("eq_field1", "1"); condition.buildQuery("Entity"); assertThat("1条件 count", condition.getCountQuery(), is("SELECT COUNT(e) FROM Entity e WHERE e.field1 = :field1")); assertThat("1条件 select", condition.getSearchQuery(), is("SELECT e FROM Entity e WHERE e.field1 = :field1")); }