2023年6月21日发(作者:)
JdbcTemplate中向in语句传参spring jdbc包提供了JdbcTemplate和它的两个兄弟SimpleJdbcTemplate和NamedParameterJdbcTemplate,我们先从JdbcTemplate⼊⼿,引⼊问题,领略⼀下类NamedParameterJdbcTemplate在处理where中包含in时的便利和优雅。⾸先创建实体类Employee:
1 public class Employee {2 private Long id;3 private String name;4 private String dept;5 // omit toString, getters and setters6 }
使⽤JdbcTemplate访问⼀批数据⽐较熟悉的使⽤⽅法如下:public List queryByFundid(int fundId) {
String sql = "select * from employee where id = ?";
Map args = new HashMap<>(); ("id", 32); return orList(sql, args , );
}但是,当查询多个部门,也就是使⽤in的时候,这种⽅法就不好⽤了,只⽀持 这种单数据类型的⼊参。如果⽤List匹配问号,你会发现出现这种的SQL:select * from employee where id in ([1,32])执⾏时⼀定会报错。解决⽅案——直接在Java拼凑⼊参,如:String ids = "3,32";String sql = "select * from employee where id in (" + ids +")";如果⼊参是字符串,要⽤两个''号引起来,这样跟数据库查询⽅式相同。⽰例中的⼊参是int类型,就没有使⽤单引号。但是,推荐使⽤NamedParameterJdbcTemplate类,然后通过: ids⽅式进⾏参数的匹配。使⽤NamedParameterJdbcTemplate访问⼀批数据public List queryByFundid(int fundId) {
String sql = "select * from employee where id in (:ids) and dept = :dept"; Map args = new HashMap<>(); ("dept", "Tech"); List ids = new ArrayList<>(); (3); (32); ("ids", ids); NamedParameterJdbcTemplate givenParamJdbcTemp = new NamedParameterJdbcTemplate(jdbcTemplate); List data = orList(sql, args, ); return data;}如果运⾏以上程序,会采坑,抛出异常:ectResultSetColumnCountException: Incorrect column count:expected 1, actual 3。抛出此异常的原因是⽅法queryForList 不⽀持多列,但是,API()并未说明。请看queryForList 在JdbcTemplate的实现:public List queryForList(String sql, SqlParameterSource paramSource, Class elementType) throws DataAccessException { return query(sql, paramSource, new SingleColumnRowMapper(elementType)); }/** * Create a new {@code SingleColumnRowMapper}. * Consider using the {@link #newInstance} factory method instead, * which allows for specifying the required type once only. * @param requiredType the type that each result object is expected to match */public SingleColumnRowMapper(Class requiredType) { setRequiredType(requiredType);}查询API发现,需要换⼀种思路,代码如下: @Autowired private JdbcTemplate jdbcTemplate; @GetMapping("/queryUsersByIds") @ApiOperation(value = "查询⽤户列表") public List queryUsersByIds() { String sql = "select * from t_user where id in (:ids);"; // id 集合 List ids = new ArrayList<>(); (3); (1); MapSqlParameterSource parameters = new MapSqlParameterSource(); // 传参 ue("ids", ids); NamedParameterJdbcTemplate givenParamJdbcTemp = new NamedParameterJdbcTemplate(jdbcTemplate); List data = (sql, parameters, new RowMapper() { @Override public User mapRow(ResultSet rs, int rowNum) throws SQLException { // TODO Auto-generated method stub User user = new User(); (g("id")); rName(ing("user_name")); ark(ing("remark")); return user; } }); return data; } 更新于2021年6⽉25⽇,基于实现,测试⽤例新写了⼀个。debug模式下的测试结果如下图所⽰:欢迎拍砖。
2023年6月21日发(作者:)
JdbcTemplate中向in语句传参spring jdbc包提供了JdbcTemplate和它的两个兄弟SimpleJdbcTemplate和NamedParameterJdbcTemplate,我们先从JdbcTemplate⼊⼿,引⼊问题,领略⼀下类NamedParameterJdbcTemplate在处理where中包含in时的便利和优雅。⾸先创建实体类Employee:
1 public class Employee {2 private Long id;3 private String name;4 private String dept;5 // omit toString, getters and setters6 }
使⽤JdbcTemplate访问⼀批数据⽐较熟悉的使⽤⽅法如下:public List queryByFundid(int fundId) {
String sql = "select * from employee where id = ?";
Map args = new HashMap<>(); ("id", 32); return orList(sql, args , );
}但是,当查询多个部门,也就是使⽤in的时候,这种⽅法就不好⽤了,只⽀持 这种单数据类型的⼊参。如果⽤List匹配问号,你会发现出现这种的SQL:select * from employee where id in ([1,32])执⾏时⼀定会报错。解决⽅案——直接在Java拼凑⼊参,如:String ids = "3,32";String sql = "select * from employee where id in (" + ids +")";如果⼊参是字符串,要⽤两个''号引起来,这样跟数据库查询⽅式相同。⽰例中的⼊参是int类型,就没有使⽤单引号。但是,推荐使⽤NamedParameterJdbcTemplate类,然后通过: ids⽅式进⾏参数的匹配。使⽤NamedParameterJdbcTemplate访问⼀批数据public List queryByFundid(int fundId) {
String sql = "select * from employee where id in (:ids) and dept = :dept"; Map args = new HashMap<>(); ("dept", "Tech"); List ids = new ArrayList<>(); (3); (32); ("ids", ids); NamedParameterJdbcTemplate givenParamJdbcTemp = new NamedParameterJdbcTemplate(jdbcTemplate); List data = orList(sql, args, ); return data;}如果运⾏以上程序,会采坑,抛出异常:ectResultSetColumnCountException: Incorrect column count:expected 1, actual 3。抛出此异常的原因是⽅法queryForList 不⽀持多列,但是,API()并未说明。请看queryForList 在JdbcTemplate的实现:public List queryForList(String sql, SqlParameterSource paramSource, Class elementType) throws DataAccessException { return query(sql, paramSource, new SingleColumnRowMapper(elementType)); }/** * Create a new {@code SingleColumnRowMapper}. * Consider using the {@link #newInstance} factory method instead, * which allows for specifying the required type once only. * @param requiredType the type that each result object is expected to match */public SingleColumnRowMapper(Class requiredType) { setRequiredType(requiredType);}查询API发现,需要换⼀种思路,代码如下: @Autowired private JdbcTemplate jdbcTemplate; @GetMapping("/queryUsersByIds") @ApiOperation(value = "查询⽤户列表") public List queryUsersByIds() { String sql = "select * from t_user where id in (:ids);"; // id 集合 List ids = new ArrayList<>(); (3); (1); MapSqlParameterSource parameters = new MapSqlParameterSource(); // 传参 ue("ids", ids); NamedParameterJdbcTemplate givenParamJdbcTemp = new NamedParameterJdbcTemplate(jdbcTemplate); List data = (sql, parameters, new RowMapper() { @Override public User mapRow(ResultSet rs, int rowNum) throws SQLException { // TODO Auto-generated method stub User user = new User(); (g("id")); rName(ing("user_name")); ark(ing("remark")); return user; } }); return data; } 更新于2021年6⽉25⽇,基于实现,测试⽤例新写了⼀个。debug模式下的测试结果如下图所⽰:欢迎拍砖。
发布评论