mybatis plus 如何自定义分页查询
时间: 2023-12-01 07:42:35 浏览: 243
Mybatis Plus提供了很多方便的分页查询方法,但是如果需要自定义分页查询,可以按照以下步骤进行操作:
1.创建一个自定义的分页查询方法,例如:
```java
List<User> selectUserPage(Page<User> page, String name);
```
2.在Mapper.xml文件中编写SQL语句,例如:
```xml
<select id="selectUserPage" resultType="User">
select * from user where name like concat('%',#{name},'%')
limit #{page.offset}, #{page.size}
</select>
```
3.在Service层中调用自定义的分页查询方法,例如:
```java
Page<User> page = new Page<>(1, 10);
List<User> userList = userService.selectUserPage(page, "张三");
```
其中,Page对象是Mybatis Plus提供的分页对象,可以设置当前页码和每页显示数量,第二个参数是查询条件。
相关问题
mybatis plus 自定义分页
MyBatis Plus是一个基于MyBatis的ORM框架增强工具,它简化了数据库操作,包括自定义分页。在MyBatis Plus中,你可以通过扩展`PageHelper`来实现自定义的分页逻辑。以下是如何使用它的步骤:
1. **引入依赖**:首先需要添加MyBatis Plus的分页插件依赖到你的项目中。
```xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-extension</artifactId>
<version>3.x.x</version> <!-- 更新到最新版本 -->
</dependency>
```
2. **创建自定义分页拦截器**:创建一个实现`IPageInterceptor`接口的类,这个类会在查询数据前对分页信息进行处理。
```java
public class CustomPageInterceptor implements IPageInterceptor {
// ... 实现方法,如 before、doIntercept 和 after 等
@Override
public void doIntercept(IPage<T> page, Model model, Object parameter, Map<String, Object> extras) {
// 这里可以根据需求修改分页逻辑,比如设置分页大小、偏移量等
// 示例:设置每页显示50条记录
page.setLimit(50);
}
// 其他方法...
}
```
3. **注册拦截器**:在配置文件中注册你的自定义分页拦截器,让它生效。
```java
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(sqlConfigSource);
Configuration configuration = sqlSessionFactory.getConfiguration();
configuration.addInterceptor(new CustomPageInterceptor());
```
4. **使用分页查询**:在Repository或者Service层中,使用`BaseMapper`提供的`selectPage`方法进行分页查询。
```java
List<User> users = userMapper.selectPage(page, params); // page 是 Page对象,params 是查询条件
```
mybatis-plus自定义分页查询
Mybatis-Plus是一个Mybatis的增强工具,它提供了很多方便的功能,其中包括自定义分页查询。
自定义分页查询可以通过继承Mybatis-Plus的BaseMapper接口来实现。在自定义的Mapper接口中,可以定义一个方法,使用@Param注解来指定查询条件,使用Page对象来指定分页信息。
例如,定义一个自定义Mapper接口:
public interface MyCustomMapper extends BaseMapper<User> {
List<User> selectUserByAge(@Param("age") Integer age, Page<User> page);
}
在这个接口中,我们定义了一个selectUserByAge方法,它接收一个age参数和一个Page对象。在方法中,我们可以使用Mybatis-Plus提供的Wrapper对象来构造查询条件,然后使用selectPage方法来执行查询。
例如,我们可以这样实现selectUserByAge方法:
@Override
public List<User> selectUserByAge(Integer age, Page<User> page) {
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.eq("age", age);
return baseMapper.selectPage(page, wrapper).getRecords();
}
在这个实现中,我们使用QueryWrapper对象来构造查询条件,然后使用baseMapper的selectPage方法来执行查询。最后,我们返回查询结果中的记录列表。
使用自定义分页查询时,我们可以在Controller中使用Page对象来指定分页信息,例如:
@GetMapping("/users")
public Page<User> getUsers(@RequestParam Integer age, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) {
Page<User> page = new Page<>(pageNum, pageSize);
List<User> users = myCustomMapper.selectUserByAge(age, page);
page.setRecords(users);
return page;
}
在这个Controller中,我们使用@RequestParam注解来指定查询条件,然后使用Page对象来指定分页信息。最后,我们调用自定义Mapper接口的selectUserByAge方法来执行查询,然后将查询结果设置到Page对象中并返回。
阅读全文
相关推荐
















