Springboot 系列(十二)使用 Mybatis 集成 pagehelper 分页插件和 mapper 插件 (4)

编写 BookMapperTest 单元测试用于测试 BookMapper 的方法。

package net.codingme.boot.domain.mapper; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import net.codingme.boot.domain.Book; import org.junit.Assert; 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; import java.util.List; @RunWith(SpringRunner.class) @SpringBootTest public class BookMapperTest { @Autowired private BookMapper bookMapper; @Test public void testSelectOne() { Book book = new Book(); book.setId(2); Book selectOne = bookMapper.selectOne(book); Assert.assertNotNull(selectOne); System.out.println(selectOne); } @Test public void testSelectByPrimaryKey() { Book book = bookMapper.selectByPrimaryKey(2); Assert.assertNotNull(book); System.out.println(book); } /** * 分页测试 */ @Test public void testSelectPageInfo() { PageHelper.startPage(2, 3); List<Book> bookList = bookMapper.selectAll(); Assert.assertNotNull(bookList); System.out.println("查询出数量:" + bookList.size()); PageInfo<Book> pageInfo = PageInfo.of(bookList); System.out.println("总数量:" + pageInfo.getTotal()); System.out.println("总页数:" + pageInfo.getPages()); System.out.println("页大小:" + pageInfo.getPageSize()); System.out.println("第几页:" + pageInfo.getPageNum()); System.out.println("当前量:" + pageInfo.getSize()); } /** * 分页测试 */ @Test public void testSelectPage() { PageHelper.startPage(2, 3); List<Book> bookList = bookMapper.selectAll(); Assert.assertNotNull(bookList); System.out.println("查询出数量:" + bookList.size()); System.out.println("总数量:" + ((Page)bookList).getTotal()); System.out.println("总页数:" + ((Page)bookList).getPages()); System.out.println("第几页:" + ((Page)bookList).getPageNum()); } }

从代码中可以看到分页的实现主要是 PageHelper 的设置,在设置 PageHelper 之后的第一个查询会进行分页。像上面的例子会查询第二页,每页三条这样。

PageHelper.startPage(2, 3); List<Book> bookList = bookMapper.selectAll();

其实使用了分页插件之后返回的数据类型是一个 Page 类,总数等分页信息都已经返回,如果要取出来使用就需要强制转换类型然后取出,上面也是演示了两种方式。

// 方式 1 PageInfo<Book> pageInfo = PageInfo.of(bookList); System.out.println("总数量:" + pageInfo.getTotal()); System.out.println("总页数:" + pageInfo.getPages()); System.out.println("页大小:" + pageInfo.getPageSize()); System.out.println("第几页:" + pageInfo.getPageNum()); System.out.println("当前量:" + pageInfo.getSize()); // 方式 2 System.out.println("查询出数量:" + bookList.size()); System.out.println("总数量:" + ((Page)bookList).getTotal()); System.out.println("总页数:" + ((Page)bookList).getPages()); System.out.println("第几页:" + ((Page)bookList).getPageNum());

运行 BookMapperTest 类测试所有的单元测试。

单元测试结果

发现单元测试全部通过,查看一个分页查询(testSelectPageInfo)输出情况。

2019-03-08 16:07:52.226 DEBUG 26764 --- [ main] n.c.b.d.m.BookMapper.selectAll_COUNT : ==> Preparing: SELECT count(0) FROM book 2019-03-08 16:07:52.227 DEBUG 26764 --- [ main] n.c.b.d.m.BookMapper.selectAll_COUNT : ==> Parameters: 2019-03-08 16:07:52.229 DEBUG 26764 --- [ main] n.c.b.d.m.BookMapper.selectAll_COUNT : <== Total: 1 2019-03-08 16:07:52.231 DEBUG 26764 --- [ main] n.c.b.d.mapper.BookMapper.selectAll : ==> Preparing: SELECT id,author,name,price,create_time,description FROM book LIMIT ?, ? 2019-03-08 16:07:52.233 DEBUG 26764 --- [ main] n.c.b.d.mapper.BookMapper.selectAll : ==> Parameters: 3(Integer), 3(Integer) 2019-03-08 16:07:52.236 DEBUG 26764 --- [ main] n.c.b.d.mapper.BookMapper.selectAll : <== Total: 3 查询出数量:3 总数量:12 总页数:4 页大小:3 第几页:2 当前量:3

再查看一个普通查询(testSelectByPrimaryKey)输出情况。

2019-03-08 16:07:52.241 DEBUG 26764 --- [ main] n.c.b.d.m.BookMapper.selectByPrimaryKey : ==> Preparing: SELECT id,author,name,price,create_time,description FROM book WHERE id = ? 2019-03-08 16:07:52.242 DEBUG 26764 --- [ main] n.c.b.d.m.BookMapper.selectByPrimaryKey : ==> Parameters: 2(Integer) 2019-03-08 16:07:52.244 DEBUG 26764 --- [ main] n.c.b.d.m.BookMapper.selectByPrimaryKey : <== Total: 1 Book(id=2, author=金庸, name=笑傲江湖, price=12.0, createTime=Sat Sep 01 10:10:12 GMT+08:00 2018, description=是作家金庸创作的一部长篇武侠小说)

文中代码已经上传到 Github Spring Boot 连接数据库 - Mybatis 插件

想要了解这几个插件的其他信息,可以查看官方文档。

如何使用分页插件

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpdddy.html