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 queryForList method, which improves code readability and maintainability.
  • The setDepartName method is only called if the departIds list is not empty, which avoids unnecessary database queries.
if !departIdsisEmpty try String sql = select depart_name from sys_depart where id in + Stringjoin departIds + ; ListString names = jdbcTemplateq

原文地址: http://www.cveoy.top/t/topic/jdro 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录