当我运行以下 SQL 时,数据以预期的排序顺序(last_run_date,升序)返回:
select * from ( select a.*, ROWNUM rnum from ( select id, job_name, description, enabled, last_run_date, last_run_msg, current_status, userid from JOBS ORDER BY last_run_date asc ) a where ROWNUM <= 5 ) where rnum > 0
但是,当我运行以下 Java/Spring 代码时,数据未按指定的排序顺序。
private final String GET_ALL_JOBS_JQGRID_SQL = "select * from " +
"( select a.*, ROWNUM rnum from " +
"( select id, job_name, description, enabled, last_run_date, last_run_msg, current_status, userid from JOBS ORDER BY :sidx asc ) a " +
" where ROWNUM <= :max_row_to_fetch ) " +
"where rnum > :min_row_to_fetch";
private NamedParameterJdbcTemplate jobStatusParameterJdbcTemplate;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
this.jobStatusParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
}
我传入 last_run_date、10 和 0 作为参数值。
public List<Job> getAllJobs(String sidx, int max_row_to_fetch, int min_row_to_fetch) {
SqlParameterSource namedParameters = new MapSqlParameterSource("sidx", sidx)
.addValue("max_row_to_fetch", max_row_to_fetch)
.addValue("min_row_to_fetch", min_row_to_fetch);
@SuppressWarnings("unchecked")
List <Job> jobs = jobStatusParameterJdbcTemplate.query(GET_ALL_JOBS_JQGRID_SQL, namedParameters, new JobRowMapper());
return jobs;
}
private static class JobRowMapper implements RowMapper
{
public Object mapRow(ResultSet rs, int arg1) throws SQLException
{
int jobId = rs.getInt(1);
String jobName = rs.getString(2);
String jobDesc = rs.getString(3);
String jobEnabled = rs.getString(4);
Date jobLastRunDate = rs.getDate(5);
String jobLastRunMsg = rs.getString(6);
String jobCurrentStatus = rs.getString(7);
String userid = rs.getString(8);
Job job = new Job();
job.setId(jobId);
job.setJob_name(jobName);
job.setDescription(jobDesc);
job.setEnabled(jobEnabled);
job.setLast_run_date(jobLastRunDate);
job.setLast_run_msg(jobLastRunMsg);
job.setCurrent_status(jobCurrentStatus);
job.setUserid(userid);
return job;
}