报错:java.sql.SQLException: Parameter index out of range (4 > number of parameter,which is 3)

版权声明:本文为博主原创文章,未经博主允许不得转载。转载请在文章开头添加出处: https://blog.csdn.net/weixin_38676276/article/details/84975941

 Mybatis查询两张表数据统计结果,报错:java.sql.SQLException: Parameter index out of range (4 > number of parameter,which is 3)

<select id="selectBloggerInfo" parameterType="java.lang.String" resultMap="userMap">
   /* select u.id,u.login_name,u.photo,u.number_of_fans,u.number_of_followers,count(e.id) essayCount
    from tb_user u right join tb_hobbies e on u.id =e.user_id
    where u.id = #{userId} and e.state = 2*/
    select u.id,u.login_name,u.photo,u.number_of_fans,u.number_of_followers, count(1) essayCount
    from (select h.id from tb_user u right join tb_hobbies h on u.id =h.user_id
          where u.id = #{userId} and h.state = 2
          union all
          select e.id
          from tb_user u right join tb_essay e on u.id =e.user_id
          where u.id = #{userId} and e.state = 2
    ) as c,tb_user u
    where u.id = #{userId}
</select>

解决:注释错了!!!!!在mapperDao.xml中不能用/**/注释,应该为<!-- -->

<select id="selectBloggerInfo" parameterType="java.lang.String" resultMap="userMap">
   <!-- select u.id,u.login_name,u.photo,u.number_of_fans,u.number_of_followers,count(e.id) essayCount
    from tb_user u right join tb_hobbies e on u.id =e.user_id
    where u.id = #{userId} and e.state = 2-->
    select u.id,u.login_name,u.photo,u.number_of_fans,u.number_of_followers, count(1) essayCount
    from (select h.id from tb_user u right join tb_hobbies h on u.id =h.user_id
          where u.id = #{userId} and h.state = 2
          union all
          select e.id
          from tb_user u right join tb_essay e on u.id =e.user_id
          where u.id = #{userId} and e.state = 2
    ) as c,tb_user u
    where u.id = #{userId}
</select>

或者直接去掉!!!

猜你喜欢

转载自blog.csdn.net/weixin_38676276/article/details/84975941
今日推荐