if !departIdsisEmpty try String sql = select depart_name from sys_depart where id in + Stringjoin departIds + ; ListString names = jdbcTemplateq
Here is an optimized version of the code:
if (!departIds.isEmpty()) {
try {
String sql = "select depart_name from sys_depart where id in (:departIds)";
Map<String, Object> params = Collections.singletonMap("departIds", departIds);
List<String> names = jdbcTemplate.queryForList(sql, params, String.class);
deviceVO.setDepartName(String.join(",", names));
} catch (Exception e) {
deviceVO.setDepartName("");
}
}
In this optimized version:
- The SQL query is modified to use named parameters instead of concatenating the IDs directly into the query string. This helps prevent SQL injection vulnerabilities.
- The departIds are passed as a parameter map to the
queryForListmethod, which improves code readability and maintainability. - The
setDepartNamemethod is only called if thedepartIdslist is not empty, which avoids unnecessary database queries.
原文地址: http://www.cveoy.top/t/topic/jdro 著作权归作者所有。请勿转载和采集!