bootstrap-table分页

  1. 1. 一、bootstrap-table客户端分页:
  2. 2. 二、bootstrap-table服务端分页:

一、bootstrap-table客户端分页:

​ 数据库查询所有的数据,在前端进行分页

​ 直接返回List<> 类型的数据即可 (rows: [{},{},,])

二、bootstrap-table服务端分页:

​ 每次只查询当前页面加载所需要的那几条数据, 从数据库返回

数据要这种json格式:

代码示例:

  1. 用来接收客户端发来的分页请求(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......

}

  1. 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.....

}

  1. controller层
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* 查询所有的图书信息 服务器端分页
* @return
*/
@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;
}
  1. 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();
}
  1. 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>
<!-- <mapper resource="com/lan/dao/BookMapper.xml"/>-->
<package name="com.lan.dao"/>
</mappers>


</configuration>
  1. 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">

<!-- 配置整合mybatis -->
<!-- 1.关联数据库文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>

<!-- 2.数据库连接池 -->
<!--数据库连接池
dbcp 半自动化操作 不能自动连接
c3p0 自动化操作(自动的加载配置文件 并且设置到对象里面)
-->
<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}"/>

<!-- c3p0连接池的私有属性 -->
<property name="maxPoolSize" value="30"/>
<property name="minPoolSize" value="10"/>
<!-- 关闭连接后不自动commit -->
<property name="autoCommitOnClose" value="false"/>
<!-- 获取连接超时时间 -->
<property name="checkoutTimeout" value="10000"/>
<!-- 当获取连接失败重试次数 -->
<property name="acquireRetryAttempts" value="2"/>
<property name="initialPoolSize" value="10"/>
</bean>

<!-- 3.配置SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>

<!-- 4.配置扫描Dao接口包,动态实现Dao接口注入到spring容器中 -->
<!--解释 :https://www.cnblogs.com/jpfss/p/7799806.html-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 给出需要扫描Dao接口包 -->
<property name="basePackage" value="com.lan.dao"/>
</bean>


</beans>