NamedParameterJdbcTemplate NOT SUPPORT SUM()?

时间:2018-05-15 02:10:51

标签: java spring jdbctemplate

我正在使用NamedParameterJdbcTemplate,下面的SQL在Oraclie SQL Developer中运行良好。

选择总和(PRINTEDLICNUM),总和(PRINTEDLICCOPYNUM),来自LICENSEPRINTRECORD的PRIPID,其中PRIPID在('370212230027855')GROUP BY PRIPID;

但是这段代码在NamedParameterJdbcTemplate

中不起作用
Map namedParameters = Collections.singletonMap("pripids", pripIds);
StringBuffer recordQueryString = new StringBuffer();
recordQueryString.append("SELECT SUM(PRINTEDLICNUM), SUM(PRINTEDLICCOPYNUM), PRIPID from LICENSEPRINTRECORD where PRIPID in (:pripids) GROUP BY PRIPID");
List<PreviousPrintRecords> records = template.query(recordQueryString.toString(), namedParameters, new RowMapper<PreviousPrintRecords>());

下面是我的RowMapper类:

public class PreviousPrintRecords implements Serializable{

/**
 * 
 */
private static final long serialVersionUID = -3763072257141955974L;
private int printedLicNum;
private int printedLicCopyNum;
private String pripId;  

public PreviousPrintRecords() {
    super();
}

public PreviousPrintRecords(int printedLicNum, int printedLicCopyNum, String pripId) {
    super();
    this.printedLicNum = printedLicNum;
    this.setPrintedLicCopyNum(printedLicCopyNum);
    this.pripId = pripId;
}

public int getPrintedLicNum() {
    return printedLicNum;
}

public void setPrintedLicNum(int printedLicNum) {
    this.printedLicNum = printedLicNum;
}

public String getPripId() {
    return pripId;
}

public void setPripId(String pripId) {
    this.pripId = pripId;
}

public int getPrintedLicCopyNum() {
    return printedLicCopyNum;
}

public void setPrintedLicCopyNum(int printedLicCopyNum) {
    this.printedLicCopyNum = printedLicCopyNum;
}

}

会发生错误,嵌套异常是 java.sql.SQLException:列名无效

下面是详细的跟踪:

10:48:09,477 INFO  [stdout] (DefaultThreadPoolService1) 2018-05-15 10:48:09 INFO  o.s.j.support.SQLErrorCodesFactory - SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase, Hana]
10:48:09,532 INFO  [stdout] (DefaultThreadPoolService1) 2018-05-15 10:48:09 ERROR c.a.p.a.task.GetLicenseItemListTask - Get licenseitem task occurs error org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [SELECT SUM(PRINTEDLICNUM), SUM(PRINTEDLICCOPYNUM), PRIPID from LICENSEPRINTRECORD where PRIPID in (?, ?, ?, ?, ?, ?, ?) GROUP BY PRIPID]; nested exception is java.sql.SQLException: Invalid column name

10:48:09,532 INFO  [stdout] (DefaultThreadPoolService1)     at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:231)

10:48:09,532 INFO  [stdout] (DefaultThreadPoolService1)     at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73)

10:48:09,532 INFO  [stdout] (DefaultThreadPoolService1)     at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:649)

10:48:09,532 INFO  [stdout] (DefaultThreadPoolService1)     at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:684)

10:48:09,532 INFO  [stdout] (DefaultThreadPoolService1)     at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:711)

10:48:09,533 INFO  [stdout] (DefaultThreadPoolService1)     at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:761)

10:48:09,533 INFO  [stdout] (DefaultThreadPoolService1)     at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.query(NamedParameterJdbcTemplate.java:192)

10:48:09,533 INFO  [stdout] (DefaultThreadPoolService1)     at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.query(NamedParameterJdbcTemplate.java:199)

10:48:09,534 INFO  [stdout] (DefaultThreadPoolService1)     at com.aw.product.abp.licenseprint.service.DaoLicensePrintService.requestLicensePrintItemData(DaoLicensePrintService.java:167)

10:48:09,534 INFO  [stdout] (DefaultThreadPoolService1)     at com.aw.product.abp.task.GetLicenseItemListTask.run(GetLicenseItemListTask.java:85)

10:48:09,534 INFO  [stdout] (DefaultThreadPoolService1)     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)

10:48:09,534 INFO  [stdout] (DefaultThreadPoolService1)     at java.util.concurrent.FutureTask.run(FutureTask.java:266)

10:48:09,534 INFO  [stdout] (DefaultThreadPoolService1)     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)

10:48:09,535 INFO  [stdout] (DefaultThreadPoolService1)     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)

10:48:09,535 INFO  [stdout] (DefaultThreadPoolService1)     at java.lang.Thread.run(Thread.java:745)

10:48:09,535 INFO  [stdout] (DefaultThreadPoolService1) Caused by: java.sql.SQLException: Invalid column name

10:48:09,535 INFO  [stdout] (DefaultThreadPoolService1)     at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:70)

10:48:09,535 INFO  [stdout] (DefaultThreadPoolService1)     at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:133)

10:48:09,537 INFO  [stdout] (DefaultThreadPoolService1)     at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:199)

10:48:09,539 INFO  [stdout] (DefaultThreadPoolService1)     at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:263)

10:48:09,539 INFO  [stdout] (DefaultThreadPoolService1)     at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:271)

10:48:09,539 INFO  [stdout] (DefaultThreadPoolService1)     at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:445)

10:48:09,539 INFO  [stdout] (DefaultThreadPoolService1)     at oracle.jdbc.driver.OracleStatement.getColumnIndex(OracleStatement.java:3367)

10:48:09,540 INFO  [stdout] (DefaultThreadPoolService1)     at oracle.jdbc.driver.OracleResultSetImpl.findColumn(OracleResultSetImpl.java:2009)

10:48:09,540 INFO  [stdout] (DefaultThreadPoolService1)     at oracle.jdbc.driver.OracleResultSet.getString(OracleResultSet.java:494)

10:48:09,540 INFO  [stdout] (DefaultThreadPoolService1)     at org.jboss.jca.adapters.jdbc.WrappedResultSet.getString(WrappedResultSet.java:1381)

10:48:09,540 INFO  [stdout] (DefaultThreadPoolService1)     at com.aw.product.abp.licenseprint.service.DaoLicensePrintService$2.mapRow(DaoLicensePrintService.java:173)

10:48:09,540 INFO  [stdout] (DefaultThreadPoolService1)     at com.aw.product.abp.licenseprint.service.DaoLicensePrintService$2.mapRow(DaoLicensePrintService.java:1)

10:48:09,541 INFO  [stdout] (DefaultThreadPoolService1)     at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:93)

10:48:09,541 INFO  [stdout] (DefaultThreadPoolService1)     at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:60)

10:48:09,541 INFO  [stdout] (DefaultThreadPoolService1)     at org.springframework.jdbc.core.JdbcTemplate$1.doInPreparedStatement(JdbcTemplate.java:697)

10:48:09,543 INFO  [stdout] (DefaultThreadPoolService1)     at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:633)

10:48:09,543 INFO  [stdout] (DefaultThreadPoolService1)     ... 12 more

NamedParameterJdbcTemplate不支持sum函数吗? 如果我想使用sum函数,有没有办法实现这个目的?

1 个答案:

答案 0 :(得分:0)

  

java.sql.SQLException:列名无效

例外很清楚。您以错误的方式正确命名了表列名称。

您正在使用rowMapper类,但未定义列名。

更改您的代码如下

recordQueryString.append("SELECT SUM(PRINTEDLICNUM) AS printedLicNum, SUM(PRINTEDLICCOPYNUM) AS printedLicCopyNum, PRIPID from LICENSEPRINTRECORD where PRIPID in (:pripids) GROUP BY PRIPID");