sqlmapclienttemplate:空数组参数导致badsqlgrammarexception

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

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

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

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

  1. <select id="get_topic_ids_from_list" parameterClass="list" resultClass="Long">
  2. select id from topic_categories where topic_name in
  3. <iterate open="(" close=")" conjunction=",">
  4. #[]#
  5. </iterate>
  6. </select>

当参数topiclist不为空时,它可以正常工作。但是,如果topiclist为空,则会导致以下错误:

  1. org.springframework.jdbc.BadSqlGrammarException: SqlMapClient operation; bad SQL grammar []; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:
  2. --- The error occurred while applying a parameter map.
  3. --- Check the get_topic_ids_from_list-InlineParameterMap.
  4. --- Check the statement (query failed).
  5. --- 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 查询前不为空

  1. List<String> topicList = licReq.getTopicCategories();
  2. if (topicList.size() > 0) {
  3. template.queryForList("get_topic_ids_from_list", topicList);
  4. } else {
  5. // return empty result
  6. }

相关问题