我无法使用Spring Java的JdbcTemplate在sql中执行该子句的返回值,在这里您可以使用表达式或字符串列表作为参数传递。
我尝试使用Object数组执行分配,如下例所示:
Object []值=新的Object [] {listDepartment};
遵循以下类的示例:
班级模型-员工
public class EmployeeDTO {
private Integer employeeId;
private String firstName;
private String lastName;
private String jobId;
private Integer departamentId;
public EmployeeDTO(){}
public EtiquetaAvulsaDTO(Integer employeeId, String firstName, String lastName, String jobId,
Integer departamentId) {
super();
this.employeeId = employeeId;
this.firstName = firstName;
this.lastName = lastName;
this.jobId = jobId;
this.departmentId = departmentId;
}
public Integer getEmployeeId() {
return employeeId;
}
public void setEmployeeId(Integer employeeId) {
this.employeeId = employeeId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getJobId() {
return jobId;
}
public void setJobId(String jobId) {
this.jobId = jobId;
}
public Integer getDepartmentId() {
return employeeId;
}
public void setDepartmentId(Integer departmentId) {
this.departmentId = departmentId;
}
}
类别:EmployeeService
public class EmployeeService {
@Autowired
private JdbcTemplate jdbcTemplate;
String GET_LIST_EMPLOYEES ="SELECT e.EMPLOYEE_ID AS employeeId, e.FIRST_NAME AS firstName, e.LAST_NAME AS lastName, e.JOB_ID AS jobId, e.DEPARTMENT_ID as departmentId FROM hr.employees e WHERE e.DEPARTMENT_ID IN (:listDepartment)";
/**
* TODO AINDA FALTA IMPLEMENTAÇAO PARA RETORNAR UMA LISTA E O JDBC TEMPLATE TRANSFORMAR ELE COMO CLAUSULA SQL (IN)
* @param listDepartment
* @return
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public List<EmployeeDTO> listEmployees( List<Integer> listDepartment){
Object[] values = new Object[] { listDepartment };
List<EmployeeDTO> result = (List<EmployeeDTO>) this.jdbcTemplate.query(GET_LIST_EMPLOYEES, values,
new RowMapper() {
public Object mapRow(ResultSet rs, int rowNum) throws SQLException{
EmployeeDTO employee = new EmployeeDTO();
employee.setEmployeeId(rs.getInt("employeeId"));
employee.setFirstName(rs.getString("firstName"));
employee.setLastName(rs.getString("lastName"));
employee.setJobId(rs.getString("jobId"));
employee.setDepartmentId(rs.getInt("departmentId"));
return employee;
}
}
);
return result;
}
}
类:EmployeeRestController-方法搜索-通过endPoint返回列表。
@GetMapping(value="/employees/lista/search", produces = "application/json")
public List<EmployeeDTO> listEmployee(@RequestParam(value = "listEmp") List<Integer> listDepartmentId
) {
return employeeService.listEmployees(listDepartmentId);
}
在执行的输出中返回以下错误:
java.sql.SQLException:列类型无效
答案 0 :(得分:0)
我解决了这个问题,在我的情况下,我使用了 NamedParameterJdbcTemplate 类EmployeeService如下:
public class EmployeeService {
@Autowired
private JdbcTemplate jdbcTemplate;
String GET_LIST_EMPLOYEES ="SELECT e.EMPLOYEE_ID AS employeeId, e.FIRST_NAME AS firstName, e.LAST_NAME AS lastName, e.JOB_ID AS jobId, e.DEPARTMENT_ID as departmentId FROM hr.employees e ";
@SuppressWarnings({ "unchecked", "rawtypes" })
public List<EmployeeDTO> listEmployees( List<Integer> listDepartment){
NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(jdbcTemplate.getDataSource());
Map<String,Object> params = new HashMap<String,Object>();
params.put("listDepartment", listDepartment);
List<EmployeeDTO> result = (List<EmployeeDTO>) template.query(GET_LIST_EMPLOYEES + "WHERE e.DEPARTMENT_ID IN (:listDepartment)", params,
new RowMapper() {
public Object mapRow(ResultSet rs, int rowNum) throws SQLException{
EmployeeDTO employee = new EmployeeDTO();
employee.setEmployeeId(rs.getInt("employeeId"));
employee.setFirstName(rs.getString("firstName"));
employee.setLastName(rs.getString("lastName"));
employee.setJobId(rs.getString("jobId"));
employee.setDepartmentId(rs.getInt("departmentId"));
return employee;
}
}
);
return result;
}
}