我想在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>