如何将 iBATIS 查询与 select 子句中的参数化列映射?

发布于 2024-11-10 01:17:17 字数 1991 浏览 0 评论 0原文

我想要一种方法,可以从数据库中特定表的列中查找特定值,其中列的名称作为参数传入。因此,Java 方法将具有以下签名:

public Integer getFoo(String column) throws DataAccessException;

我尝试对此查询的映射如下:

<select id="getFoo" parameterClass="java.lang.String" resultClass="java.lang.Integer">
    select min($column$) from daily_statistics where $column$ &gt; 0
</select>

这以一种有趣的方式失败了。如果我调用这个方法一次,它就会起作用。但是,如果我使用不同的列名称调用它两次,第二次调用就会失败,并显示以下堆栈跟踪:

Caused by: com.ibatis.common.jdbc.exception.NestedSQLException:   
--- The error occurred in com/company/project/dao/ibatis/maps/FooBar.xml.  
--- The error occurred while applying a result map.  
--- Check the getSomething-AutoResultMap.  
--- Check the result mapping for the 'MIN(FIRST_COLUMN)' property.  
--- Cause: java.sql.SQLException: Invalid column name
Caused by: java.sql.SQLException: Invalid column name
at com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryWithCallback(GeneralStatement.java:181)
at com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryForObject(GeneralStatement.java:100)
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForObject(SqlMapExecutorDelegate.java:561)
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForObject(SqlMapExecutorDelegate.java:536)
at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.queryForObject(SqlMapSessionImpl.java:97)
at org.springframework.orm.ibatis.SqlMapClientTemplate$1.doInSqlMapClient(SqlMapClientTemplate.java:273)
at org.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:209)
... 21 more

请注意,“FIRST_COLUMN”表示第一列的名称,即使错误发生在第二次调用时,也不会在第一次调用时发生。

我发现以下映射即使多次调用也不会给出错误:

<select id="getFoo" parameterClass="java.lang.String" resultClass="java.lang.Integer">
    select min(ANY_COLUMN) from daily_statistics where $column$ &gt; 0
</select>

因此,问题似乎与 select 子句中参数化列的使用有关。

I want to have a method that finds a certain value from a column of a particular table in the database, where the name of the column is passed in as a parameter. So the Java method would have the following signature:

public Integer getFoo(String column) throws DataAccessException;

My attempted mapping for this query is the following:

<select id="getFoo" parameterClass="java.lang.String" resultClass="java.lang.Integer">
    select min($column$) from daily_statistics where $column$ > 0
</select>

This fails in an interesting way. If I call this method once, it works. But if I call it twice with different column names, the second call fails with the following stack trace:

Caused by: com.ibatis.common.jdbc.exception.NestedSQLException:   
--- The error occurred in com/company/project/dao/ibatis/maps/FooBar.xml.  
--- The error occurred while applying a result map.  
--- Check the getSomething-AutoResultMap.  
--- Check the result mapping for the 'MIN(FIRST_COLUMN)' property.  
--- Cause: java.sql.SQLException: Invalid column name
Caused by: java.sql.SQLException: Invalid column name
at com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryWithCallback(GeneralStatement.java:181)
at com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryForObject(GeneralStatement.java:100)
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForObject(SqlMapExecutorDelegate.java:561)
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForObject(SqlMapExecutorDelegate.java:536)
at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.queryForObject(SqlMapSessionImpl.java:97)
at org.springframework.orm.ibatis.SqlMapClientTemplate$1.doInSqlMapClient(SqlMapClientTemplate.java:273)
at org.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:209)
... 21 more

Note that 'FIRST_COLUMN' represents the name of the first column, even though the error occurs on the second call, never on the first call.

I have discovered that the following mapping will not give errors, even when called multiple times:

<select id="getFoo" parameterClass="java.lang.String" resultClass="java.lang.Integer">
    select min(ANY_COLUMN) from daily_statistics where $column$ > 0
</select>

So it seems that the problem is related to the use of a parametrized column in the select clause.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

避讳 2024-11-17 01:17:17

在 SQL 查询中使用别名。这应该可以解决将结果映射回 java 的问题。

Use an alias in the SQL query. That should solve the problem of mapping the result back to java.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文