0
点赞
收藏
分享

微信扫一扫

5分钟学会Springboot结合Mybatis Plus实现分页

祈澈菇凉 2022-02-04 阅读 83

1、导入Mybatis Plus场景

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

2、编写实体类User

@Data
@AllArgsConstructor
@NoArgsConstructor
public class  User {
    private Long id;
    private String name;
    private Integer age;
    private String address;
    private String username;
    private String password;
}

3、编写UserMapper

@Mapper
@Component
public interface UserMapper extends BaseMapper<User> {

}

        继承BaseMapper<T>类,T为泛型,需要操作的表的实体类

4、编写Service层

接口:

public interface UserService extends IService<User> {
    
}


实现类:
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}

        service接口继承IService<T> 

        实现类继承ServiceImpl<参数1, 参数2>:参数1为对应的Mapper文件,参数2为操作的表实体类

        此时的UserService已经具有了很多操作数据的方法

5、编写Controller

    @Autowired
    UserService userService;  
    @GetMapping("/dynamic_table")
    public String dynamic_table(@RequestParam(value = "pn",defaultValue = "1")Integer pn, Model model){
        //查询所有数据
        // List<User> list = userService.list();
        //model.addAttribute("users",list);
        //分页查询数据
        Page<User> userPage = new Page<User>(pn, 3);//pn是当前页码,5为每页显示的数据条数
        Page<User> page = userService.page(userPage, null);//分页查询的结果
        model.addAttribute("page",page);
        return "table/dynamic_table";
    }

        pn参数为当前页码,如果当前页码为空,就默认为1

6、配置分页插件

@Configuration
public class MyBatisConfig {

    @Bean
    public MybatisPlusInterceptor paginationInterceptor(){
        MybatisPlusInterceptor mybatisPlusInterceptor=new MybatisPlusInterceptor();

        //分页拦截器
        PaginationInnerInterceptor paginationInnerInterceptor=new PaginationInnerInterceptor();
        paginationInnerInterceptor.setOverflow(true);//到最后一页时,自动跳转到第一页  到第一页时,不会再向前
        paginationInnerInterceptor.setMaxLimit(5L);//每页最多为5条
        mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptor);

        return mybatisPlusInterceptor;
    }
}

7、前端界面

                             <div class="adv-table">
                                <table class="display table table-bordered" id="hidden-table-info">
                                    <thead>
                                    <tr>
                                        <th>#</th>
                                        <th>id</th>
                                        <th>name</th>
                                        <th>age</th>
                                        <th>address</th>
                                        <th>操作</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <tr class="gradeX" th:each="user,stat:${page.records}">
                                        <td th:text="${stat.count}"></td>
                                        <td th:text="${user.getId()}"></td>
                                        <td th:text="${user.getName()}"></td>
                                        <td th:text="${user.getAge()}"></td>
                                        <td th:text="${user.getAddress()}"></td>
                                        <td>
                                            <button class="btn btn-danger btn-sm">删除</button>
                                        </td>
                                    </tr>
                                    </tbody>
                                </table>

                                <div class="row-fluid">
                                    <div class="span6">
                                        <div class="dataTables_info" id="dynamic-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 th:href="@{/dynamic_table(pn=${page.current}-1)}">← 上一页</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 th:href="@{/dynamic_table(pn=${page.current}+1)}">下一页 → </a></li>
                                            </ul>
                                        </div>
                                    </div>
                                </div>
                            </div>

        查询的当前页的用户,在Page.records里面

8、效果展示

 

举报

相关推荐

0 条评论