如何在select子句中使用参数化列映射iBATIS的查询? [英] How to map query for iBATIS with parameterized column in select clause?

查看:259
本文介绍了如何在select子句中使用参数化列映射iBATIS的查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想拥有一种从数据库中特定表的列中查找某个值的方法,该列的名称作为参数传递给该表.因此,Java方法将具有以下签名:

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$ &gt; 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

请注意,"FIRST_COLUMN"代表第一列的名称,即使该错误发生在第二次调用中,也从未发生在第一次调用中.

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$ &gt; 0
</select>

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

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

推荐答案

在SQL查询中使用别名.那应该解决将结果映射回java的问题.

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

这篇关于如何在select子句中使用参数化列映射iBATIS的查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆