一、bootstrap-table客户端分页:
数据库查询所有的数据,在前端进行分页
直接返回List<> 类型的数据即可 (rows: [{},{},,])
二、bootstrap-table服务端分页:
每次只查询当前页面加载所需要的那几条数据, 从数据库返回
数据要这种json格式:
![](/../images/a1.png)
代码示例:
- 用来接收客户端发来的分页请求(offset,pageNumber)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| package com.lan.pojo;
public class Page { private int offset; private int pageNumber;
public Page(int pageNumber, int offset) { this.pageNumber = pageNumber; this.offset = offset; }
get/set/toString...... }
|
- controller层向客户端发送分页的json格式数据
1 2 3 4 5 6 7 8 9 10 11 12 13
| package com.lan.pojo;
public class PageBean<T> { private T rows; private long total;
public PageBean() { }
get/set/toString..... }
|
- controller层
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
@ResponseBody @GetMapping("/AllBooks") public PageBean<List<Book>> selectAllBooks(Page page) { System.out.println("page: " + page.toString()); List<Book> list = bookService.selectAllBooks(page); PageBean<List<Book>> pageBean = new PageBean<>(); int total = bookService.getTatlo(); if(list!=null) { pageBean.setRows(list); pageBean.setTotal(total); } else { System.out.println("获取图书数据失败!"); } return pageBean; }
|
- dao层使用了mybatis,在接口出写好sql语句
1 2 3 4 5 6 7 8 9 10
| @Mapper public interface BookDao { @Select("select * from book_info limit #{offset},#{pageNumber}") public List<Book> selectAllBooks(Page page);
@Select("select count(1) from book_info") public int getTatlo(); }
|
- mybatis配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <?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>
<typeAliases> <package name="com.lan.pojo"/> </typeAliases>
<mappers>
<package name="com.lan.dao"/> </mappers>
</configuration>
|
- spring-dao.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.user}"/> <property name="password" value="${jdbc.password}"/>
<property name="maxPoolSize" value="30"/> <property name="minPoolSize" value="10"/> <property name="autoCommitOnClose" value="false"/> <property name="checkoutTimeout" value="10000"/> <property name="acquireRetryAttempts" value="2"/> <property name="initialPoolSize" value="10"/> </bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <property name="basePackage" value="com.lan.dao"/> </bean>
</beans>
|