namedParameterJdbcTemplate返回的spring jdbc数据不是按排序顺序排列的

时间:2013-09-24 20:33:19

标签: spring jdbc spring-jdbc

当我运行以下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;
        }

0 个答案:

没有答案