首先是springboot版本,使用的是2.1.6,随后导入pagehelper,pagehelper能找到两种,这里使用pagehelper-spring-boot-starter 1.2.5,另一个测试了一下没有生效。pagehelper使用默认配置即可使用。然后是freemarker,导入后需要设置配置文件。
完整依赖如下
<!-- freemarker -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
freemarker的配置根据网上搜到的直接用即可,需要注意的地方是这里
spring.freemarker.settings.number_format=#
如果不进行这个设置,比较大的数字会带有逗号,例如:9,999
这种格式的数字无法直接使用,需要去掉逗号,所以不要忘记在配置中加上这一条。
具体使用如下:
@RequestMapping("/show")
public String t(Map<String, Object> map,@RequestParam(value="pageNum",defaultValue="1")Integer pageNum,
@RequestParam(value="pageSize",defaultValue="3")Integer pageSize){
map.put("test", "It's a springboot integrate freemarker's demo!!!!");
PageHelper.startPage(pageNum, pageSize);
//拿到所有用户数据
List<Body> list = bodyService.findAll();
//将查询到的数据放入pagehepler中
PageInfo<Body> pageInfoUser = new PageInfo<Body>(list);
map.put("bodylist" ,pageInfoUser);
// 将查询到的数据存到返回
return "main";
}
前台页面:
<!DOCTYPE html>
<html lang="zh">
<head>
<META http-equiv=Content-Type content='text/html; charset=UTF-8'>
<title>Title</title>
<style type="text/css">
table.reference, table.tecspec {
border-collapse: collapse;
width: 100%;
margin-bottom: 4px;
margin-top: 4px;
}
table.reference tr:nth-child(even) {
background-color: #fff;
}
table.reference tr:nth-child(odd) {
background-color: #f6f4f0;
}
table.reference th {
color: #fff;
background-color: #0dcde8;
border: 1px solid #555;
font-size: 12px;
padding: 3px;
vertical-align: center;
}
table.reference td {
line-height: 2em;
min-width: 24px;
border: 1px solid #d4d4d4;
padding: 5px;
padding-top: 7px;
padding-bottom: 7px;
vertical-align: center;
text-align: center;
}
.article-body h3 {
font-size: 1.8em;
margin: 2px 0;
line-height: 1.8em;
}
.active {
font-size: 18px;
background-color: #0dcde8;
}
#nav {
height: 65px;
border-top: #060 2px solid;
border-bottom: #060 2px solid;
background-color: #8a858566;
}
#nav ul {
list-style: none;
margin-left: 50px;
}
#nav li {
display: inline;
line-height: 40px;
float: left
}
#nav a {
color: #0a0a0a;
text-decoration: none;
padding: 20px 20px;
}
#nav a:hover {
background-color: #060;
}
</style>
</head>
<body>
<div>
<table class="reference">
<tbody>
<tr>
<th>ID</th>
<th>编号</th>
<th>厂家</th>
<th>项目</th>
<th>判定</th>
<th>上月平均</th>
</tr>
${bodylist}
<#list bodylist.list as element>
<tr>
<td>
${element.id}
</td>
<td>
${element.cybh}
</td>
<td>
${element.sszzq}
</td>
<td>
${element.jyxm}
</td>
<td>
${element.jgpd}
</td>
<td>
${element.cydd1}
</td>
</tr>
</#list>
<br>
</tbody>
</table>
<div class="message">
共<i class="blue">${bodylist.total}</i>条记录,当前显示第 <i
class="blue">${bodylist.pageNum}/${bodylist.pages}</i> 页
</div>
<div style="text-align:center;" id="nav">
<ul>
<#if bodylist.isFirstPage==false>
<li><a href="./show">首页</a></li>
<li><a href="./show?pageNum=${bodylist.prePage}">上一页</a></li>
</#if>
<#list bodylist.navigatepageNums as element>
<#if element==bodylist.pageNum>
<li class="active"><a href="./show?pageNum=${element}">${element}</a></li>
</#if>
<#if element!=bodylist.pageNum>
<li><a href="./show?pageNum=${element}">${element}</a></li>
</#if>
</#list>
<#if bodylist.isLastPage==false>
<li><a href="./show?pageNum=${bodylist.nextPage}">下一页</a></li>
<li><a href="./show?pageNum=${bodylist.pages}">最后一页</a></li>
</#if>
</ul>
</div>
</div>
</body>
</html>
最终效果图