java-IBATIS 2.0动态设置表名

我想在Ibatis select标签中动态设置表名.

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

查询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

问题似乎在于双引号.如何设置不带双引号的字符串参数?

解决方法:

而不是用#toptable#括住参数,我们需要用美元符号$toptable $括起来

<select id="queryGetTopSongCount" parameterClass="java.lang.String" resultClass="java.lang.Integer">
    SELECT
    count(0)
    FROM 
    $toptable$
</select>
上一篇:java-MyBatis中的映射组成


下一篇:mybatis执行test07测试类却显示test05测试类调用的sql语句出错