springboot + mybatisplus分页查询
时间: 2023-11-24 19:49:15 浏览: 125
Spring Boot是一个基于Spring框架的快速开发Web应用程序的框架,而Mybatis Plus是Mybatis的增强工具,可以简化Mybatis的开发流程。在Spring Boot中使用Mybatis Plus进行分页查询,需要进行以下步骤:
1. 在pom.xml文件中添加Mybatis Plus和PageHelper的依赖。
2. 编写Mybatis Plus的配置类,其中包括分页插件的配置。
3. 在Mapper接口中编写分页查询的方法。
4. 在Controller中调用Mapper接口中的分页查询方法,并将查询结果返回给前端。
具体实现可以参考引用中提供的代码资源。其中,前端使用Vue和ElementUI进行开发,后端使用Spring Boot和Mybatis Plus进行开发,数据库使用MySQL。在Mybatis Plus的配置类中,使用PaginationInterceptor进行分页插件的配置。在Mapper接口中,使用Mybatis Plus提供的分页查询方法进行分页查询。在Controller中,使用@RequestParam注解获取前端传递的分页参数,并调用Mapper接口中的分页查询方法进行查询。
相关问题
springboot+mybatisplus查询分页
Spring Boot和MyBatis Plus是一对非常好的组合,可以轻松地实现查询分页功能。下面是一个简单的步骤:
1.在pom.xml文件中添加MyBatis Plus和MySQL的依赖。
2.创建一个实体类,并使用MyBatis Plus的注解来映射数据库表。
3.创建一个Mapper接口,并继承MyBatis Plus提供的BaseMapper接口。
4.在Mapper接口中定义一个方法,使用MyBatis Plus提供的分页查询方法来实现分页查询。
5.在Controller中调用Mapper接口中的方法,并将查询结果返回给前端。
下面是一个简单的示例代码:
```
// 实体类
@Data
public class User {
private Long id;
private String name;
private Integer age;
}
// Mapper接口
public interface UserMapper extends BaseMapper<User> {
List<User> selectUserPage(Page<User> page, @Param("name") String name);
}
// Controller
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@GetMapping("/users")
public Page<User> getUsers(@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "10") Integer pageSize,
@RequestParam(required = false) String name) {
Page<User> page = new Page<>(pageNum, pageSize);
List<User> users = userMapper.selectUserPage(page, name);
page.setRecords(users);
return page;
}
}
```
在上面的示例中,我们使用了MyBatis Plus提供的分页查询方法selectPage,并将查询结果封装到了Page对象中返回给前端。同时,我们还可以通过@RequestParam注解来接收前端传递的分页参数和查询条件。
springboot+mybatisplus如何分页查询
### Spring Boot 中 MyBatis-Plus 分页查询示例
#### 配置环境
为了在 Spring Boot 项目中使用 MyBatis-Plus 实现分页查询,首先需要完成项目的初始化以及必要的依赖引入。以下是具体的操作说明:
1. **创建 Spring Boot 项目**
使用 Spring Initializr 或其他方式创建一个新的 Spring Boot 项目。
2. **导入依赖**
在 `pom.xml` 文件中添加 MyBatis-Plus 和数据库驱动的相关依赖[^2]。
```xml
<!-- MyBatis-Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<!-- 数据库驱动 (以 MySQL 为例) -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
```
3. **配置数据库连接**
修改 `application.yml` 文件,设置数据库连接信息。
```yaml
spring:
datasource:
url: jdbc:mysql://localhost:3306/your_database?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
username: root
password: your_password
driver-class-name: com.mysql.cj.jdbc.Driver
```
4. **启用分页插件**
在启动类或配置类中注册 MyBatis-Plus 提供的分页拦截器[^3]。
```java
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyBatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
}
```
---
#### 编写实体类和 Mapper 接口
1. **定义实体类**
创建一个与数据库表对应的实体类,例如 `User.java`。
```java
package com.example.demo.entity;
public class User {
private Long id;
private String name;
private Integer age;
// Getter 和 Setter 方法省略
}
```
2. **编写 Mapper 接口**
定义继承自 `BaseMapper<T>` 的 Mapper 接口,用于操作数据库表。
```java
package com.example.demo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper extends BaseMapper<User> {}
```
---
#### 实现分页查询
1. **调用分页方法**
在 Service 层或 Controller 层中调用 `page` 方法实现分页查询[^5]。
```java
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public IPage<User> getUserList(int currentPage, int pageSize) {
Page<User> page = new Page<>(currentPage, pageSize);
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
return userMapper.selectPage(page, queryWrapper);
}
}
```
2. **Controller 调用**
将分页结果封装并返回给前端。
```java
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/users")
public IPage<User> getUsers(@RequestParam int currentPage, @RequestParam int pageSize) {
return userService.getUserList(currentPage, pageSize);
}
}
```
---
#### 测试分页功能
访问 URL `/users?currentPage=1&pageSize=10` 即可测试分页查询的结果。返回的数据结构通常如下所示:
```json
{
"records": [
{"id": 1, "name": "Alice", "age": 25},
{"id": 2, "name": "Bob", "age": 30}
],
"total": 100,
"size": 10,
"current": 1,
"pages": 10
}
```
---
### 自定义 SQL 查询
如果需要执行复杂的分页查询,可以通过 XML 映射文件或注解的方式定义自定义 SQL[^4]。
1. **XML 方式**
在 Mapper 对应的 XML 文件中定义 `<select>` 标签,并结合 `#{}` 参数占位符实现动态查询。
```xml
<mapper namespace="com.example.demo.mapper.UserMapper">
<select id="selectUsersByCondition" resultType="com.example.demo.entity.User">
SELECT * FROM user WHERE age > #{minAge} LIMIT #{offset}, #{limit}
</select>
</mapper>
```
2. **注解方式**
在 Mapper 接口中直接使用 `@Select` 注解定义 SQL。
```java
@Select("SELECT * FROM user WHERE age > #{minAge}")
List<User> selectUsersByCondition(@Param("minAge") int minAge);
```
---
阅读全文
相关推荐














