SearchConditionDo implements Serializable { protected String buildWhere(String shortName) { StringBuilder sb = new StringBuilder(); for (Entry<String, Object> param : getParams().entrySet()) { Object value = param.getValue(); if (value == null || value.toString().isEmpty() || (value.getClass().isArray() && Array.getLength(value) == 0)) { continue; } if (sb.length() > 0) { sb.append(" AND "); } String[] operatorAndField = param.getKey().split("_"); if (operatorAndField.length != 2) { throw new IllegalArgumentException("Map paramsに不正なキー:" + param.getKey() + "が含まれています。" + "キーは「OperatorCd + \"_\" + endityField」で指定してください。"); } String opeStr = operatorAndField[0].toUpperCase(); String field = operatorAndField[1]; OperatorCd ope = OperatorCd.valueOf(opeStr); sb.append(shortName).append("."); sb.append(field).append(" ").append(ope).append(" "); if (OperatorCd.IN.equals(ope)) { StringBuilder inSb = new StringBuilder(); String[] inValues = (String[]) param.getValue(); int paramSufix = 1; for (String inValue : inValues) { if (inSb.length() > 0) { inSb.append(", "); } String placeHolder = field + "_" + paramSufix++; inSb.append(":").append(placeHolder); queryParams.put(placeHolder, inValue); } sb.append("(").append(inSb).append(")"); } else { sb.append(":"); sb.append(field); queryParams.put(field, value); } } return sb.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 testBuildWhere() { SearchConditionDo condition = new SearchConditionDo(); Map<String, Object> params = new HashMap<>(); params.put("eq_field1", "1"); condition.setParams(params); assertThat("1条件 equals", condition.buildWhere("entity"), is("entity.field1 = :field1")); assertThat("クエリパラメーター", condition.getQueryParams().get("field1"), is("1")); params.put("in_field2", new String[] { "2", "two" }); assertThat("2条件 equals,in", condition.buildWhere("entity"), is("entity.field1 = :field1 AND entity.field2 IN (:field2_1, :field2_2)")); assertThat("クエリパラメーター1", condition.getQueryParams().get("field2_1"), is("2")); assertThat("クエリパラメーター2", condition.getQueryParams().get("field2_2"), is("two")); }