多条件查询
protected NamedParameterJdbcTemplate jdbcTemplate;
public DataGrid query(QueryCriteria qc) { Class<RivGeneralConfig> clazz = RivGeneralConfig.class; Integer currentPage = null; Integer pageSize = null; List<Object> params = new ArrayList<Object>(); String sql = " select " + SqlUtils.buildProjectionClause(clazz) + " from riv_general_config gc "; String whereClause = ""; if (qc != null) { Map<String, Object> criterias = qc.getCriterias(); if (criterias != null) { String scope = (String) criterias.get("gcScope"); if(scope != null && scope.equalsIgnoreCase("tx")) { whereClause = " where gc.gc_scope = ? or gc.gc_scope = ? or gc.gc_scope = ? or gc.gc_scope = ? "; params.add("tx_in"); params.add("tx_out"); params.add("tx_mat_status_change"); params.add("tx_inventory"); } else { if(scope != null) { whereClause = " where gc.gc_scope = ? "; params.add(scope); } } } } sql = sql + whereClause ; String countSql = " select count(*) from riv_general_config gc "; countSql = countSql + whereClause ; Integer totalCount = jdbcTemplate.getJdbcOperations().queryForObject(countSql, params.toArray(), Integer.class); pageSize = totalCount; currentPage = 1; StringBuilder sb = new StringBuilder(); sb.append(sql); sb.append(" limit " + (currentPage - 1) * pageSize + " , " + pageSize); List<RivGeneralConfig> rows = new ArrayList<RivGeneralConfig>(); rows = jdbcTemplate.getJdbcOperations().query(sb.toString(), params.toArray(), new BeanPropertyRowMapper<RivGeneralConfig>(clazz)); DataGrid dg = new DataGrid(); dg.setCurrentPage(currentPage); dg.setPageSize(pageSize); dg.setRows(rows); dg.setTotal(totalCount.longValue()); return dg; }
import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @SuppressWarnings("rawtypes") public class DataGrid { // total count private Long total = 0L; // result set private Collection rows = new ArrayList(); // current page private Integer currentPage; // page size private Integer pageSize; public Integer getCurrentPage() { return currentPage; } public void setCurrentPage(Integer currentPage) { this.currentPage = currentPage; } public Integer getPageSize() { return pageSize; } public void setPageSize(Integer pageSize) { this.pageSize = pageSize; } public Long getTotal() { return total; } public void setTotal(Long total) { this.total = total; } /** * @return the rows */ public Collection getRows() { return rows; } /** * @param rows the rows to set */ public void setRows(Collection rows) { this.rows = rows; } public static DataGrid constructEmptyDataGrid(PageProxy pageProxy) { DataGrid dg = new DataGrid(); if(pageProxy != null) { dg.setCurrentPage(pageProxy.getCurrentPage() == null ? 10 : pageProxy.getCurrentPage() ); dg.setPageSize(pageProxy.getPageSize() == null ? 1 : pageProxy.getPageSize()); } else { dg.setCurrentPage(10); dg.setPageSize(1); } dg.setTotal(0L); dg.setRows(Collections.EMPTY_LIST); return dg; } }
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class QueryCriteria { /**order proxies */ private List<OrderProxy> orderProxies = new ArrayList<OrderProxy>(); /**page proxy */ private PageProxy pageProxy; /** criterias */ private Map<String, Object> criterias = new HashMap<String, Object>(); public List<OrderProxy> getOrderProxies() { return orderProxies; } public void setOrderProxies(List<OrderProxy> orderProxies) { this.orderProxies = orderProxies; } public PageProxy getPageProxy() { return pageProxy; } public void setPageProxy(PageProxy pageProxy) { this.pageProxy = pageProxy; } public Map<String, Object> getCriterias() { return criterias; } public void setCriterias(Map<String, Object> criterias) { this.criterias = criterias; } }
public class OrderProxy { // ASC ORDER public static final String ASC = "ASC"; // DESC ORDER public static final String DESC = "DESC"; /** order field */ private String orderField; /** order indicator */ private String orderIndicator; public String getOrderField() { return orderField; } public void setOrderField(String orderField) { this.orderField = orderField; } public String getOrderIndicator() { return orderIndicator; } public void setOrderIndicator(String orderIndicator) { this.orderIndicator = orderIndicator; } }
public class PageProxy { /**page size*/ private Integer pageSize; /**current page*/ private Integer currentPage; public Integer getPageSize() { return pageSize; } public void setPageSize(Integer pageSize) { this.pageSize = pageSize; } public Integer getCurrentPage() { return currentPage; } public void setCurrentPage(Integer currentPage) { this.currentPage = currentPage; } }