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

在使用Mybatis-Plus自定义SQL查询博客时,遇到分页插件导致的SQL语法错误。问题在于Mapper方法中的分号多余,移除后解决了查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、问题

在使用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语句时直接拼接在了后面,这就导致了这个分号出错了。。。。

最后去掉分号问题解决。

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

@Override public Page<Order> getOrders(String customerName, Integer page, Integer limit, HttpServletRequest request) { UserVO orderVO = (UserVO) request.getSession().getAttribute(UserConstant.LOGIN_INFO); if (orderVO == null || !"0".equals(orderVO.getRole())) { throw new BusinessException(ErrorCode.NO_AUTH_ERROR); } if (limit > 100) { throw new BusinessException(ErrorCode.OPERATION_ERROR); } Page<Order> orderPage = new Page<>(page, limit); LambdaQueryWrapper<Order> orderLambdaQueryWrapper = new LambdaQueryWrapper<Order>() .like(StringUtils.hasText(customerName), Order::getCustomerName, customerName) .orderByDesc(Order::getId); Page<Order> selectPage = orderMapper.selectPage(orderPage, orderLambdaQueryWrapper); return selectPage; }上面是我的代码,报错信息如下:### 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 near 'order' at line 1 ### The error may exist in com/ease/book/mapper/OrderMapper.java (best guess) ### The error may involve defaultParameterMap ### The error occurred while setting parameters ### SQL: SELECT COUNT(*) AS total FROM order ### 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 near 'order' at line 1 ] 21:48:48.300 [http-nio-8080-exec-1] DEBUG org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver - Using @ExceptionHandler com.ease.book.exception.GlobalExceptionHandler#runtimeExceptionHandler(RuntimeException) 21:48:48.304 [http-nio-8080-exec-1] ERROR com.ease.book.exception.GlobalExceptionHandler - RuntimeException org.springframework.jdbc.BadSqlGrammarException: ### Error querying database. Cause: java.sql.SQLSyntaxErrorExcepti
03-19
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值