Ethel_Pagination-----mybatis的分页插件

Ethel_Pagination

简介

Ethel是一款基于mybatis的分页插件,支持多种数据库,简单配置就可使用。先后端能够彻底分离,传递须要的参数到后台就能够,经过json与前端交互。

使用

简单配置

mybatis-config.xml添加以下代码:前端

//数据库方言选择
<properties>
	<property name="dialectClass" value="com.ethel.pagination.dialect.MySql5Dialect"/>
</properties>

//插件配置
<plugins>
    <plugin interceptor="com.ethel.pagination.dialect.mybatis.PaginationStatementHandlerInterceptor"/>
</plugins>

完整代码:git

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	
<properties>
	<property name="dialectClass" value="com.ethel.pagination.dialect.MySql5Dialect"/>
</properties>

<!-- 配置mybatis的缓存,延迟加载等等一系列属性 -->
<settings>

<!-- 全局映射器启用缓存 -->
<setting name="cacheEnabled" value="true"/>

<!-- 查询时,关闭关联对象即时加载以提升性能 -->
<setting name="lazyLoadingEnabled" value="true"/>

<!-- 对于未知的SQL查询,容许返回不一样的结果集以达到通用的效果 -->
<setting name="multipleResultSetsEnabled" value="true"/>

<!-- 容许使用列标签代替列名 -->
<setting name="useColumnLabel" value="true"/>

<!-- 不容许使用自定义的主键值(好比由程序生成的UUID 32位编码做为键值),数据表的PK生成策略将被覆盖 -->
<setting name="useGeneratedKeys" value="false"/>

<!-- 给予被嵌套的resultMap以字段-属性的映射支持 FULL,PARTIAL -->
<setting name="autoMappingBehavior" value="PARTIAL"/>

<!-- 对于批量更新操做缓存SQL以提升性能 BATCH,SIMPLE -->
<setting name="defaultExecutorType" value="SIMPLE" />

<!-- 数据库超过25000秒仍未响应则超时 -->
<!-- <setting name="defaultStatementTimeout" value="25000" /> -->

<!-- Allows using RowBounds on nested statements -->
<setting name="safeRowBoundsEnabled" value="false"/>

<!-- Enables automatic mapping from classic database column names A_COLUMN to camel case classic Java property names aColumn. -->
<setting name="mapUnderscoreToCamelCase" value="true"/>

<!-- MyBatis uses local cache to prevent circular references and speed up repeated nested queries. By default (SESSION) all queries executed during a session are cached. If localCacheScope=STATEMENT 
local session will be used just for statement execution, no data will be shared between two different calls to the same SqlSession. -->
<setting name="localCacheScope" value="SESSION"/>

<!-- Specifies the JDBC type for null values when no specific JDBC type was provided for the parameter. Some drivers require specifying the column JDBC type but others work with generic values 
like NULL, VARCHAR or OTHER. -->
<setting name="jdbcTypeForNull" value="OTHER"/>

<!-- Specifies which Object's methods trigger a lazy load -->
<setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>

<!-- 设置关联对象加载的形态,此处为按需加载字段(加载字段由SQL指 定),不会加载关联表的全部字段,以提升性能 -->
<setting name="aggressiveLazyLoading" value="false"/>

</settings>

<typeAliases>
<package name="com.ethel.pagination.*.po"/>
</typeAliases>

<plugins>
	<plugin interceptor="com.ethel.pagination.dialect.mybatis.PaginationStatementHandlerInterceptor"/>
</plugins>

</configuration>

简单使用

说明:能够用内部提供的com.ethel.pagination.dialect.mybatis.RspPage做为返回实体,也能够本身定义。 示例代码:github

//controller
@RestController
public class RestDataController {

	[@Resource](https://my.oschina.net/u/929718)
	private PropertyService propertyService;
	
	@RequestMapping("/dataList")
	public RspPage<Property> list(Integer pageIndex, Integer pageSize){
		if(null == pageIndex){
		pageIndex = 1; //默认从第一页开始查
	}
		pageIndex = pageIndex + 1; //dataTable插件默认传递的是pageIndex是0,须要加1,我前端用的datatable
	if(null == pageSize){
		pageSize = 10; //一页10条数据
	}
	//返回数据
		RspPage<Property> pages = propertyService.queryList(pageIndex,pageSize);
		return pages;
	}
}

//service
public RspPage<Property> queryList(Integer pageNo, Integer pageSize) {
	//分页对象
	Page<Property> page = new Page<Property>(pageNo,pageSize);
	List<Property> list = propertyMapper.queryList(page);
	//分页数据返回
	RspPage<Property> rspPage = new RspPage<Property>();
	rspPage.setRows(list);
	rspPage.setTotal(page.getTotalCount());
	rspPage.setTotalPages(page.getTotalPages());
	return rspPage;
}

//dao
List<Property> queryList(Page<Property> page);

mapper文件查询语句:sql

<select id="queryList" resultMap="BaseResultMap">
	select 
	<include refid="Base_Column_List" />
	from loupan
</select>

你会发现这个sql中并无分页参数,插件帮你作了这个事情,是否是很清爽的sql啊。数据库

相关链接:json

  1. mybatis:https://github.com/mybatis
  2. mybatis blog:http://blog.mybatis.org
相关文章
相关标签/搜索