ushouldup 2021-11-08 21:50 采纳率: 83.3%
浏览 43

mybatis用动态SQL<foreach>报错

代码:

<!--根据角色id查询所拥有的菜单信息 public List<Menu> findParentMenuByRoleId(List<Integer> ids);-->
    <select id="findParentMenuByRoleId" parameterType="java.util.List" resultType="com.lagou.domain.Menu">
        SELECT
        DISTINCT m.*
        FROM roles r INNER JOIN role_menu_relation rm ON r.`id` = rm.role_id
        INNER JOIN menu m ON rm.menu_id = m.id
        WHERE m.parent_id = -1 AND r.id IN
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        GROUP BY m.id
    </select>

报错信息:
21:30:46,742 DEBUG findParentMenuByRoleId:143 - ==> Preparing: SELECT DISTINCT m.* FROM roles r INNER JOIN role_menu_relation rm ON r.id = rm.role_id INNER JOIN menu m ON rm.menu_id = m.id WHERE m.parent_id = -1 AND r.id IN GROUP BY m.id
21:30:46,743 DEBUG findParentMenuByRoleId:143 - ==> Parameters:
21:30:46,745 DEBUG SqlSessionUtils:191 - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@16b5d491]
21:30:46,745 DEBUG SQLErrorCodeSQLExceptionTranslator:402 - Translating SQLException with SQL state '42000', error code '1064', message [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 'GROUP BY m.id' at line 7] for task [

Error querying database. 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 'GROUP BY m.id' at line 7

The error may exist in com/lagou/dao/UserMapper.xml

The error may involve com.lagou.dao.UserMapper.findParentMenuByRoleId-Inline

The error occurred while setting parameters

SQL: SELECT DISTINCT m.* FROM roles r INNER JOIN role_menu_relation rm ON r.id = rm.role_id INNER JOIN menu m ON rm.menu_id = m.id WHERE m.parent_id = -1 AND r.id IN GROUP BY m.id

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 'GROUP BY m.id' at line 7

]
21:30:46,746 DEBUG DispatcherServlet:1100 - Failed to complete request: org.springframework.jdbc.BadSqlGrammarException:

Error querying database. 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 'GROUP BY m.id' at line 7

The error may exist in com/lagou/dao/UserMapper.xml

The error may involve com.lagou.dao.UserMapper.findParentMenuByRoleId-Inline

The error occurred while setting parameters

SQL: SELECT DISTINCT m.* FROM roles r INNER JOIN role_menu_relation rm ON r.id = rm.role_id INNER JOIN menu m ON rm.menu_id = m.id WHERE m.parent_id = -1 AND r.id IN GROUP BY m.id

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 'GROUP BY m.id' at line 7

; bad SQL grammar []; nested exception is 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 'GROUP BY m.id' at line 7

  • 写回答

3条回答 默认 最新

  • flying_fish79 2021-11-09 09:01
    关注

    少了index?

    评论

报告相同问题?

问题事件

  • 创建了问题 11月8日