1、利用BootstrapTable插件进行数据展示,指定sidePagination=‘server’之后,需要从后端进行分页,在请求‘information/getAllNews’的时候,会传递两个参数offset和limit,后台根据这两个参数进行分页查询。
$('#tableNews').bootstrapTable({
type: 'GET',
url: '<%=request.getContextPath()%>/information/getAllNews',
striped: 'true',
cache: false,
sidePagination: 'server',
pageSize: 10,
pageList: [10, 20, 50],
pageNumber: 1,
pagination: true,
columns: [
{
title: '序号',
formatter:function(value,row,index){
return index+1;
}
},
{
title: '标题',
formatter:function(value,row,index){
return '<a href="<%=request.getContextPath()%>/information/toNewsCenterDetails?serial='+row.serial+'">' +row.infotopic+'</a>';
}
},
{
field: 'createdate',
title: '时间',
formatter: function (value, row, index) {
return changeDateFormat(value)
}
}
]
});
2、后端配置
(1)pom.xml:
<!--分页器pagehelper-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.0.0</version>
</dependency>
(2)spring-mybatis.xml:
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" >
<list>
<value>classpath:com/wapssei/mapper/*.xml</value>
</list>
</property>
<!--分页-->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties" >
<value >
<!--分页插件会自动检测当前的数据库链接,自动选择合适的分页方式。 你可以配置helperDialect属性来指定分页插件使用哪种方言。配置时,可以使用下面的缩写值:-->
<!--oracle,mysql,mariadb,sqlite,hsqldb,postgresql,db2,sqlserver,informix,h2,sqlserver2012,derby-->
<!--特别注意:使用 SqlServer2012 数据库时,需要手动指定为 sqlserver2012,否则会使用 SqlServer2005 的方式进行分页。-->
helperDialect=sqlserver2012
<!--reasonable,默认为false。为 true 时,pageNum<=0 时查询第一页, pageNum>pages(超过总数时),查询最后一页。-->
<!--当 offsetAsPageNum=false 的时候,由于 PageNum 问题,RowBounds查询的时候 reasonable 会强制为 false。使用 PageHelper.startPage 方法不受影响。-->
reasonable=true
<!--supportMethodsArguments,支持通过 Mapper 接口参数来传递分页参数,默认值false,分页插件会从查询方法的参数值中,自动根据上面 params 配置的字段中取值,
查找到合适的值时就会自动分页。 使用方法可以参考测试代码中的 com.github.pagehelper.test.basic 包下的 ArgumentsMapTest 和ArgumentsObjTest。-->
supportMethodsArguments=true
<!--params:为了支持startPage(Object params)方法,增加了该参数来配置参数映射,用于从对象中根据属性名取值, 可以配置 pageNum,pageSize,count,pageSizeZero,reasonable,不配置映射的用默认值, 默认值为pageNum=pageNum;pageSize=pageSize;count=countSql;reasonable=reasonable;pageSizeZero=pageSizeZero。-->
params=count=countSql
<!--autoRuntimeDialect:默认值为 false。 为 true时,这样在使用不同数据源时,会使用匹配的分页进行查询。 这种情况下,你还需要特别注意 closeConn 参数,由于获取数据源类型会获取一个数据库连接,所以需要通过这个参数来控制获取连接后,是否关闭该连接。 默认为 true,有些数据库连接关闭后就没法进行后续的数据库操作。而有些数据库连接不关闭就会很快由于连接数用完而导致数据库无响应。所以在使用该功能时,特别需要注意你使用的数据源是否需要关闭数据库连接。-->
autoRuntimeDialect=true
<!--closeConn:默认值为 true。当使用运行时动态数据源或没有设置 helperDialect 属性自动获取数据库类型时,会自动获取一个数据库连接, 通过该属性来设置是否关闭获取的这个连接,默认true关闭,设置为 false 后,不会关闭获取的连接,这个参数的设置要根据自己选择的数据源来决定。-->
closeConn=true
<!--rowBoundsWithCount:默认值为false,该参数对使用 RowBounds 作为分页参数时有效。 当该参数设置为true时,使用 RowBounds 分页会进行 count 查询。-->
rowBoundsWithCount=true
<!--offsetAsPageNum:默认值为 false,该参数对使用 RowBounds(offset、limit) 作为分页参数时有效。 当该参数设置为 true 时,offset会当成 pageNum 使用,limit 和 pageSize 含义相同。-->
offsetAsPageNum=false
<!--pageSizeZero:默认值为 false,当该参数设置为 true 时,如果 pageSize=0 或者 RowBounds.limit = 0 就会查询出全部的结果(相当于没有执行分页查询,但是返回结果仍然是 Page 类型)。-->
pageSizeZero=true
</value>
</property>
</bean>
</array>
</property>
</bean>
(3)封装后台返回的数据格式,用于在BootstrapTable中显示:
public class OffsetLimitPage {
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;
}
public OffsetLimitPage() {
}
public OffsetLimitPage(List rows, Long total) {
this.rows = rows;
this.total = total;
}
public OffsetLimitPage(Page rows) {
this(rows,rows.getTotal());
}
}
(4)Controller:
@ResponseBody
@RequestMapping(value = "getAllNews")
public OffsetLimitPage getAllNews(HttpServletRequest request, HttpServletResponse response, Model model,Integer offset,Integer limit){
OffsetLimitPage result = newsInfoService.getAllNews(offset,limit);
return result;
}
(5)Service.java:
public abstract OffsetLimitPage getAllNews(Integer offset, Integer limit);
(6)ServiceImpl.java:
public OffsetLimitPage getAllNews(Integer offset, Integer limit){
PageHelper.offsetPage(offset, limit);
List<NewsInfo> newsInfoList = newsInfoMapper.selectAllNews();
return new OffsetLimitPage((Page)newsInfoList);
}
(7)Mapper.java:(这里不需要offset和limit参数)
List<NewsInfo> selectAllNews();
(8)Mapper.xml:(这里不需要offset和limit参数)
<select id="selectAllNews" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from Info where Type = 14 ORDER BY CreateDate DESC </select>
3、运行结果:

来源:oschina
链接:https://my.oschina.net/u/2519523/blog/1863502
