mybatisplus多表分页
时间: 2023-11-29 22:47:44 浏览: 102
以下是使用MyBatis Plus实现多表联接、分页查询的方法:
1.首先,在pom.xml文件中添加MyBatis Plus的依赖:
```xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
```
2.在application.yml文件中配置MyBatis Plus的分页插件:
```yaml
mybatis-plus:
configuration:
# 开启驼峰命名转换
map-underscore-to-camel-case: true
# 配置分页插件
plugins:
- com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor
```
3.创建一个VO类,用于封装多表联接查询的结果:
```java
@Data
public class QuestionWithStudentVO extends Question {
private Student student;
}
```
4.创建一个Mapper接口,用于定义多表联接查询的方法:
```java
public interface QuestionMapper extends BaseMapper<Question> {
List<QuestionWithStudentVO> selectQuestionWithStudentByPage(Page<QuestionWithStudentVO> page);
}
```
5.创建一个Mapper XML文件,用于实现多表联接查询的SQL语句:
```xml
<select id="selectQuestionWithStudentByPage" resultMap="BaseResultMap">
SELECT q.*, s.*
FROM question q
LEFT JOIN student s ON q.student_id = s.id
ORDER BY q.create_time DESC
</select>
```
6.在Service层中调用Mapper接口的方法,实现多表联接、分页查询:
```java
@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> implements QuestionService {
@Autowired
private QuestionMapper questionMapper;
@Override
public IPage<QuestionWithStudentVO> getQuestionWithStudentByPage(Page<QuestionWithStudentVO> page) {
return questionMapper.selectQuestionWithStudentByPage(page);
}
}
```
阅读全文