IBATIS 2.0 dynamically sets table name

I want to dynamically set the table name in the select Ibatis tag.

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

The GetTopSongCount request is called below.

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

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

I get the following error

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

The problem is with double quotes. How can we set the string parameter without double quotes?

+3
source share
1 answer

Instead of including a parameter with hash # toptable #, we need to wrap it with the dollar sign $ toptable $

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

All Articles