zoukankan      html  css  js  c++  java
  • mybatis-plus分页查询数据库数据

    在springboot中整合mybatis-plus

    <!-- 引入mybatisPlus -->
           <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>3.2.0</version>
            </dependency>
            <!-- 引入mysql驱动包 -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.27</version>
            </dependency>
            <!-- 引入Druid依赖,阿里巴巴所提供的数据源 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.0.29</version>
        </dependency>

    2 在application.yml配置

    spring:
      datasource:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8
        username: root
        password: 123456

    3 在启动类上面添加@MapperScan注解,扫描mapper包

    @MapperScan("com.atguigu.admin.mapper")

    4 新建User和UserMapper

    import com.baomidou.mybatisplus.annotation.TableField;
    import com.baomidou.mybatisplus.annotation.TableName;
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    @AllArgsConstructor
    @NoArgsConstructor
    @Data
    @TableName("user")
    public class User {
        //非数据库中的数据
        @TableField(exist = false)
        private String userName;
        @TableField(exist = false)
        private String password;
        
        //数据库中的数据
        private int id;
        private String name;
        private String age;
        private String email;
    }
    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    import com.fxz.admin.bean.User;
    import org.apache.ibatis.annotations.Mapper;
    
    @Mapper
    public interface UserMapper extends BaseMapper<User> { //mapper继承父类获取操作方法 泛型为数据库数据对应的类 无需配置xml映射文件
    }

    5  Service

    import com.baomidou.mybatisplus.extension.service.IService;
    import com.fxz.admin.bean.User;
    
    public interface UserService extends IService<User> {
    }
    //继承父类 泛型为对应mapper和操作对象
    @Service
    public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {
    }

    6 分页查询拦截器

    @Configuration
    public class MybatisConfig { //mybatis分页查询必须配置分页拦截器
    
         @Bean
         public PaginationInterceptor paginationInterceptor() {
            PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
            return paginationInterceptor;
         }
    }

    7 前端控制器 Controller

     @GetMapping(value = "/dynamic_table") //拦截请求
        public String dynamic(Model model,@RequestParam(value = "pn",defaultValue = "1")int pn){
            //分页参数
            Page<User> page = new Page(pn,2);
            //调用page分页
            Page<User> users = userService.page(page);
            model.addAttribute("users",users);
            return "table/dynamic_table";
        }

    8 前端页面展示数据

    <tr class="gradeX" th:each="user: ${users.records}"> <!--这里按照取得的记录数遍历-->
                <td th:text="${user.id}"></td>
                <td th:text="${user.name}"></td>
                <td th:text="${user.age}"></td>
                <td th:text="${user.email}"></td>
     </tr>
    </div>
                <div class="row-fluid">
                    <div class="span6">
                        <div class="dataTables_info" id="dynamic-table_info">  <!--分页栏 获取数据-->
                            当前第[[${users.current}]]页  总计 [[${users.pages}]]页  共[[${users.total}]]条记录 
                        </div>
                    </div>
                    <div class="span6">
                        <div class="dataTables_paginate paging_bootstrap pagination">
                            <ul>   
                                <li class="prev disabled"><a href="#">← 前一页</a></li>           <!---生成数字序列-->
                                <li th:class="${num == users.current?'active':''}" th:each="num:${#numbers.sequence(1,users.pages)}" >
                                    <a th:href="@{/dynamic_table(pn=${num})}">[[${num}]]</a>
                                </li>
                                <li class="next disabled"><a href="#">下一页 → </a></li>
                            </ul>
                        </div>
                    </div>
                </div> </div>
  • 相关阅读:
    Flash & Flex组件优化的杀手锏callLater
    35个设计一流的美味的水果壁纸欣赏
    Web开发人员有用的代码比较工具
    25个强大的CSS代码,据说这些是开发者经常遇到比较棘手的代码
    跨浏览器兼容性检查的最佳工具
    30个景观网页设计举例
    对pg_buffercache 的利用实验
    如何跳到vi 的最后一行
    PostgreSQL 在何处真正开始写数据
    从源代码上理解 PostgreSQL 的 bgwriter_lru_maxpages
  • 原文地址:https://www.cnblogs.com/fxzemmm/p/14342259.html
Copyright © 2011-2022 走看看