MapSqlParameterSource的使用和DAO层sql语句加列名参数
在这里插入代码片 @Autowired
@Qualifier(value = "namedParameterJdbcTemplate")
private NamedParameterJdbcTemplate namedParameterJdbcTemplate; //用MapSqlParameterSource必须用NamedParameter
private final static RowMapper<ConfPerform> CONF_PERFORM_MAPPER= BeanPropertyRowMapper.newInstance(ConfPerform.class);
@Override
public List<ConfPerform> findByAllPaging(Set<String> brands, String keyword, String property, String start, String limit) {
String SQL_FIND_BY_ALL_PAGING="SELECT * FROM conf_perform WHERE brand IN (:brands) and (template_id like '%'+:keyword+'%' " +
"OR perform_type_desc like '%'+keyword+'%') ORDER BY "+property+" OFFSET :start ROWS FETCH NEXT :limit ROWS ONLY";
// sql是先找包含brand的数据,再模糊查询template_id和perform_type, 最后进行分页,property是列名,start、limit 是从几列开始,显示几行
String userName = TextUtil.underscoreName("userName"); //返回user_name 反向驼峰
MapSqlParameterSource parameters=new MapSqlParameterSource(); //Set<String> & List<String> 数据类型的时候用这个
parameters.addValue("brands",brands);
parameters.addValue("keyword",keyword);
parameters.addValue("start",start);
parameters.addValue("limit",limit);
try {
return namedParameterJdbcTemplate.query(SQL_FIND_BY_ALL_PAGING, parameters, CONF_PERFORM_MAPPER);
} catch (DataAccessException e) {
log.error("Error findByAllPaging:{}",e.getMessage());
return null;
}
}
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
<!--这个包是反向驼峰映射的-->