刚开始使用sql的时候遇到换行没有加空格导致的小白低级错误,
例子如下:
- 错误sql代码片段
public interface ArticleMapper {
// @Select("select * from tb_article" +
// "where id in(select article_id from tb_item" +
// "where order_id = #{id})")
//
@Select("SELECT * FROM tb_article " +
"WHERE id IN (SELECT article_id FROM tb_item " +
"WHERE order_id = #{id} ) ")
List<Article> selectByOrderId(Integer order_id);
}
如果写成像上面注释代码,即在使用 + 连接符前面的引号未加空格 (tb_article 和tb_item后面未加空格)则会报如下错误:
- 错误提示
Exception in thread "main" 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 'IN (SELECT article_id FROM tb_item WHERE order_id = 1 )' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
解决办法
见代码中未加注释部分的sql代码,即换行是在引号前面加上空格