mybatis-plus分页查询

<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>

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值