mybatis-plus多表分页查询
时间: 2023-04-28 19:06:10 浏览: 87
Mybatis-Plus是基于Mybatis的增强框架,它提供了很多便捷的操作数据库的方法,其中包括多表分页查询。在Mybatis-Plus中,我们可以使用Page类进行分页查询。下面是一个示例:
```
Page<Map<String, Object>> page = new Page<>(1, 10);
List<Map<String, Object>> result = baseMapper.selectMapsPage(page,
new QueryWrapper<Table1>().eq("column1", value1)
.eq("column2", value2));
```
其中baseMapper是你的Mapper接口,Table1是你的实体类,column1和column2是你要查询的字段,value1和value2是你要查询的值。
如果你要进行多表查询,可以使用Mybatis-Plus提供的注解@SqlParser(filter = true),加在你的Mapper接口的方法上,这样就可以忽略分页插件的拦截。
如果你要使用多表分页,需要在xml 中自己编写多表关联查询语句,并通过Page参数进行分页查询
请注意,这是一个示例,在实际开发中,你需要根据你的实际情况进行修改。
相关问题
mybatis-plus多表分页查询代码
MyBatis-Plus提供了一种简便的方法来实现多表分页查询,可以使用Page类中的com.baomidou.mybatisplus.extension.plugins.pagination.Page对象来实现。 下面是代码示例: Page page = new Page(current, size);
List<User> users = userMapper.selectPageVo(page);
mybatis-plus多表分页查询 自定义返回对象
在 MyBatis-Plus 中,可以通过使用 @SqlResultSetMapping 注解和 @Query 注解来实现自定义返回对象的多表分页查询。
首先,需要使用 @SqlResultSetMapping 注解定义自定义返回对象。该注解的 name 属性指定了返回对象的名称,classes 属性指定了返回对象的属性。例如:
```java
@SqlResultSetMapping(name = "UserAndOrderMapping", classes = {
@ConstructorResult(targetClass = UserAndOrderDTO.class,
columns = {
@ColumnResult(name = "id", type = Long.class),
@ColumnResult(name = "name", type = String.class),
@ColumnResult(name = "order_id", type = Long.class),
@ColumnResult(name = "order_name", type = String.class)
})
})
public class UserAndOrderDTO {
private Long id;
private String name;
private Long orderId;
private String orderName;
public UserAndOrderDTO(Long id, String name, Long orderId, String orderName) {
this.id = id;
this.name = name;
this.orderId = orderId;
this.orderName = orderName;
}
// getters and setters
}
```
然后,在使用 @Query 注解时,需要指定 nativeQuery 属性为 true,表示使用原生 SQL 查询。同时,需要将 @SqlResultSetMapping 注解的 name 属性值作为 resultClass 属性的值传入 @Query 注解。例如:
```java
@Query(value = "SELECT u.id, u.name, o.id AS order_id, o.name AS order_name FROM user u LEFT JOIN order o ON u.id = o.user_id",
countQuery = "SELECT COUNT(*) FROM user u LEFT JOIN order o ON u.id = o.user_id",
nativeQuery = true, resultSetMapping = "UserAndOrderMapping")
Page<UserAndOrderDTO> selectUserAndOrderPage(Page page);
```
这样就可以实现自定义返回对象的多表分页查询了。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)