1.工程搭建
- 导入依赖
<!--jdbc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--加载springboot-web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--数据库-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!-- 通用mapper -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
<!--thymeleaf-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- druid连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!--属性注入bean-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
yml配置
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/demo
username: root
password: 1234
thymeleaf:
cache: false #关闭缓存
#mybatis配置
mybatis:
#输出sql
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: mapper/*.xml #扫描mapper.xml文件
type-aliases-package: com.whpu.pojo #javabean别名包扫描
pagehelper:
helper-dialect: mysql
reasonable: true
support-methods-arguments: true
params: count=countSql
2. 业务代码
public interface UserMapperT extends Mapper<UserT> {
//继承通用mapper
//此处不需要任何方法
}
user类
@Data
@Table(name="user") //指向数据库表名
public class UserT {
@Id
@KeySql(useGeneratedKeys = true)//表示在insert语句中,可将自动生成的主键id返回
@GeneratedValue
private Integer id;
private String username;
private String password;
}
3.测试代码
@SpringBootTest
class Springboot06ApplicationTests {
@Autowired
private UserMapperT userMapperT;
@Test
void test2() {
//执行分页,pageNum: 当前页 , pageSize :每页显示数据条数
PageHelper.startPage(1, 5);
List<UserT> user=userMapperT.selectAll();
//查询结果封装到pageinfo对象
PageInfo<UserT> pageInfo =new PageInfo<>(user);
System.out.println(pageInfo);
}
}
- 输出结果
4.后台代码
@Autowired
private UserMapperT userMapperT;
@RequestMapping("lookuser")
public ModelAndView lookuser(ModelAndView mv,
@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum){
//分页
PageHelper.startPage(pageNum,5);
//查询
List<UserT> user=userMapperT.selectAll();
//查询结果封装到pageinfo对象
PageInfo<UserT> pageInfo =new PageInfo<>(user);
mv.addObject("user",pageInfo);
mv.setViewName("/select");
return mv;
}
其中:PageHelper.startPage(int PageNum,int PageSize):用来设置页面的位置和展示的数据条目数,我们设置每页展示5条数据。PageInfo用来封装页面信息,返回给前台界面。PageInfo中的一些我们需要用到的参数如下表:
PageInfo.list | 结果集 |
PageInfo.pageNum | 当前页码 |
PageInfo.pageSize | 当前页面显示的数据条目 |
PageInfo.pages | 总页数 |
PageInfo.total | 数据的总条目数 |
PageInfo.prePage | 上一页 |
PageInfo.nextPage | 下一页 |
PageInfo.isFirstPage | 是否为第一页 |
PageInfo.isLastPage | 是否为最后一页 |
PageInfo.hasPreviousPage | 是否有上一页 |
PageHelper.hasNextPage | 是否有下一页 |
5.前端代码
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" th:href="@{/amazeui/assets/css/amazeui.css}">
<script th:src="@{/amazeui/assets/js/jquery.min.js}"></script>
<script th:src="@{/amazeui/assets/js/amazeui.js}"></script>
<script th:src="@{/amazeui/pagination/amazeui-pagination.js}"></script>
</head>
<body>
<div class="am-g">
<div class="am-u-sm-5">
<table class="am-table am-table-bordered am-table-striped am-table-centered am-table-hover am-table-compact">
<h3 style="margin-bottom: 0px;margin-top: 30px">用户信息</h3>
<tr>
<td colspan ="4" >
<a class="am-btn am-btn-primary" style="float: right " th:href="@{/addtk(flag=1)}">添加</a>
</td>
</tr>
<tr>
<th>编号</th>
<th>账号</th>
<th>密码</th>
<th>操作</th>
</tr>
<!-- 遍历结果集list-->
<tr th:each="u:${user.list}" >
<td th:text="${u.id}"></td>
<td th:text="${u.username}"></td>
<td th:text="${u.password}"></td>
<td ><a class="am-btn am-btn-success" th:href="@{/updatetk(flag=1,id=${u.id})}">修改</a><a class="am-btn am-btn-danger" th:href="@{/deletetk(id=${u.id})}">删除</a></td>
</tr>
</table>
<p>当前<span th:text="${user.pageNum}"></span>页,总<span th:text="${user.pages}"> </span>页,共<span th:text="${user.total}"></span>条数据</p>
<a th:href="@{/lookuser}">首页</a>
<a th:href="@{/lookuser(pageNum=${user.hasPreviousPage}?${user.prePage}:1)}">上一页</a>
<a th:href="@{/lookuser(pageNum=${user.hasNextPage}?${user.nextPage}:${user.pages})}">下一页</a>
<a th:href="@{/lookuser(pageNum=${user.pages})}">尾页</a>
</div>
</div>
</body>
</html>
- 结果