IBatis如何在一次调用中执行多个查询

时间:2015-07-21 09:59:59

标签: mysql spring ibatis

我正在使用IBatis和Spring框架。

我想执行多个查询,但我无法得到响应。

<select id="getUIs" resultMap="blpUiVOMap" parameterClass="Map">
    SELECT
      GROUP_CONCAT(DISTINCT
        CONCAT('COUNT(CASE WHEN bug.BUG_STT = ', STT_ID, ' THEN 1 END) AS ''bugI', STT_ID, '''')
      ) INTO @sql
    from BLP_STT <![CDATA[;]]>

    SET @sql = CONCAT('SELECT ui.UI_ID, ui.UI_NM, cat.CAT_ID, cat.CAT_NM, cat.PRNT_ID, cat1.CAT_NM as PRNT_NM,',@sql,'FROM BLP_UI ui JOIN BLP_CAT cat ON ui.CAT_ID = cat.CAT_ID JOIN BLP_CAT cat1 ON cat.PRNT_ID = cat1.CAT_ID JOIN BLP_BUG bug ON ui.UI_ID = bug.UI_ID JOIN BLP_STT stt ON bug.BUG_STT = stt.STT_ID WHERE 1 = 1 GROUP BY ui.UI_ID ORDER BY ui.UI_ID ASC') <![CDATA[;]]>

    PREPARE stmt FROM @sql <![CDATA[;]]>
    EXECUTE stmt <![CDATA[;]]>
    DEALLOCATE PREPARE stmt <![CDATA[;]]>
</select>

在MySQL中,此查询工作正常。

1 个答案:

答案 0 :(得分:1)

<select>将在JDBC中创建一个PreparedStatement。您正在尝试从MySQL命令行执行命令。这在JDBC或MyBatis中不起作用。

您需要创建一个组合所有字符串的查询。也许是这样的?

SELECT ui.UI_ID, ui.UI_NM, cat.CAT_ID, cat.CAT_NM, cat.PRNT_ID, cat1.CAT_NM as PRNT_NM,
(SELECT
  GROUP_CONCAT(DISTINCT
  CONCAT('COUNT(CASE WHEN bug.BUG_STT = ', STT_ID, ' THEN 1 END) AS ''bugI', STT_ID, '''')
  )
  from BLP_STT
)
FROM BLP_UI ui JOIN BLP_CAT cat ON ui.CAT_ID = cat.CAT_ID JOIN BLP_CAT cat1 ON cat.PRNT_ID = cat1.CAT_ID JOIN BLP_BUG bug ON ui.UI_ID = bug.UI_ID JOIN BLP_STT stt ON bug.BUG_STT = stt.STT_ID WHERE 1 = 1 GROUP BY ui.UI_ID ORDER BY ui.UI_ID ASC')

注意,您也不需要在XML中放置;