Mybatis-plus| You have an error in your SQL syntax the right syntax to use near ‘LIMIT 0,10‘

一、问题

在使用Mybatis-plus自定义SQL语句进行查询时,需要用到分页插件进行分页,Mapper层的方法如下:

@Select("select id, title, type_id,user_id, description,update_time,first_picture,views,comment_count  from t_blog where update_time BETWEEN #{start_date} and #{end_date} ORDER BY update_time DESC;")
    @Results(
            {
                    @Result(id=true, column = "id", property = "id"),
                    @Result(column ="appreciation", property = "appreciation"),
                    @Result(column = "commentabled", property = "commentabled"),
//                    @Result(column = "content", property = "content"),
                    @Result(column = "update_time", property = "updateTime"),
                    @Result(column = "description", property = "description"),
                    @Result(column = "first_picture", property = "firstPicture"),
                    @Result(column = "share_statement", property = "shareStatement"),
                    @Result(column = "title", property = "title"),
                    @Result(column = "views", property = "views"),
                    @Result(column = "comment_count", property = "commentCount"),
                    @Result(column = "type_id", property = "type", one=@One(select = "com.kevin.mapper.TypeMapper.selectTypeById", fetchType = FetchType.EAGER)),
                    @Result(column = "user_id", property = "user", one=@One(select = "com.kevin.mapper.UserMapper.selectUserById", fetchType = FetchType.EAGER))
            }
    )
    List<Blog> selectBlogByYearItem( IPage page, @Param("start_date") Date start_date, @Param("end_date") Date end_date);

 报错如下:You have an error in your SQL syntax; check the manual that corresponds to your MySQL 

### 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 'LIMIT 0,10' at line 1
; 
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 'LIMIT 0,10' at line 1] with root cause

 经过各种百度方法测试无效。。

二、解决

当我去掉分页插件,只是用日期参数时能够正常获取数据结果。最后去顶问题出在分页插件上,仔细研究报错信息发现如下:

### SQL: select id, title, type_id,user_id, description,update_time,first_picture,views,comment_count  from t_blog where update_time BETWEEN ? and ? ORDER BY update_time DESC; LIMIT ?,?

问题所在:报错信息中显示出了Mybatis-plus生成的SQL语言,问题就出在多了个分号!!! ==》ORDER BY update_time DESC; LIMIT ?,?

 最后检查代码发现Mapper方法中的:

@Select("select id, title, type_id,user_id, description,update_time,first_picture,views,comment_count  from t_blog where update_time BETWEEN #{start_date} and #{end_date} ORDER BY update_time DESC;")

末尾加了个分号,于是乎Mybatis在生成SQL语句时直接拼接在了后面,这就导致了这个分号出错了。。。。

最后去掉分号问题解决。

最后丝袜哥表示很赞同并给我传回了正确的结果!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值