spring boot + thymeleaf 实现自定义分页

1.引入MybatisPlus依赖

<dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.2.0</version>
        </dependency>

1.1 添加分页配置

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@EnableTransactionManagement
@Configuration
public class MybatisPlusConfig {

    /**
     * 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}

2. 后台接口

2.1 BaseMapper

import com.baomidou.mybatisplus.core.mapper.BaseMapper;


public interface IBaseMapper<T> extends BaseMapper<T> {
}

2.2. 使用的mapper继承mapper

import com.bicon.problemtrack.model.entity.Problemtrack;

public interface ProblemTrackMapper extends IBaseMapper<Problemtrack>{

}

2.3 BaseService


import com.baomidou.mybatisplus.extension.service.IService;

public interface IBaseService<T> extends IService<T> {
}

2.4使用的service 继承BaseService

public interface IproblemTrackService extends IBaseService<Problemtrack> {

}

2.5 Controller

我添加了搜索功能 所以需要添加参数

/**
     *  分页
     * @param current 当前页数
     * @param searchtime 时间
     * @param state  状态
     * @param map
     * @return
     * @throws Exception
     */
    @GetMapping("/problemlist")
    public String problemList(@RequestParam(value = "current") int current,
                              @RequestParam(value = "searchtime", required = false) String searchtime,
                              @RequestParam(value = "state", required = false) String state,
                              Map<String, Object>map) throws Exception{
        QueryWrapper<Problemtrack> problemtrackQueryWrapper = new QueryWrapper<>();
        Page<Problemtrack> page = new Page<Problemtrack>(current, 1);

        if (state != null && !state.equals("null") && !state.equals("2")) {
            problemtrackQueryWrapper.eq("state", state);
        }

        if (searchtime != null && searchtime.length() != 0 && !searchtime.equals("null")) {
            String start = searchtime + " 00:00:00";
            String end = searchtime + " 23:59:59";

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            Date t = simpleDateFormat.parse(start);
            Date e = simpleDateFormat.parse(end);

            long timeStampSta = t.getTime();  // 开始时间戳
            long timeStampEnd = e.getTime();  // 结束时间戳

            problemtrackQueryWrapper.ge("create_time", timeStampSta).le("create_time", timeStampEnd);
        }

        problemtrackQueryWrapper.orderByDesc("create_time");
        IPage<Problemtrack> problemtracks = problemTrackService.page(page, problemtrackQueryWrapper);

        map.put("problemtracks", problemtracks);

        map.put("state", state);
        map.put("searchtime", searchtime);

        return "list";
    }

3 模板写法

  <nav aria-label="Page navigation example">
        <ul class="pagination paddingUL">

            <li class="page-item">
                <a class="page-link" th:href="@{/problemlist?current=} + ${problemtracks.current == 1?1:problemtracks.current-1} + '&state='+${state} + '&searchtime=' + ${searchtime}" aria-label="Previous" th:if="${problemtracks.pages > 0}">
                    <span aria-hidden="true">&laquo;</span>
                    <span class="sr-only">Previous</span>
                </a>
                <a class="page-link" href="#" aria-label="Previous" th:if="${problemtracks.pages == 0}">
                    <span aria-hidden="true">&laquo;</span>
                    <span class="sr-only">Previous</span>
                </a>
            </li>

            <th:block  th:if="${problemtracks.pages <=6}">
                <li class="page-item active" th:if="${problemtracks.pages == 0}"><a class="page-link active" href="#">1</a></li>
                <li th:class="${problemtracks.current == i? 'page-item active':'page-item'}" th:each="i:${#numbers.sequence(1,problemtracks.pages)}" th:if="${problemtracks.pages > 0}"><a class="page-link"th:href="@{/problemlist?current=} + ${i}+ '&state='+${state} + '&searchtime=' + ${searchtime}" th:text="${i}"></a></li>
            </th:block>
            <th:block  th:if="${problemtracks.pages > 6}">
                <li th:class="${problemtracks.current == 1 ?'page-item active':'page-item'}"><a  class="page-link" th:href="@{/problemlist?current=1} "  th:text="1"></a></li>
                <li class="page-item page-link" th:if="${problemtracks.current > 3}">...</li>
                <li th:class="${problemtracks.current == i? 'page-item active':'page-item'}" th:each="i:${#numbers.sequence({problemtracks.current == 1?2: (problemtracks.pages - problemtracks.current < 4 ? problemtracks.pages-4 : (problemtracks.current- 1)==1?2:problemtracks.current- 1)},
                                         {problemtracks.pages - problemtracks.current > 2 ? (problemtracks.current <= 3?5:problemtracks.pages - problemtracks.current > 3?problemtracks.current +1:problemtracks.current +2):problemtracks.pages-1})}">
                    <a class="page-link" th:href="@{/problemlist?current=} + ${i}+ '&state='+${state} + '&searchtime=' + ${searchtime}" th:text="${i}"></a></li>

                <li class="page-item page-link" th:if="${problemtracks.pages - problemtracks.current > 3}">...</li>
                <li th:class="${problemtracks.current == problemtracks.pages? 'page-item active':'page-item'}"><a  class="page-link" th:href="@{/problemlist?current=} + ${problemtracks.pages}+ '&state='+${state} + '&searchtime=' + ${searchtime}" th:text="${problemtracks.pages}"></a></li>
            </th:block>

            <li class="page-item">
                <a class="page-link" aria-label="Next" th:href="@{/problemlist?current=}+${problemtracks.current == problemtracks.pages ? problemtracks.pages : problemtracks.current + 1}+ '&state='+${state} + '&searchtime=' + ${searchtime}" th:if="${problemtracks.pages > 0}">
                    <span aria-hidden="true">&raquo;</span>
                    <span class="sr-only">Next</span>
                </a>
                <a class="page-link" aria-label="Next" href="#" th:if="${problemtracks.pages == 0}">
                    <span aria-hidden="true">&raquo;</span>
                    <span class="sr-only">Next</span>
                </a>
            </li>

        </ul>
    </nav>

4. 最终效果

在这里插入图片描述


我们各自努力, 最高处见!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值