pagehelper联表分页查询

(springboot2.0.1、pagehelper5.1.4)

pagehelper联表分页,会默认在sql语句的后面添加 limit ?,进行分页。如果是复杂的sql联表查询结果远远不能满足我们,添加 limit在我们想要的位置进行分页,下面的代码中“AS limitable”,为分页标识的sql. 末尾带有 AS limitable 的子查询会拼接上limit进行先分页.然后再外联查询

import java.lang.reflect.Field;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

import com.github.pagehelper.Dialect;
import com.github.pagehelper.PageException;
import com.github.pagehelper.cache.Cache;
import com.github.pagehelper.cache.CacheFactory;
import com.github.pagehelper.util.MSUtils;
import com.github.pagehelper.util.StringUtil;


@SuppressWarnings({ "rawtypes", "unchecked" })
@Intercepts({
		@Signature(type = Executor.class, method = "query", args = { MappedStatement.class, Object.class,
				RowBounds.class, ResultHandler.class }),
		@Signature(type = Executor.class, method = "query", args = { MappedStatement.class, Object.class,
				RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class }), })
public class MumuPageInterceptor implements Interceptor {
	//private static final Logger logger = LoggerFactory.getLogger(MumuPageInterceptor.class);

	// 缓存count查询的ms
	protected Cache<String, MappedStatement> msCountMap = null;
	private Dialect dialect;
	private String default_dialect_class = "com.github.pagehelper.PageHelper";
	private Field additionalParametersField;
	private String countSuffix = "_COUNT";

	@Override
	public Object intercept(Invocation invocation) throws Throwable {
		try {
			Object[] args = invocation.getArgs();
			MappedStatement ms = (MappedStatement) args[0];
			Object parameter = args[1];
			RowBounds rowBounds = (RowBounds) args[2];
			ResultHandler resultHandler = (ResultHandler) args[3];
			Executor executor = (Executor) invocation.getTarget();
			CacheKey cacheKey;
			BoundSql boundSql;
			// 由于逻辑关系,只会进入一次
			if (args.length == 4) {
				// 4 个参数时
				boundSql = ms.getBoundSql(parameter);
				cacheKey = executor.createCacheKey(ms, parameter, rowBounds, boundSql);
			} else {
				// 6 个参数时
				cacheKey = (CacheKey) args[4];
				boundSql = (BoundSql) args[5];
			}
			List resultList;
			// 调用方法判断是否需要进行分页&#
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
pagehelper是一个开源的Java分页插件,可以方便地实现多表查询的分页功能。使用pagehelper进行多表查询分页,需要进行以下几个步骤: 1. 首先,在你的项目中引入pagehelper的依赖。可以在pom.xml文件中添加以下代码: ```xml <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>最新版本号</version> </dependency> ``` 2. 在需要进行多表查询分页的Mapper接口的方法上,使用@Select注解定义查询语句。查询语句可以包含多个表的关联查询,并通过使用PageHelper.startPage方法设置分页参数。例如: ```java @Select("SELECT a.id, a.name, b.age FROM table_a a LEFT JOIN table_b b ON a.id = b.a_id") List<YourResultType> selectMultiTableData(); ``` 3. 在Service层或者Controller层调用Mapper接口的相应方法,获取查询结果。例如: ```java PageHelper.startPage(pageNum, pageSize); // 设置分页参数 List<YourResultType> resultList = yourMapper.selectMultiTableData(); ``` 4. 处理分页结果。可以通过将查询结果封装到PageInfo对象中,获取分页相关信息。例如: ```java PageInfo<YourResultType> pageInfo = new PageInfo<>(resultList); long total = pageInfo.getTotal(); // 获取总记录数 List<YourResultType> data = pageInfo.getList(); // 获取当前页数据列表 ``` 这样就实现了多表查询分页的功能。可以根据具体的业务需求,调整查询语句和分页参数的设置。注意,PageHelper.startPage方法需要在实际查询之前调用,以便正确地进行分页。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值