这里我想从列表中设置2列值,这样我就可以一次更新多行,但是它没有用。
String hql = "UPDATE empDTO set empstatus =(:status) , empreason =
(:reason) WHERE enpID=(:id) and createdOn =:CreatedOn ";
Query query = session.createQuery(hql);
query.setParameterList("status", status);//status is list
query.setParameterList("reason", reason);//reason is list
query.setParameterList("id", id);//id is list
query.setParameter("CreatedOn",new Date());
query.executeUpdate();
rowAffected = query.executeUpdate();
org.hibernate.exception.DataException:无法执行更新查询 在org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:102) 在org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
引起:java.sql.SQLException:操作数应包含1列
答案 0 :(得分:0)
我建议你BatchPreparedStatementSetter来做
您创建DTO以更新employeeUpdateDTO列表
public class EmployeeUpdateDTO {
private Integer empId;
private String reason;
private String status;
public Integer getEmpId() {
return empId;
}
public void setEmpId(Integer empId) {
this.empId = empId;
}
public String getReason() {
return reason;
}
public void setReason(String reason) {
this.reason = reason;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
将此添加到您的实施中;
public void updateEmployee(List<EmployeeUpdateDTO> employeeUpdateDTOs) {
String updateQuery = "UPDATE empDTO set empstatus =?,empreason =? WHERE enpID=? and createdOn =?";
try {
int[] updateCounts = jdbcTemplate.batchUpdate(updateQuery,
new BatchPreparedStatementSetter() {
public void setValues(PreparedStatement ps, int i) throws SQLException {
ps.setInt(1, employeeUpdateDTOs.get(i).empId);
ps.setString(2, employeeUpdateDTOs.get(i).getReason());
ps.setString(3, employeeUpdateDTOs.get(i).getStatus());
ps.setDate(4, new java.sql.Date(System.currentTimeMillis())));
}
public int getBatchSize() {
return employeeUpdateDTOs.size();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}