@Select("SELECT * FROM book_info LEFT JOIN book_type ON book_info.book_type=book_type.id ${ew.customSqlSegment}")
IPage<BookInfoVo> queryAll(Page<BookInfoVo> page, @Param("ew") QueryWrapper<BookInfo> queryWrapper);
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.builder.BuilderException: Error evaluating expression ‘ew.customSqlSegment’. Cause: org.apache.ibatis.ognl.NoSuchPropertyException: com.baomidou.mybatisplus.core.conditions.query.QueryWrapper.customSqlSegment
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: Error instantiating interface com.baomidou.mybatisplus.core.metadata.IPage with invalid types () or values (). Cause: java.lang.NoSuchMethodException: com.baomidou.mybatisplus.core.metadata.IPage.()
移除 PerformanceInterceptor 相关(SQL执行效率插件), 建议使用 p6spy(p6spy是一个开源项目,通常使用它来跟踪数据库操作,查看程序运行过程中执行的sql语句。)
Resolved [org.mybatis.spring.MyBatisSystemException: nested exception
is org.apache.ibatis.exceptions.TooManyResultsException: Expected one
result (or null) to be returned by selectOne(), but found: 6]
mybatis-plus 中page参数不在第一个位置,返回的结果集接收对象不被认为是一个集合,而放在第一位就没有问题。
https://blog.csdn.net/weixin_43611145/article/details/106501748
我的代码:@Select("SELECT * FROM book_info LEFT JOIN book_type ON book_info.book_type=book_type.id ${ew.customSqlSegment}")IPage<BookInfoVo> queryAll(Page<BookInfoVo> page, @Param("ew") Query...
< dependency>
< groupId>com.github.yulichang</ groupId>
< artifactId>
mybatis
-plus-join</ artifactId>
< version>1.1.1</ version>
</ dependency>
或者克隆代码到本地,执行mvn install,再重新以上依赖
添加配置文件
@Configuration
public class
Mybatis
PlusConfig {
* 启用连表拦截器
@Bean
public
Mybatis
PlusInterceptor paginationInterceptor () {
MP自带的条件构造器虽然很强大,有时候也避免不了写稍微复杂一点业务的
sql
,那么那么今天说说MP怎么
自定义
sql语句
吧。另外,除了下文提到的通过
query
Wrapper
实现筛选以外,调用查询时,如果你需要做分页,通过
mybatis
Plus提供的分页接口IPage,能够避免自己手写分页的
sql语句
,这么好用的东西,你还不入坑吗?详情点击:
Mybatis
-plus多表关联查询,多表分页查询
二、具体实现
使用注解实现:
在我们Mapper接口中定义
自定义
方法即可。
public interface
MyBatis
Plus是一个开源的
MyBatis
增强工具,它提供了很多便捷的操作接口,包括实现分页操作。但是在某些情况下,我们需要用到
自定义
的
SQL语句
实现分页操作,
MyBatis
Plus也提供了这样的功能。
首先,我们需要在Mapper接口中定义
自定义
SQL语句
的方法,例如:
```java
@Select("SELECT * FROM user WHERE age > #{age}")
List<User> selectByAge(@Param("age") int age, Page<User> page);
然后,在service层调用该方法时,需要使用
MyBatis
Plus提供的Page工具类来构建分页参数,例如:
```java
Page<User> page = new Page<>(1, 10); // 构建分页参数
List<User> userList = userService.selectByAge(18, page); // 调用
自定义
SQL
方法
page.setRecords(userList); // 构建分页结果
return page;
其中,构建分页参数时,第一个参数为当前页数,第二个参数为每页数量。调用
自定义
SQL
方法时,需要将Page对象作为参数传入。最后,我们可以将查询结果设置到Page对象中,构建完整的分页结果。
值得注意的是,使用
自定义
的
SQL语句
实现分页操作时,需要按照
MyBatis
Plus的分页规则来编写
SQL语句
,例如在SELECT语句中使用LIMIT关键字实现分页。同时,需要避免使用ORDER BY语句,在
SQL语句
中执行排序操作,以保证分页功能的性能。
综上所述,
MyBatis
Plus提供了很多便捷的操作接口,但是在某些情况下,我们需要用到
自定义
的
SQL语句
实现操作,
MyBatis
Plus也提供了这样的功能,只需要按照规则编写
SQL语句
,并将Page对象作为参数传入
自定义
SQL
方法即可实现分页操作。