Spring Boot整合Mybatis及PageHelper实现分页查询

1、导入jar包

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.4</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>

2、数据源及日志配置

spring:
  datasource:
    driver-class-name: oracle.jdbc.driver.OracleDriver
    url: jdbc:oracle:thin:@localhost:1521:orcl
    username: scott
    password: scott
#开启SQL打印    
logging:
  level:
    #单独指定包
    org.silence.blog.mapper: debug

3、编写Mapper

@Repository
public interface EmpMapper {

    @Select("select  * from emp  ")
    List<Emp> queryList();

    @Select("select *  from emp where empno = #{empno}")
    Emp queryByEmpNo(String empno);

}

4、测试

@SpringBootTest
class EmpMapperTest {

    @Autowired
    EmpMapper mapper;

    @Test
    void queryList() {
        //设置查询页面
        PageHelper.startPage(1, 6);
        //执行查询
        List<Emp> emps = mapper.queryList();
        //构建PageInfo对象,将结果集传入
        PageInfo<Emp> pageInfo = new PageInfo<Emp>(emps);
        pageInfo.getList().forEach(System.out::println);
    }

    @Test
    void queryByEmpNo() {
    }
}

5、输出结果

2021-03-04 21:20:49.349 DEBUG 14456 --- [           main] o.s.b.mapper.EmpMapper.queryList_COUNT   : ==>  Preparing: SELECT count(0) FROM emp
2021-03-04 21:20:49.451 DEBUG 14456 --- [           main] o.s.b.mapper.EmpMapper.queryList_COUNT   : ==> Parameters: 
2021-03-04 21:20:49.513 DEBUG 14456 --- [           main] o.s.b.mapper.EmpMapper.queryList_COUNT   : <==      Total: 1
2021-03-04 21:20:49.515 DEBUG 14456 --- [           main] o.s.blog.mapper.EmpMapper.queryList      : ==>  Preparing: SELECT * FROM ( SELECT TMP_PAGE.*, ROWNUM ROW_ID FROM ( select * from emp ) TMP_PAGE) WHERE ROW_ID <= ? AND ROW_ID > ?
2021-03-04 21:20:49.517 DEBUG 14456 --- [           main] o.s.blog.mapper.EmpMapper.queryList      : ==> Parameters: 6(Integer), 0(Integer)
2021-03-04 21:20:49.529 DEBUG 14456 --- [           main] o.s.blog.mapper.EmpMapper.queryList      : <==      Total: 6
Emp(empno=7369, ename=SMITH, job=CLERK, mgr=7902, hiredate=1980-12-17T00:00, sal=800, comm=null, deptno=20)
Emp(empno=7499, ename=ALLEN, job=SALESMAN, mgr=7698, hiredate=1981-02-20T00:00, sal=1600, comm=300, deptno=30)
Emp(empno=7521, ename=WARD, job=SALESMAN, mgr=7698, hiredate=1981-02-22T00:00, sal=1250, comm=500, deptno=30)
Emp(empno=7566, ename=JONES, job=MANAGER, mgr=7839, hiredate=1981-04-02T00:00, sal=2975, comm=null, deptno=20)
Emp(empno=7654, ename=MARTIN, job=SALESMAN, mgr=7698, hiredate=1981-09-28T00:00, sal=1250, comm=1400, deptno=30)
Emp(empno=7698, ename=BLAKE, job=MANAGER, mgr=7839, hiredate=1981-05-01T00:00, sal=2850, comm=null, deptno=30)
上一篇:SpringBoot整合PageHelper


下一篇:Element.prototype.addDependent will call addAggregation