<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.1</version> </dependency>
Mapper接口继承BeanMapper类,泛型是要操作的实体类的类型 public interface UserMapper extends BaseMapper<User> { //什么方法都不需要写,它已经帮我们写好了 }
//service接口继承IService类,泛型是要操作的实体类的类型 public interface UserService extends IService<User> { }
//service实现类实现UserService接口,并且继承ServiceImpl,他需要两个泛型,一个是Mapper接口的类型,还有一个是操作实体类的类型
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {
}
//
@RequestMapping("/editable_table")
public String editableTable(Model model,
@RequestParam(value="pn",defaultValue = "1") Integer pn){
//分页查询数据
//这个page表示当前从第几页开始查,每页显示多少条
Page<User> pageNoPageSize=new Page<User>(pn,3);
//调用page方法,传入page对象,条件为空则传null
Page<User> page = userService.page(pageNoPageSize, null);
//获取总页码
long pages = page.getPages();
//获取当前页
long current = page.getCurrent();
//获取总记录数
long total = page.getTotal();
//获取查询出来的对象
List<User> records = page.getRecords();
//也可以将page传入model中,直接点属性调用
model.addAttribute("page",page);
return "table/editable_table";
}
HTML基于thymeleaf
//page.getRecords获取查询到的所有对象
<tr th:each="user,stat:${page.getRecords}" class="">
<td th:text="${stat.count}">Jonathan</td>
<td th:text="${user.name}">Smith</td>
<td th:text="${user.age}">3455</td>
<td th:text="${user.email}" class="center">Lorem ipsume</td>
<td><a class="btn btn-danger btn-sm" th:type="button" th:href="@{/deleteUser(id=${user.id},pn=${page.getCurrent})}">删除</a></td>
</tr>
<table class="table table-striped table-hover table-bordered" id="editable-sample">
<thead>
<tr>
<th>ID</th>
<th>名字</th>
<th>年龄</th>
<th>邮箱</th>
</tr>
</thead>
<tbody>
<tr th:each="user,stat:${page.getRecords}" class="">
<td th:text="${stat.count}">Jonathan</td>
<td th:text="${user.name}">Smith</td>
<td th:text="${user.age}">3455</td>
<td th:text="${user.email}" class="center">Lorem ipsume</td>
<!-- <td><a class="btn btn-primary btn-lg" th:type="button" th:href="">修改</a></td>-->
<td><a class="btn btn-danger btn-sm" th:type="button" th:href="@{/deleteUser(id=${user.id},pn=${page.getCurrent})}">删除</a></td>
</tr>
</tbody>
</table>
<div class="row-fluid">
<div class="span6">
<div class="dataTables_info" id="dynamic-table_info">
当前第 [[${page.getCurrent}]] 页 总计 [[${page.getPages}]] 页 共 [[${page.getTotal}]] 条数据
</div>
</div>
<div class="span6">
<div class="dataTables_paginate paging_bootstrap pagination">
<ul>
<li class="pre disabled">
<!-- <a th:href="@{/editable_table(pn=(${num}+1))}"> <- Previous </a>-->
</li>
<!--点到哪张卡片上,哪张就显示高亮-->
<li th:class="${num==page.getCurrent ?'active':''}"
th:each="num:${#numbers.sequence(1,page.getPages)}"><!--显示多少张卡片-->
<!--请求路径中的方法赋值,赋值当前num,相当于点哪张,就显示哪页-->
<a th:href="@{/editable_table(pn=${num})}" th:text="${num}" ></a>
</li>
<!-- <li class="next"><a th:href="@{/editable_table(pn=(${num}-1))}">Next -></a></li>-->
</ul>
</div>
</div>
</div>