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
相關文章
相關標籤/搜索