sqlMapClientTemplate:空数组参数导致 BadSqlGrammarException
sqlMapClientTemplate: empty array parameter causes BadSqlGrammarException
Java版本为Java8,MySQL版本为5.6
我在运行这一行JavaSpring:
private SqlMapClientTemplate template = getSqlMapClientTemplate();
List<String> topicList = licReq.getTopicCategories();
template.queryForList("get_topic_ids_from_list", topicList);
其中“get_topic_ids_from_list”在 pom.xml 文件中按以下方式定义:
<select id="get_topic_ids_from_list" parameterClass="list" resultClass="Long">
select id from topic_categories where topic_name in
<iterate open="(" close=")" conjunction=",">
#[]#
</iterate>
</select>
当参数topicList不为空时,正常。但是,如果 topicList 为空,则会导致以下错误:
org.springframework.jdbc.BadSqlGrammarException: SqlMapClient operation; bad SQL grammar []; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred while applying a parameter map.
--- Check the get_topic_ids_from_list-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 '' at line 1
有没有更好的方法来定义 pom.xml 中的“get_topic_ids_from_list”查询,以便它只是 returns 空数组(long 类型),或者如果空数组情况的语句?
查询前需要确保topicList
不为空
List<String> topicList = licReq.getTopicCategories();
if (topicList.size() > 0) {
template.queryForList("get_topic_ids_from_list", topicList);
} else {
// return empty result
}
Java版本为Java8,MySQL版本为5.6
我在运行这一行JavaSpring:
private SqlMapClientTemplate template = getSqlMapClientTemplate();
List<String> topicList = licReq.getTopicCategories();
template.queryForList("get_topic_ids_from_list", topicList);
其中“get_topic_ids_from_list”在 pom.xml 文件中按以下方式定义:
<select id="get_topic_ids_from_list" parameterClass="list" resultClass="Long">
select id from topic_categories where topic_name in
<iterate open="(" close=")" conjunction=",">
#[]#
</iterate>
</select>
当参数topicList不为空时,正常。但是,如果 topicList 为空,则会导致以下错误:
org.springframework.jdbc.BadSqlGrammarException: SqlMapClient operation; bad SQL grammar []; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred while applying a parameter map.
--- Check the get_topic_ids_from_list-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 '' at line 1
有没有更好的方法来定义 pom.xml 中的“get_topic_ids_from_list”查询,以便它只是 returns 空数组(long 类型),或者如果空数组情况的语句?
查询前需要确保topicList
不为空
List<String> topicList = licReq.getTopicCategories();
if (topicList.size() > 0) {
template.queryForList("get_topic_ids_from_list", topicList);
} else {
// return empty result
}