优化代码private List QryAllStaff(SqlSessionTemplate sqlSession, String selectStatement) { Map<String, Object> resultMap = new HashMap<>(); String selectSql = "select * from iuap_apdoc_basedoc.tb_hr_user a where DATE_FORMAT(NOW(), '%Y-%m-%d') = SUBSTRING(a.HR_TIMESTAMP,1,10) "; resultMap.put("sql", selectSql); List<Map<String, Object>> StaffDateList = sqlSession.selectList(selectStatement, resultMap); selectSql = "select * from iuap_apdoc_basedoc.tb_hr_family a where DATE_FORMAT(NOW(), '%Y-%m-%d') = SUBSTRING(a.HR_TIMESTAMP,1,10)"; resultMap.put("sql", selectSql); List<Map<String, Object>> StaffDateList1 = sqlSession.selectList(selectStatement, resultMap); selectSql = "select * from iuap_apdoc_basedoc.tb_hr_education a where DATE_FORMAT(NOW(), '%Y-%m-%d') = SUBSTRING(a.HR_TIMESTAMP,1,10)"; resultMap.put("sql", selectSql); List<Map<String, Object>> StaffDateList2 = sqlSession.selectList(selectStatement, resultMap); selectSql = "select * from iuap_apdoc_basedoc.tb_hr_employment_record a where DATE_FORMAT(NOW(), '%Y-%m-%d') = SUBSTRING(a.HR_TIMESTAMP,1,10)"; resultMap.put("sql", selectSql); List<Map<String, Object>> StaffDateList3 = sqlSession.selectList(selectStatement, resultMap); StaffDateList.addAll(StaffDateList1); StaffDateList.addAll(StaffDateList2); StaffDateList.addAll(StaffDateList3); return StaffDateList; }
时间: 2024-03-01 10:54:55 浏览: 80
可以将该函数优化为:
```java
private List<Map<String, Object>> QryAllStaff(SqlSessionTemplate sqlSession, String selectStatement) {
Map<String, Object> paramMap = Collections.singletonMap("dateStr", LocalDate.now().toString());
String selectSql = "select * from iuap_apdoc_basedoc.tb_hr_user a where SUBSTRING(a.HR_TIMESTAMP,1,10) = #{dateStr}";
List<Map<String, Object>> staffDataList = sqlSession.selectList(selectStatement, selectSql);
selectSql = "select * from iuap_apdoc_basedoc.tb_hr_family a where SUBSTRING(a.HR_TIMESTAMP,1,10) = #{dateStr}";
staffDataList.addAll(sqlSession.selectList(selectStatement, selectSql));
selectSql = "select * from iuap_apdoc_basedoc.tb_hr_education a where SUBSTRING(a.HR_TIMESTAMP,1,10) = #{dateStr}";
staffDataList.addAll(sqlSession.selectList(selectStatement, selectSql));
selectSql = "select * from iuap_apdoc_basedoc.tb_hr_employment_record a where SUBSTRING(a.HR_TIMESTAMP,1,10) = #{dateStr}";
staffDataList.addAll(sqlSession.selectList(selectStatement, selectSql));
return staffDataList;
}
```
这个函数使用了以下优化:
1. 使用 `LocalDate.now().toString()` 获取当前日期,避免了使用 `DATE_FORMAT(NOW(), '%Y-%m-%d')` 的开销;
2. 将每个查询的 SQL 语句和参数独立出来,避免了重复的代码;
3. 使用 `List.addAll()` 方法将查询的结果合并为一个列表,避免了多个列表合并的开销;
4. 将变量名由 `StaffDateList` 改为 `staffDataList`,符合 Java 命名规范。
阅读全文