JDBC DAO SQL 不区分大小写的查询
JDBC DAO SQL case-insensitive query
IDE:IntelliJ
开发工具包:JAVA 11.0
SQL 客户端:postgreSQL
我的 DAO 包含以下 @Override,我 运行 遇到了一个不区分大小写的问题。尽我所能,我无法弄清楚如何使 postgreSQL 搜索不区分大小写。我的测试条件也在最下面。有什么建议吗?
@Override
public List<Employee> searchEmployeesByName(String firstNameSearch, String lastNameSearch) {
List<Employee> employees = new ArrayList<>();
String sql = "SELECT employee_id, department_id, first_name, last_name, birth_date, hire_date " +
"FROM employee " +
"WHERE first_name LIKE ? AND last_name LIKE ?;";
firstNameSearch = "%" + firstNameSearch + "%";
lastNameSearch = "%" + lastNameSearch + "%";
SqlRowSet results = jdbcTemplate.queryForRowSet(sql, firstNameSearch, lastNameSearch);
while (results.next()) {
employees.add(mapRowToEmployee(results));
}
return employees;
}
@Test
public void employee_added_to_project_is_in_list_of_employees_for_project() {
sut.addEmployeeToProject(1L, 3L);
List<Employee> employees = sut.getEmployeesByProjectId(1L);
Assert.assertEquals("addEmployeeToProject didn't increase number of employees assigned to project",
2, employees.size());
assertEmployeesMatch("addEmployeeToProject assigned wrong employee to project", EMPLOYEE_3, employees.get(1));
}
如果您希望与 PostgreSQL 进行不区分大小写的匹配,请使用 ILIKE
instead of LIKE
:
The key word ILIKE can be used instead of LIKE to make the match case-insensitive according to the active locale. This is not in the SQL standard but is a PostgreSQL extension.
IDE:IntelliJ 开发工具包:JAVA 11.0 SQL 客户端:postgreSQL
我的 DAO 包含以下 @Override,我 运行 遇到了一个不区分大小写的问题。尽我所能,我无法弄清楚如何使 postgreSQL 搜索不区分大小写。我的测试条件也在最下面。有什么建议吗?
@Override
public List<Employee> searchEmployeesByName(String firstNameSearch, String lastNameSearch) {
List<Employee> employees = new ArrayList<>();
String sql = "SELECT employee_id, department_id, first_name, last_name, birth_date, hire_date " +
"FROM employee " +
"WHERE first_name LIKE ? AND last_name LIKE ?;";
firstNameSearch = "%" + firstNameSearch + "%";
lastNameSearch = "%" + lastNameSearch + "%";
SqlRowSet results = jdbcTemplate.queryForRowSet(sql, firstNameSearch, lastNameSearch);
while (results.next()) {
employees.add(mapRowToEmployee(results));
}
return employees;
}
@Test
public void employee_added_to_project_is_in_list_of_employees_for_project() {
sut.addEmployeeToProject(1L, 3L);
List<Employee> employees = sut.getEmployeesByProjectId(1L);
Assert.assertEquals("addEmployeeToProject didn't increase number of employees assigned to project",
2, employees.size());
assertEmployeesMatch("addEmployeeToProject assigned wrong employee to project", EMPLOYEE_3, employees.get(1));
}
如果您希望与 PostgreSQL 进行不区分大小写的匹配,请使用 ILIKE
instead of LIKE
:
The key word ILIKE can be used instead of LIKE to make the match case-insensitive according to the active locale. This is not in the SQL standard but is a PostgreSQL extension.