zoukankan      html  css  js  c++  java
  • Mybatis分页插件

    1.分页插件的方案?

    可以使用mybatis的拦截器机制,在SQL执行前做一个拦截,然后对SQL语句加上limit ,这样所有需要分页的SQL就自动实现分页逻辑了。

    2,拦截器实现分页的原理图:

    对于以上原理图,主要就是基于拦截器机制,在执行sql语句之前,进行拦截,在你需要查询的语句前,加上limit ?,?

    3.案例

    需求:客户端传来两个参数:page,rows,实现分页

    返回的是数据的总数和查询的数据

    1)请求路径:  GET请求

    2)请求参数:page(当前页码),rows(每页条数)

    3)响应结果:必须包含两个内容:总条数xx.getTotal()  ,数据集合xx.getList()

     3.1添加依赖

        <!-- 分页助手 -->
            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper</artifactId>
                <version>3.7.5</version>
            </dependency>
            <dependency>
                <groupId>com.github.jsqlparser</groupId>
                <artifactId>jsqlparser</artifactId>
                <version>0.9.1</version>
            </dependency>

    3.2定义Mybatis核心配置文件添加插件

        <plugins>
            <!-- 配置分页助手的插件 -->
            <plugin interceptor="com.github.pagehelper.PageHelper">
                <!-- 指定数据库方言 -->
                <property name="dialect" value="mysql"/>
                <!-- 设置为true时,查询结果中会查询出总条数信息 -->
                <property name="rowBoundsWithCount" value="true"/>
            </plugin>
        </plugins>

    3.3定义controller类

    /*
         * 关于分页的参数 :
         * 异步
         * 有参page,rows
         * 有返回值List<User>,total
         * */
        
        @RequestMapping(value="list",method=RequestMethod.POST)
        public ResponseEntity<PageResult> queryUserList(@RequestParam("page")Integer page,@RequestParam("rows")Integer rows){
            
            try {
                PageInfo<User> pageInfo = userService.queryUserList(page, rows);
                
                PageResult pageResult = new PageResult(pageInfo.getList(),pageInfo.getTotal());
                
                return ResponseEntity.status(HttpStatus.OK).body(pageResult);
            } catch (Exception e) {
                e.printStackTrace();
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
            }
        }

    3.4.调用service层

    @Service
    public class UserServiceImpl implements UserService{
    
        @Autowired
        private UserMapper userMapper;
    
        @Override
        public PageInfo<User> queryUserList(Integer page, Integer rows) {
    
            PageHelper.startPage(page, rows);
            
            List<User> list = this.userMapper.select(null);
            
            PageInfo<User> pageInfo = new PageInfo<User>(list);
            
            return pageInfo;
        }

    3.5,因为单表的操作,这里使用通用Mapper类。调用dao

    编写接口继承Mapper即可

  • 相关阅读:
    【Educational Codeforces Round 101 (Rated for Div. 2) C】Building a Fence
    【Codeforces Round #698 (Div. 2) C】Nezzar and Symmetric Array
    【Codeforces Round #696 (Div. 2) D】Cleaning
    【Codeforces Round #696 (Div. 2) C】Array Destruction
    【Educational Codeforces Round 102 D】Program
    【Educational Codeforces Round 102 C】No More Inversions
    【Good Bye 2020 G】Song of the Sirens
    【Good Bye 2020 F】Euclid's nightmare
    使用mobx入门
    requestAnimationFrame 控制速度模拟setinterval
  • 原文地址:https://www.cnblogs.com/lichangyun/p/8542307.html
Copyright © 2011-2022 走看看