Error querying database. Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL

在写Mapper文件中的SQL语句时,写完单元测试报了如下错误Error querying database. Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use****

查了许久没发现问题,最后终于发现了。

@Select({"select last_profit and total_profit"+
            "from profit"+
            "where user_id=#{userId} and last_day=#{lastDay}"})
    List<Float> queryProfit(@Param("userId")String userId,@Param("lastDay")String lastDay);

在书写注解中的SQL语句时,直接按了ENTER键换行,IDEA自动给我补全了+,但是我没有留空格,当写“+”号时,注意在换行时在引号里留空格,否则改成一下形式也可以。

@Select({"select last_profit and total_profit" ,
            "from profit" ,
            "where user_id=#{userId} and last_day=#{lastDay}"})
    List<Float> queryProfit(@Param("userId")String userId,@Param("lastDay")String lastDay);

用“,”就没有这个问题了,至此,错误解决。
当然这个报错还有可能是别的语法错误,我只列出了我出现的小问题供大家参考。


版权声明:本文为weixin_45614626原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。