MyBatis使用(二)分页查询

由于MyBatis没有相关的分页查询,因此抽取出hibernate的分页查询功能,通过过滤器,对本地sql方言进行区分,对sql语句进行拼接和封装

 

步骤分为:1、先查询出总数

       2、通过拼接的sql再去查询相关信息

/**
	 * 带有分页信息的查询
	 * @param sqlMapId mybatis映射id
	 * @param pageRequest 分页请求参数信息
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Page findForPage(String sqlMapId, PageRequest pageRequest) {

		// 查询总数
		pageRequest.getFilters().put(SelectCountSqlInterceptor.COUNT, null); // 设置是否需要将sql转换成总数查询sql
		Number totalCount = (Number) findForObject(sqlMapId, pageRequest
				.getFilters());
		if (totalCount == null || totalCount.intValue() <= 0) {
			return new Page(pageRequest, 0);
		}
		if(totalCount != null && totalCount.intValue() <= (pageRequest.getPageNumber()-1) * pageRequest.getPageSize()){
			return new Page(pageRequest.getPageNumber(), pageRequest.getPageSize(), totalCount.intValue(), new ArrayList(0));
		}
		pageRequest.getFilters().remove(SelectCountSqlInterceptor.COUNT);

		Map filters = new HashMap();
		filters.putAll(pageRequest.getFilters());
		Page page = new Page(pageRequest, totalCount.intValue());
		List list = findForList(sqlMapId, filters, page.getFirstResult(), page.getPageSize());

		page.setResult(list);

		return page;
	}

  

/**
 * 分页请求信息
 */
public class PageRequest implements Serializable {

	private static final long serialVersionUID = 9092186838918641382L;

	/**
	 * 过滤参数
	 */
	private Map filters;

	/**
	 * 页号码,页码从1开始
	 */
	private int pageNumber;

	/**
	 * 分页大小
	 */
	private int pageSize;
}

  

/**
 * 将查询SQL转成查询总数SQL<br>
 * 
 * 配置文件内容:
 * 
 * <pre>
 * 	<plugins>
 * 		<plugin interceptor="ewell.nis.common.ibatis.plugin.SelectCountSqlInterceptor"/>
 * 	</plugins>
 * </pre>
 */
@Intercepts( { @Signature(type = Executor.class, method = "query", args = {
		MappedStatement.class, Object.class, RowBounds.class,
		ResultHandler.class }) })
public class SelectCountSqlInterceptor implements Interceptor {

	public static String COUNT = "_count";

	private static int MAPPED_STATEMENT_INDEX = 0;

	private static int PARAMETER_INDEX = 1;

	@Override
	public Object intercept(Invocation invocation) throws Throwable {

		processCountSql(invocation.getArgs());

		return invocation.proceed();
	}

	private void processCountSql(final Object[] queryArgs) {

		if (queryArgs[PARAMETER_INDEX] instanceof Map) {
			Map parameter = (Map) queryArgs[PARAMETER_INDEX];
			if (parameter.containsKey(COUNT)) {
				MappedStatement ms = (MappedStatement) queryArgs[MAPPED_STATEMENT_INDEX];
				BoundSql boundSql = ms.getBoundSql(parameter);
				String sql = ms.getBoundSql(parameter).getSql().trim();
				BoundSql newBoundSql = new BoundSql(ms.getConfiguration(),
						getCountSQL(sql), boundSql.getParameterMappings(),
						boundSql.getParameterObject());
				MappedStatement newMs = copyFromMappedStatement(ms,
						new BoundSqlSqlSource(newBoundSql));
				queryArgs[MAPPED_STATEMENT_INDEX] = newMs;
			}
		}
	}

	// see: MapperBuilderAssistant
	private MappedStatement copyFromMappedStatement(MappedStatement ms,
			SqlSource newSqlSource) {

		Builder builder = new MappedStatement.Builder(ms.getConfiguration(), ms
				.getId(), newSqlSource, ms.getSqlCommandType());

		builder.resource(ms.getResource());
		builder.fetchSize(ms.getFetchSize());
		builder.statementType(ms.getStatementType());
		builder.keyGenerator(ms.getKeyGenerator());
		builder.keyProperty(getKeyProperty(ms.getKeyProperties()));

		// setStatementTimeout()
		builder.timeout(ms.getTimeout());

		// setParameterMap()
		builder.parameterMap(ms.getParameterMap());

		// setStatementResultMap()
		List<ResultMap> resultMaps = new ArrayList<ResultMap>();
		String id = "-inline";
		if (ms.getResultMaps() != null) {
			id = ms.getResultMaps().get(0).getId() + "-inline";
		}
		ResultMap resultMap = new ResultMap.Builder(null, id, Long.class,
				new ArrayList()).build();
		resultMaps.add(resultMap);
		builder.resultMaps(resultMaps);
		builder.resultSetType(ms.getResultSetType());

		// setStatementCache()
		builder.cache(ms.getCache());
		builder.flushCacheRequired(ms.isFlushCacheRequired());
		builder.useCache(ms.isUseCache());

		return builder.build();
	}

	private String getKeyProperty(String[] keyProperties) {

		StringBuilder builder = new StringBuilder();
		if (keyProperties != null && keyProperties.length > 0) {
			int length = keyProperties.length;
			for (int i = 0; i < length; i++) {
				builder.append(keyProperties[i]);
				if (i < length -1) {
					builder.append(",");
				}
			}
			return builder.toString();
		}
		
		return null;
	}

	private String getCountSQL(String sql) {

		String lowerCaseSQL = sql.toLowerCase().replace("\n", " ").replace(
				"\t", " ");
		int index = lowerCaseSQL.indexOf(" order ");

		if (index != -1) {
			sql = sql.substring(0, index);
		}

		if (lowerCaseSQL.indexOf(" group ") != -1) {
			return "SELECT COUNT(*) FROM (SELECT COUNT(*) AS COUNT_" + sql.substring(lowerCaseSQL.indexOf(" from ")) + ") TABLE_";
		}
		
		return "SELECT COUNT(*) AS COUNT_" + sql.substring(lowerCaseSQL.indexOf(" from "));
	}

	@Override
	public Object plugin(Object target) {

		return Plugin.wrap(target, this);
	}

	@Override
	public void setProperties(Properties properties) {

	}
}

  

/**
 * 为ibatis3提供基于方言(Dialect)的分页查询的插件<br>
 * 
 * 将拦截Executor.query()方法实现分页方言的插入<br>
 * 
 * 配置文件内容:
 * 
 * <pre>
 * 	<plugins>
 * 	<plugin interceptor="ewell.nis.common.ibatis.plugin.OffsetLimitInterceptor">
 * 		<property name="dialectClass" value="ewell.nis.common.ibatis.dialect.MySQLDialect"/>
 * 	</plugin>
 * </plugins>
 * </pre>
 * 
 * 
 */

@Intercepts( { @Signature(type = Executor.class, method = "query", args = {
		MappedStatement.class, Object.class, RowBounds.class,
		ResultHandler.class }) })
public class OffsetLimitInterceptor implements Interceptor {

	private static int MAPPED_STATEMENT_INDEX = 0;

	private static int PARAMETER_INDEX = 1;

	private static int ROWBOUNDS_INDEX = 2;

	private Dialect dialect;

	@Override
	public Object intercept(Invocation invocation) throws Throwable {

		processIntercept(invocation.getArgs());
		return invocation.proceed();
	}

	void processIntercept(final Object[] queryArgs) {

		// queryArgs = query(MappedStatement ms, Object parameter, RowBounds
		// rowBounds, ResultHandler resultHandler)
		MappedStatement ms = (MappedStatement) queryArgs[MAPPED_STATEMENT_INDEX];
		Object parameter = queryArgs[PARAMETER_INDEX];
		final RowBounds rowBounds = (RowBounds) queryArgs[ROWBOUNDS_INDEX];
		int offset = rowBounds.getOffset();
		int limit = rowBounds.getLimit();

		if (dialect.supportsLimit()
				&& (offset != RowBounds.NO_ROW_OFFSET || limit != RowBounds.NO_ROW_LIMIT)) {
			BoundSql boundSql = ms.getBoundSql(parameter);
			String sql = boundSql.getSql().trim();
			if (dialect.supportsLimitOffset()) {
				sql = dialect.getLimitString(sql, offset, limit);
				offset = RowBounds.NO_ROW_OFFSET;
			}
			else {
				sql = dialect.getLimitString(sql, 0, offset+limit);
			}
			limit = RowBounds.NO_ROW_LIMIT;

			queryArgs[ROWBOUNDS_INDEX] = new RowBounds(offset, limit);
			BoundSql newBoundSql = new BoundSql(ms.getConfiguration(), sql,
					boundSql.getParameterMappings(), boundSql
							.getParameterObject());
			MappedStatement newMs = copyFromMappedStatement(ms,
					new BoundSqlSqlSource(newBoundSql));
			queryArgs[MAPPED_STATEMENT_INDEX] = newMs;
		}
	}

	// see: MapperBuilderAssistant
	private MappedStatement copyFromMappedStatement(MappedStatement ms,
			SqlSource newSqlSource) {

		Builder builder = new MappedStatement.Builder(ms.getConfiguration(), ms
				.getId(), newSqlSource, ms.getSqlCommandType());

		builder.resource(ms.getResource());
		builder.fetchSize(ms.getFetchSize());
		builder.statementType(ms.getStatementType());
		builder.keyGenerator(ms.getKeyGenerator());
		builder.keyProperty(getKeyProperty(ms.getKeyProperties()));

		// setStatementTimeout()
		builder.timeout(ms.getTimeout());

		// setStatementResultMap()
		builder.parameterMap(ms.getParameterMap());

		// setStatementResultMap()
		builder.resultMaps(ms.getResultMaps());
		builder.resultSetType(ms.getResultSetType());

		// setStatementCache()
		builder.cache(ms.getCache());
		builder.flushCacheRequired(ms.isFlushCacheRequired());
		builder.useCache(ms.isUseCache());

		return builder.build();
	}

	public Object plugin(Object target) {

		return Plugin.wrap(target, this);
	}

	public void setProperties(Properties properties) {

		String dialectClass = new PropertiesHelper(properties)
				.getRequiredString("dialectClass");
		try {
			dialect = (Dialect) Class.forName(dialectClass).newInstance();
		} catch (Exception e) {
			throw new RuntimeException(
					"cannot create dialect instance by dialectClass:"
							+ dialectClass, e);
		}
		System.out.println(OffsetLimitInterceptor.class.getSimpleName()
				+ ".dialect=" + dialectClass);
	}
	
	private String getKeyProperty(String[] keyProperties) {

		StringBuilder builder = new StringBuilder();
		if (keyProperties != null && keyProperties.length > 0) {
			int length = keyProperties.length;
			for (int i = 0; i < length; i++) {
				builder.append(keyProperties[i]);
				if (i < length -1) {
					builder.append(",");
				}
			}
			return builder.toString();
		}
		
		return null;
	}

	public static class BoundSqlSqlSource implements SqlSource {

		private BoundSql boundSql;

		public BoundSqlSqlSource(BoundSql boundSql) {

			this.boundSql = boundSql;
		}

		public BoundSql getBoundSql(Object parameterObject) {

			return boundSql;
		}
	}
}

  

  

转载于:https://www.cnblogs.com/binbang/p/4772677.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Mybatis是一款优秀的ORM框架,而PageHelper则是一款用于Mybatis的分页插件。使用PageHelper可以方便地实现分页查询功能。具体使用方法如下: 1.在pom.xml文件中添加PageHelper的依赖。 2.在Mybatis的配置文件中添加PageHelper的插件配置。 3.在Mapper接口中定义分页查询的方法,并使用PageHelper.startPage方法设置分页参数。 4.在Controller中调用Mapper接口中的分页查询方法,并将查询结果封装到PageInfo对象中返回给前端。 下面是一个示例代码: 1.在pom.xml文件中添加PageHelper的依赖: ``` <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.11</version> </dependency> ``` 2.在Mybatis的配置文件中添加PageHelper的插件配置: ``` <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <property name="helperDialect" value="mysql"/> <property name="reasonable" value="true"/> <property name="supportMethodsArguments" value="true"/> <property name="params" value="count=countSql"/> </plugin> </plugins> ``` 3.在Mapper接口中定义分页查询的方法,并使用PageHelper.startPage方法设置分页参数: ``` List<User> getUserList(@Param("pageNum") int pageNum, @Param("pageSize") int pageSize); ``` ``` PageHelper.startPage(pageNum, pageSize); List<User> userList = userMapper.getUserList(pageNum, pageSize); ``` 4.在Controller中调用Mapper接口中的分页查询方法,并将查询结果封装到PageInfo对象中返回给前端: ``` @RequestMapping("/getUserList") public PageInfo<User> getUserList(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) { List<User> userList = userMapper.getUserList(pageNum, pageSize); PageInfo<User> pageInfo = new PageInfo<>(userList); return pageInfo; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值