IBATIS 2.0 动态设置表名

3
我希望您能在Ibatis的select标签中动态设置表名。
<select id="queryGetTopSongCount" parameterClass="java.lang.String" resultClass="java.lang.Integer">
    SELECT
    count(0)
    FROM 
    #toptable#
</select>

以下是调用Query GetTopSongCount:

Map<String, Object> parameterMap = new HashMap<String, Object>();
parameterMap.put("toptable", "top_of_week_tab_6_2014");

int totalPagination=(Integer)getMainSqlMapClient().queryForObject(queryGetTopSongCount, toptable);

我遇到了以下错误:

com.ibatis.common.jdbc.exception.NestedSQLException:   
--- The error occurred in resources/ibatis/song-sqlMap.xml.  
--- The error occurred while applying a parameter map.  
--- Check the song.queryGetTopSongCount-InlineParameterMap.  
--- Check the statement (query failed).  
--- Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''top_of_week_tab_6_2014'' at line 1

问题似乎出在双引号上。我们如何在没有双引号的情况下设置字符串参数?
2个回答

3

我们需要使用美元符号$来包含参数$toptable$,而不是井号#。

<select id="queryGetTopSongCount" parameterClass="java.lang.String" resultClass="java.lang.Integer">
    SELECT
    count(0)
    FROM 
    $toptable$
</select>

1
请问您能解释一下这个是从哪里得来的吗?因为阅读(旧)文档并不能给我答案:关于#和$之间的区别,根本没有清晰/明确的解释。https://ibatis.apache.org/docs/java/pdf/iBATIS-SqlMaps-2_en.pdf - maxxyme

0

@Hasan Jamshaid已经展示了解决方案

接下来的这个也可以

<select id="queryGetTopSongCount" parameterClass="java.lang.String" resultClass="java.lang.Integer">
    SELECT
    count(0)
    FROM 
    ${toptable}
</select>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接