JdbcTemplate passed when using the template parameters Map

Recent projects which use the template to JdbcTemplate query:

When the query parameters fixed example the following code:

 @Override
    public boolean checkExistChildren(String stdCategoryId) {
        String sql ="select  count(1) as num from t_std_category t " +
                " left join t_std_category t1  on t.std_category_id = t1.std_category_parent_id " +
                " where t.std_category_id=?";
        Map<String, Object> map = jdbcTemplate.queryForMap(sql,new Object[]{stdCategoryId});
        String count = map.get("num").toString();
        return Integer.parseInt(count) > 0 ? true : false;
    }

Here, when the number of parameters is fixed, a plurality of parameters may be, if the time that the interface advanced query (query uncertain) page appears. In this case, this query with which it occurs in two cases, a Code error, less than 2 query results, but the project requires advanced query interface, the current situation is a multi-table associated with the query, this time, you should consider NamedParameterJdbcTemplate;

  The NamedParameterJdbcTemplate contains a JdbcTemplate, so JdbcTemplate can do NamedParameterJdbcTemplate are competent, NamedParameterJdbcTemplate increased relative to the main JdbcTemplate parameters can be named functions. NamedParameterJdbcTemplate provides named parameters, use: x instead?

if(StringHelper.isNotEmpty(workflowNodeHandler)){
            stringBuilder.append(" and t3.workflow_node_handler=:workflowNodeHandler  ");
            map.put("workflowNodeHandler",workflowNodeHandler);
        }
        if(StringHelper.isNotEmpty(standardNameCn)){
            stringBuilder.append(" and ts.standard_name_cn like:standardNameCn");
            map.put("standardNameCn","%"+standardNameCn+"%");
        }
        if(StringHelper.isNotEmpty(standardNameEn)){
            stringBuilder.append(" and ts.standard_name_en like :standardNameEn");
            map.put("standardNameEn","%"+standardNameEn+"%");
        }if(StringHelper.isNotEmpty(standardState)){
            stringBuilder.append(" and ts.standard_state <=:standardState");
            map.put("standardState",standardState);
        }
        if(StringHelper.isEmptyOrWhiteSpace(standardState)){
            stringBuilder.append(" and  ts.standard_state in ('3','4')");
        }
        stringBuilder.append(" and ts.standard_state>'0'");
        stringBuilder.append(" order by t2.create_time asc ");
        String sql = SqlSupport.generatePagingSql(DatabaseType.MYSQL,stringBuilder.toString(),pageNumber,pageSize);
        int totalElement = namedParameterJdbcTemplate.queryForList(sql,map).size();
        contentInfo.put("totalElement",totalElement);
        contentInfo.put("content",namedParameterJdbcTemplate.queryForList(sql,map));

In this way, no longer worried error or check on the status of the results can not be retrieved, limited personal capacity, have found a good way, the message exchange.

Published 23 original articles · won praise 7 · views 20000 +

Guess you like

Origin blog.csdn.net/ghd602338792/article/details/89469455