When the query result of yyds dry goods count and jdbctemplate is map, it should be handled with caution

Java journey 2022-01-26 11:21:02 阅读数:562

query result yyds dry goods

JdbcTemplate As a common way to interact with the database , It's very convenient to use . But when the query result is map It is required that the query result has and only has one piece of data . Next, let's see how the source code is handled .
#yyds Dry inventory #JdbcTemplate The query result is map Caution is required when _java#yyds Dry inventory #JdbcTemplate The query result is map Caution is required when _java_02#yyds Dry inventory #JdbcTemplate The query result is map Caution is required when _java_03
Focus on the third screenshot , How to handle query results , If the query result is empty , be throw new EmptyResultDataAccessException(1);, If the result is greater than 1 strip , be throw new IncorrectResultSizeDataAccessException(1, results.size());
Let's look at these two exceptions , Find out EmptyResultDataAccessException yes IncorrectResultSizeDataAccessException A subclass of .
#yyds Dry inventory #JdbcTemplate The query result is map Caution is required when _java_04
When this exception is thrown , What we see on the console is the statement printed by the following method .
#yyds Dry inventory #JdbcTemplate The query result is map Caution is required when _ data _05
that , How to be in Dao How to deal with this layer , Because we will be in service Processing transactions in , All will dao The anomaly of the layer is thrown to service, But the query map Null is reasonable , So we usually catch EmptyResultDataAccessException,map For multiple pieces of data, we think it is unreasonable , Usually throw it out , Next, let's look at the code written , In general, we only catch EmptyResultDataAccessException , return null.IncorrectResultSizeDataAccessException We don't catch exceptions , It will be thrown directly and returned to the front-end page after processing .

 public Map<String,Object> queryDetail(String id) throws Exception {
String sql="select * from `v_device_user` where status=1 and data_id = ?";
try {
return jdbcTemplate.queryForMap(sql, new Object[]{id});
}catch (EmptyResultDataAccessException e){
// When data cannot be queried 
e.printStackTrace()
return null;
}catch (IncorrectResultSizeDataAccessException e){
// When more than one piece of data is queried 
e.printStackTrace()
return null;
}
}

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

Generally, we can deal with business requirements by writing like this , In the actual development, it should be handled according to the specific business .

public Map<String,Object> queryDetail(String id) throws Exception {
String sql="select * from `test` where status=1 and data_id = ?";
try {
return jdbcTemplate.queryForMap(sql, new Object[]{id});
}catch (EmptyResultDataAccessException e){
// When data cannot be queried 
e.printStackTrace()
return null;
}
}

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
copyright:author[Java journey],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/01/202201261120594996.html