springboot + freemarker + pagehelper paginación y configuración yml

1. Spingboot2.0 se usa aquí, y hay dos versiones de pagehelper. Aquí está pagehelper 4.1.0 en lugar de pagehelper-spring-boot-starter.

<!--pagehelper相关依赖-->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>4.1.0</version>
</dependency>
<!-- freemarker -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

2. Configuración de Freemarker, hay una necesidad principal de establecer number_format a 0, de lo contrario habrá una separación de comas cuando el número sea grande, como 1,000.

spring:
	freemarker:
	    cache: false
	    suffix: .ftl
	    template-loader-path: classpath:/templates
	    content-type: text/html; charset=utf-8
	    settings:
	      number_format: 0   #数字格式,比较大的数字会带有逗号,如1,000

3. configuración de ayuda de página:

#pagehelper分页插件
pagehelper:
  helper-dialect: mysql
  reasonable: true
  support-methods-arguments: true
  params: count=countsql

4. Clase de controlador:

@RequestMapping(value = "/main")
    public String main(@RequestParam(value = "pageNum",defaultValue = "1") int pageNum, Model model) {
        int pageSize = 10; //默认分页条目10
        PageHelper.startPage(pageNum, pageSize);
        List<Article> list = articleService.selectArticles();
        PageInfo<Article> pageInfo = new PageInfo<>(list,5);
        model.addAttribute("pageInfo", pageInfo);
        return "main";
    }

5. Ingrese PageInfo para ver la definición de los parámetros en el interior, aquí principalmente ver el significado de cada variable

public class PageInfo<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    private int pageNum; //当前页
    private int pageSize;  //每一页的数量
    private int size;	//当前页的数量,比如16条记录,每页10条,那么第一页的size是10,第二页的size就是6。
    private String orderBy; 
    private int startRow;
    private int endRow;
    private long total;		//总记录数
    private int pages;
    private List<T> list;	//结果集
    private int firstPage;
    private int prePage;
    private int nextPage;
    private int lastPage;
    private boolean isFirstPage;	//是否为第一页
    private boolean isLastPage;		//是否为最后一页
    private boolean hasPreviousPage;	//是否有上一页
    private boolean hasNextPage;	//是否有下一页
    private int navigatePages;		
    private int[] navigatepageNums;		//所有导航页号

6. Portada

<#list pageInfo.list as article>
            <tr>
                <td>
                    ${article.userId}
                </td>
                <td>
                    ${article.pointCount}
                </td>
                <td>
                    ${article.typeId}
                </td>
                <td>
                    ${article.title}
                </td>
                <td>
                    ${article.simpleContent}
                </td>
                <td>
                    ${article.pageView}
                </td>

            </tr>
</#list>
<!--分页-->
                <div style="margin-top: 10px;margin-bottom: 10px;"><i class="blue">${pageInfo.total}</i>条记录,当前显示第&nbsp;<i
                            class="blue">${pageInfo.pageNum}/${pageInfo.pages}</i>&nbsp;</div>

                <div style="text-align:center;font-size: 16px;" id="nav">
                    <ul>
                        <#if pageInfo.isFirstPage==false>
                            <li><a href="/main">首页</a></li>
                            <li><a href="/main?pageNum=${pageInfo.prePage}">上一页</a></li>
                        </#if>
                        <#list pageInfo.navigatepageNums as pageNum>
                            <#if pageNum==pageInfo.pageNum>
                                <li class="active"><a href="/main?pageNum=${pageNum}">${pageNum}</a></li>
                            </#if>
                            <#if pageNum!=pageInfo.pageNum>
                                <li><a href="/main?pageNum=${pageNum}">${pageNum}</a></li>
                            </#if>
                        </#list>
                        <#if pageInfo.isLastPage==false>
                            <li><a href="/main?pageNum=${pageInfo.nextPage}">下一页</a></li>
                            <li><a href="/main?pageNum=${pageInfo.pages}">最后一页</a></li>
                        </#if>
                    </ul>
                </div>

7. El efecto es el siguiente, puedes diseñar tu propio estilo
Inserte la descripción de la imagen aquí

Publicado 25 artículos originales · elogiado 4 · visitas 1516

Supongo que te gusta

Origin blog.csdn.net/weixin_39025362/article/details/105198491
Recomendado
Clasificación