sqlmapclienttemplate:空数组参数导致badsqlgrammarexception

vxf3dgd4  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(261)

java版本是Java8,mysql版本是5.6
我在java spring中运行这一行:

private SqlMapClientTemplate template = getSqlMapClientTemplate();
List<String> topicList = licReq.getTopicCategories();
template.queryForList("get_topic_ids_from_list", topicList);

在pom.xml文件中,按照以下方式定义“从列表中获取主题\u id\u”:

<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”查询,以便它只返回空数组(long类型),还是必须为空数组大小写if语句?

2nbm6dog

2nbm6dog1#

你需要确认一下 topicList 查询前不为空

List<String> topicList = licReq.getTopicCategories();

if (topicList.size() > 0) {
    template.queryForList("get_topic_ids_from_list", topicList);
} else {
    // return empty result
}

相关问题