spring Java中的RowMapper列名无效错误

wbgh16ku  于 2023-02-11  发布在  Spring
关注(0)|答案(1)|浏览(198)

我有一个查询,它在数据库上运行良好。但是,当我试图通过使用RowMapper将它们作为Java对象时,我得到了一个无效列名错误。我检查了所有内容,但我不明白发生此错误的原因。
我的疑问:

SELECT TEMP.SUMALLTXN, SUM(TEMP.SUMCARD), SUM(TEMP.SUMERRORTXN), SUM(TEMP.SUMERRORTXNCARD)
FROM
(SELECT
SUM(COUNT(*)) OVER() AS SUMALLTXN,

COUNT(mdmtxn.BIN) OVER (PARTITION BY mdmtxn.BIN) AS SUMCARD,

SUM(case when mdmtxn.MDSTATUS NOT IN ('1','9', '60') then 1 else 0 end) AS SUMERRORTXN,

SUM(case when mdmtxn.MDSTATUS NOT IN ('1','9', '60') then 1 else 0 end) OVER (PARTITION BY mdmtxn.BIN) AS SUMERRORTXNCARD

FROM MDM59.MDMTRANSACTION2 mdmtxn WHERE
    mdmtxn.CREATEDDATE < TO_CHAR(SYSDATE - INTERVAL ':initialMinuteParameterValue' MINUTE ,'YYYYMMDD HH24:MI:SS') AND
    mdmtxn.CREATEDDATE > TO_CHAR(SYSDATE - INTERVAL ':intervalMinuteParameterValue' MINUTE ,'YYYYMMDD HH24:MI:SS')
GROUP BY mdmtxn.MDSTATUS, mdmtxn.BIN
) TEMP
GROUP BY TEMP.SUMALLTXN

我的行Map器:

@Component
public class TotalTransactionsReportRw implements RowMapper<TotalTransactionsReportDto> {

    @Override
    public TotalTransactionsReportDto mapRow(ResultSet rs, int rowNum) throws SQLException {
        return TotalTransactionsReportDto.builder()
                .totalNumbersOfTransactions(rs.getString("SUMALLTXN"))
                .totalNumbersOfCard(rs.getString("SUMCARD"))
                .totalNumbersOfErrorTransactions(rs.getString("SUMERRORTXN"))
                .totalNumbersOfErrorCard(rs.getString("SUMERRORTXNCARD"))
                .build();
    }

    private static class TotalTransactionsDetailRwHolder {
        private static final TotalTransactionsReportRw INSTANCE = new TotalTransactionsReportRw();
    }

    public static TotalTransactionsReportRw getInstance() {
        return TotalTransactionsReportRw.TotalTransactionsDetailRwHolder.INSTANCE;
    }
}

我的Dto:

@Value
@Builder
@Data
public class TotalTransactionsReportDto {
    private String totalNumbersOfTransactions;
    private String totalNumbersOfCard;
    private String totalNumbersOfErrorTransactions;
    private String totalNumbersOfErrorCard;
}

在我的tasklet类中,我创建了一个列表来从rowmapper获取所有数据:

@Slf4j
@Component
@RequiredArgsConstructor
public class NotificationTasklet implements Tasklet {

    private final PofPostOfficeServiceClient pofPostOfficeServiceClient;
    private final SequenceSysGuid sequenceSysGuid;
    private final BatchProps batchProps;
    private JdbcTemplate jdbcTemplate;
    private String notificationMailSql;
    private String totalTransactionsSql;
    private String endOfHtmlString = "</table></body></html>";
    private String endOfTableString = "</table>";
    private String jobName = "vpos-notification";
    private String tdClose = "</td>";`

    @Override
    public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {

        List<VposNotificationBatchDto> notificationList = getNotificationList();

        List<TotalTransactionsReportDto> totalTransactionsList = getTotalTransactionsList();

        AlertMailDto alertMailDto = createAlertMailDto(notificationList,totalTransactionsList);

        if (!(notificationList.isEmpty())) {
            sendMail(alertMailDto);
        }

        return RepeatStatus.FINISHED;
    }

    List<TotalTransactionsReportDto> getTotalTransactionsList() {
        return jdbcTemplate.query(
                totalTransactionsSql,
                new TotalTransactionsReportRw());
    }

    @Autowired
    public void     setTotalTransactionsSql(@Value("classpath:sql/vposnotification/select_total_transactions_data.sql")
                                               Resource res) {
        int intervalnext = batchProps.getJobProps()
                .get(jobName).getAlertProps().getIntervalMinuteParameterValue();
        String intervalMinutes = String.valueOf(intervalnext);

        int initialMinuteParameterValue = batchProps.getJobProps()
                .get(jobName).getAlertProps().getInitialMinuteParameterValue();
        String initialMinutes = String.valueOf(initialMinuteParameterValue);

        this.totalTransactionsSql = SqlUtils.readSql(res);
             this.totalTransactionsSql = this.totalTransactionsSql.replace(":initialMinuteParameterValue",          initialMinutes);
              this.totalTransactionsSql = this.totalTransactionsSql.replace(":intervalMinuteParameterValue",     intervalMinutes);
    }

    @Autowired
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
         this.jdbcTemplate = jdbcTemplate;
    }
ep6jt1vc

ep6jt1vc1#

问题是,您的查询实际上并不包含列SUMCARDSUMERRORTXNSUMERRORTXNCARD。虽然有些DBMS使用要求和的列的名称作为SUM列的别名,但Oracle不是其中之一。IIRC,Oracle将其作为别名,例如"SUM(SUMCARD)""SUM(TEMP.SUMCARD)"。但是,在我看来,这是一个不应该依赖实现细节。
要获得想要使用的名称,需要显式地为SUM列指定别名,例如SUM(TEMP.SUMCARD) AS SUMCARD

相关问题