準備工作:
web層目錄下的整合文件:mybatis-config.xml 中的配置:
<configuration>
<!-- 配置分頁插件 -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 設置數據庫類型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六種數據庫-->
<property name="dialect" value="mysql"/>
</plugin>
</plugins>
</configuration>
封裝個簡單的工具類:
public class EUDataGridResult {
private long total;
private List<?> rows;
public long getTotal() {
return total;
}
public void setTotal(long total) {
this.total = total;
}
public List<?> getRows() {
return rows;
}
public void setRows(List<?> rows) {
this.rows = rows;
}
}
mapper層的 pom.xml 中配置架包:
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.6</version>
</dependency>
好了一切準備就緒,業務層代碼(有點囉嗦,別介意哈):
//EUDataGridResult 封裝的工具類。
@Override
public EUDataGridResult selectBuildingByViews(int page, int rews) {
List<Building> list = mapper.selectBuildingByViews();//查詢所有數據
PageHelper.startPage(page, rews);//page 當前頁數,rews 頁面顯示數據的大小
PageInfo info=new PageInfo<>(list);//把查詢出的數據存入分頁插件
long total=info.getTotal();//獲取總條數,也就是查詢出的總數據有幾條
EUDataGridResult result=new EUDataGridResult();
result.setTotal(total);//顯示總頁數
result.setRows(list);//顯示根據分頁查詢出的數據
return result;//返回總條數和分頁的數據。
}
Controller控制層:
@RequestMapping(value="")//都知道怎麼寫,自己填哈。
public @ResponseBody EUDataGridResult selectBuildingByViews(int page, int rews){
return buildingService.selectBuildingByViews(page,rews);
}
以上便是全部代碼了,有點 low ,大佬們勿噴哈~