ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Springboot Mybatis分页插件PageHelper

2019-08-13 18:05:34  阅读:250  来源: 互联网

标签:插件 pageNum pageSize int private PageHelper Mybatis 页码 public


1,在pom.xml 导入依赖

        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.5</version>
        </dependency>

2,dao接口

public interface RoleDao {
	@Select("select * from dbrole")
	public List<Role> queryRoles();
}

3,service接口

public interface RoleService {
	public PageInfo<Role> queryRoles(Integer pageNum, Integer pageSize);
}

4,service实现类

@Service
public class RoleServiceImp implements RoleService{
	@Autowired
	private RoleDao roleDao;
	/**
	 * //PageHelper.startPage(pageNum,pageSize,"id desc");
		//第一个参数:当前页码;第二个参数:每页显示数量;第三个参数:排序(默认升序)
		//该行代码要和查询分页数据代码行一起,中间不可加入其它行代码。如下
		//PageMethod.startPage(pageNum, pageSize);
		//PageMethod和PageHelper两个类都调用的是相同的方法。
		-----------------------------------------
		//PageInfo<?> pageInfo=new PageInfo<?>(roles,6);
		//第一个参数为查询出需要分页的数据集,第二个参数为要显示导航页码数量:
		//(1)当总页码数小于填的导航页码数量时候,中间导航页码显示按照总页码数显为准
		//(2)当总页码数大于填的导航页码数时候,中间导航页码页面显示按照 自定义的导航页码数量为准。
		//例如:当总页码数量为5页(总页码数=总记录数%每页显示数量是否等0,不等,需加1),而我们定义中间导航码显示6,5<6,按页面显示5. 反之。
	 */
	@Override
	public PageInfo<Role> queryRoles(Integer pageNum, Integer pageSize) {
		//PageHelper.startPage(pageNum,pageSize,"id desc");//解释看上注释
		PageMethod.startPage(pageNum, pageSize, "id desc");
		List<Role> roles=roleDao.queryRoles();
		PageInfo<Role> pageInfo=new PageInfo<Role>(roles,6);//解释看上注释
		return pageInfo;
	}
}

5,controller

	@RequestMapping("/turnRole")
	public String turnRole(Model model,@RequestParam(value="pageNum",defaultValue = "1")
	Integer pageNum,@RequestParam(value="pageSize",defaultValue = "8")Integer pageSize) {
		PageInfo<Role> pageInfo = roleService.queryRoles(pageNum,pageSize);
		model.addAttribute("pageInfo", pageInfo);
		return "role"; 
	}

6,SpringApplication启动类 

@MapperScan(basePackages = {"com.example.demo.dao"})
@SpringBootApplication
public class RbacWebApplication {
	public static void main(String[] args) {
		SpringApplication.run(RbacWebApplication.class, args);
	}
}

7,html 显示数据部分

<thead>
	<tr>
	  <th width="30">#</th>
	  <th width="30"><input type="checkbox"></th>
	  <th width="80">编号ID</th>
	  <th>名称</th>
	</tr>
</thead>
<tbody>
	<tr th:each="info,status:${pageInfo.list}">
	  <td>[[${status.count}]]</td>
	  <td><input type="checkbox"></td>
	  <td>[[${info.id}]]</td>
	  <td>[[${info.name}]]</td>
	</tr>
</tbody>

8,html 显示页码部分

<tfoot>
	<tr>
		<td colspan="6" align="center">
			<ul class="pagination">
				<li><a href="#" th:href="'?pageNum=1'">首页</a></li>
				<li th:if="${pageInfo.hasPreviousPage}"><a th:href="'?pageNum='+${pageInfo.prePage}">上一页</a></li>
				<li th:each="num : ${pageInfo.navigatepageNums}" >
					<a href="#" th:href="'?pageNum='+${num}" th:text="${num}" th:if="${num != pageInfo.pageNum}">pageNum不相等无颜色</a>
					<span  style="font-weight:bold;color:red" th:if="${num == pageInfo.pageNum}" th:text="${num}">pageNum相等的颜色高亮</span> 
				</li>
				<li th:if="${pageInfo.hasNextPage}"><a th:href="'?pageNum='+${pageInfo.nextPage}">下一页</a></li>
				<li><a href="#"  th:href="'?pageNum='+${pageInfo.pages}">末页</a></li>
			</ul>
		</td>
	</tr>
</tfoot>

9,页面效果展示

 

10,如果需要对分页插件中的一些参数设置

 application.properties

#pagehelper
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql

application.yml

pagehelper:
    helperDialect: mysql
    reasonable: true
    supportMethodsArguments: true
    params: count=countSql

11,PageInfo 类的一些属性查看

  //当前页
    private int pageNum;
    //每页的数量
    private int pageSize;
    //当前页的数量
    private int size;

    //由于startRow和endRow不常用,这里说个具体的用法
    //可以在页面中"显示startRow到endRow 共size条数据"

    //当前页面第一个元素在数据库中的行号
    private int startRow;
    //当前页面最后一个元素在数据库中的行号
    private int endRow;
    //总页数
    private int pages;

    //前一页
    private int prePage;
    //下一页
    private int nextPage;

    //是否为第一页
    private boolean isFirstPage = false;
    //是否为最后一页
    private boolean isLastPage = false;
    //是否有前一页
    private boolean hasPreviousPage = false;
    //是否有下一页
    private boolean hasNextPage = false;
    //导航页码数
    private int navigatePages;
    //所有导航页号
    private int[] navigatepageNums;
    //导航条上的第一页
    private int navigateFirstPage;
    //导航条上的最后一页
    private int navigateLastPage;

 

标签:插件,pageNum,pageSize,int,private,PageHelper,Mybatis,页码,public
来源: https://blog.csdn.net/qq_45315910/article/details/99449359

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有