JdbcTemplate queryForObject query result set quantity, jdbctemplate result set
1. Organize SQL statements, query parameter arrays, and set the return type
public int countByCondtion(String title, int mediaType, String currentStatus, String provider, String region, Date from, Date to) { List<Object> params = new ArrayList<Object>(); StringBuffer sql = new StringBuffer(); sql.append("select count(1) from checkin_request where 1=1 "); if(StringUtils.isNotEmpty(title)){ sql.append("and title=? "); params.add(title); } if(mediaType !=-1){ sql.append("and mediatype=? "); params.add(mediaType); } if(StringUtils.isNotEmpty(currentStatus)){ sql.append("and current_status=? "); params.add(currentStatus); } if(StringUtils.isNotEmpty(provider)){ sql.append("and provider=? "); params.add(provider); } if(StringUtils.isNotEmpty(region)){ sql.append("and region=? "); params.add(region); } if(from !=null){ sql.append("and createtime>=? "); params.add(from); } if(to !=null){ sql.append("and createtime<? "); params.add(to); } Object[] para = params.toArray(new Object[params.size()]); return this.jdbcTemplate.queryForObject(sql.toString(), para, Integer.class); }
2. Problems:
The preceding code is in the correct format. If the select count statement is incorrectly written as select *, an error occurs in the query.