分页操作
1、加入依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
2、Mybatis-Plus工具类
@Configuration
public class MyBatisPageConfig {
@Bean
public MybatisPlusInterceptor paginationInterceptor(){
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
//这是分页拦截器
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
paginationInnerInterceptor.setOverflow(true);
paginationInnerInterceptor.setMaxLimit(500L);
mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptor);
return mybatisPlusInterceptor;
}
}
3、测试
UserMapper
package com.baidu.admin.mapper;
import com.baidu.admin.bean.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springframework.stereotype.Repository;
@Repository
public interface UserMapper extends BaseMapper<User> {
}
这里用到userService;需要创建UserService接口和实现类
注意:由于是mybatis-plus需要继承 IService
UserService
package com.baidu.admin.service;
import com.baidu.admin.bean.User;
import com.baomidou.mybatisplus.extension.service.IService;
public interface UserService extends IService<User> {
}
UserServiceImpl
package com.baidu.admin.service.impl;
import com.baidu.admin.bean.User;
import com.baidu.admin.mapper.UserMapper;
import com.baidu.admin.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}
Controller层
@Controller
public class TableController {
@Autowired
UserService userService;
@GetMapping("/dynamic_table")
public String dynamic_table(@RequestParam(value = "pn",defaultValue = "1")Integer pn, Model model){
//这里查询全部数据
List<User> list = userService.list();
//分页查询数据
Page<User> userPage = new Page<>(pn,2);
//分页查询的结果
Page<User> page = userService.page(userPage, null);
long current = page.getCurrent();
long pages = page.getPages();
long total = page.getTotal();
List<User> records = page.getRecords();
model.addAttribute("page",page);
return "table/dynamic_table";
}
}
4、编写前端
这里自己找一些前端的前端的样式;大概效果就这样
<div class="row-fluid">
<div class="span6">
<div class="dataTables_info" id="dynmaic-table_info">
当前第 [[${page.current}]] 页 总计 [[${page.pages}]] 页 共 [[${page.total}]] 条记录
</div>
</div>
<div class="span6">
<div class="dataTables_paginate paging_bootstrap pagination">
<ul>
<li class="prev"><a href="#">前一页</a></li>
<li th:class="${num == page.current?'active':''}" th:each="num:${#numbers.sequence(1,page.pages)}">
<a th:href="@{/dynamic_table(pn=${num})}">[[${num}]]</a>
</li>
<li class="next"><a href="#">下一页</a></li>
</ul>
</div>
</div>
</div>