springboot中使用mybatis的分页插件pageHelper

 

 

首先在pom.xml中配置

<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.16</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.12</version>
</dependency>

 

然后就是写代码了,最重要的两个类是:

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuanqiao.dao.UserDao;
import com.yuanqiao.entity.User;
import com.yuanqiao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public PageInfo<User> getAllUsers(int pageNum,int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<User> allUsers = userDao.getAllUsers();
        PageInfo<User> pageInfo=new PageInfo<>(allUsers);
        return pageInfo;
    }


    @Override
    public User getUserById(Integer id) {
        User user = userDao.getUserById(id);
        System.out.println("------------------这里打印了吗?------------------------------");
        System.out.println(JSON.toJSONString(user));
        return user;
    }


}

 

执行测试类

package com.yuanqiao.service;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserServiceTest {

    @Autowired
    private UserService userService;

    @Test
    public void getAllUsers(){
        userService.getAllUsers(1,2);
    }
    @Test
    public void getAllUsers002(){
        userService.getAllUsers(2,3);
    }
}

 

 

数据库数据如下;

 

测试sql日志结果如下:

2019-08-03 00:21:00.053  INFO 24268 --- [           main] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} inited
JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@6d963d70] will not be managed by Spring
==>  Preparing: SELECT count(0) FROM user 
==> Parameters: 
<==    Columns: count(0)
<==        Row: 14
<==      Total: 1
==>  Preparing: select * from user LIMIT ?, ? 
==> Parameters: 3(Integer), 3(Integer)
<==    Columns: id, name, sex, age
<==        Row: 4, zhangsan, female, 33
<==        Row: 5, zhangsan, male, 18
<==        Row: 6, zhangsan, male, 19
<==      Total: 3

 

可以看到pageHelper实质是帮我们重新封装了sql的,并没有全表扫描出来再物理组装,所以是很高级的一个工具了。

 

posted on 2019-08-03 00:32  坚守梦想  阅读(3650)  评论(0编辑  收藏  举报