0


Mybatis---一分钟学会Mybatis分页插件

Mybatis---一分钟学会Mybatis分页插件

还不会使用mybatis的建议看看我以往的文章:
Mybatis入门—如何使用mybatis查询数据库数据

一、mybatis四步走(实体类、Mapper接口、Mapper.xml配置、mybatis-config.xml配置)

Person类

publicclassPerson{privateInteger id;privateString name;privateDate birthday;privateString address;privateCar car;//一个用户拥有一个carprivateList<Order> orderList;//一个用户拥有多个订单publicPerson(){}publicPerson(Integer id,String name,Date birthday,String address,Car car,List<Order> orderList){this.id = id;this.name = name;this.birthday = birthday;this.address = address;this.car = car;this.orderList = orderList;}publicIntegergetId(){return id;}publicvoidsetId(Integer id){this.id = id;}publicStringgetName(){return name;}publicvoidsetName(String name){this.name = name;}publicDategetBirthday(){return birthday;}publicvoidsetBirthday(Date birthday){this.birthday = birthday;}publicStringgetAddress(){return address;}publicvoidsetAddress(String address){this.address = address;}publicCargetCar(){return car;}publicvoidsetCar(Car car){this.car = car;}publicList<Order>getOrderList(){return orderList;}publicvoidsetOrderList(List<Order> orderList){this.orderList = orderList;}@OverridepublicStringtoString(){return"Person{"+"id="+ id +", name='"+ name +'\''+", birthday="+ birthday +", address='"+ address +'\''+", car="+ car +", orderList="+ orderList +'}';}}

PersonMapper

publicinterfacePersonMapper{publicList<Person>findPersonByPage();}

PersonMapper.xml

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPEmapperPUBLIC"-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="com.mapper.PersonMapper"><selectid="findPersonByPage"resultType="com.entity.Person">
        select * from person
    </select></mapper>

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><!-- 别名 --><typeAliases><packagename="com.entity"/></typeAliases><environments default="p1"><environment id="p1"><transactionManager type="JDBC"/><dataSource type="POOLED"><property name="driver" value="com.mysql.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/mybatis"/><property name="username" value="root"/><property name="password" value="123456"/></dataSource></environment></environments><!-- 注册映射  --><mappers><!-- 使用package标签映射时,接口和xml文件都要放到相同的路径下 --><packagename="com.mapper"/></mappers></configuration>

二、在pom.xml中添加依赖

<dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId><version>5.2.0</version></dependency>

三、修改mybatis-config.xml配置文件

<environments>

标签外面添加

<plugins><plugininterceptor="com.github.pagehelper.PageInterceptor"></plugin></plugins>

四、语法

  • 设置分页参数 PageHelper.startPage(1,2)
  • 总记录数 getTotal()
  • 总页数 getPages()
  • 上一页的页码 getPrePage()
  • 下一页的页码 getNextPage()
publicclassTestMybatis6{@Testpublicvoidtest()throwsIOException{InputStream is =Resources.getResourceAsStream("mybatis-config.xml");SqlSessionFactory sessionFactory =newSqlSessionFactoryBuilder().build(is);SqlSession sqlSession = sessionFactory.openSession();PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);//设置分页参数//PageHelper.startPage(1,2);//按照生日升序序排序后分页PageHelper.startPage(1,2,"birthday");//按照生日降序排序后分页//PageHelper.startPage(1,2,"birthday desc");List<Person> personList = personMapper.findPersonByPage();for(Person person : personList){System.out.println(person.getName()+"  "+person.getBirthday());}//假如需要知道总记录数、总页数、能不能上一页、下一页等等//如果不需要可以不写PageInfo<Person> pageInfo =newPageInfo<>(personList);System.out.println("总记录数:"+pageInfo.getTotal());System.out.println("总页数:"+pageInfo.getPages());System.out.println("上一页的页码:"+pageInfo.getPrePage());System.out.println("下一页的页码:"+pageInfo.getNextPage());System.out.println("显示所有页码:"+Arrays.toString(  pageInfo.getNavigatepageNums()));System.out.println("是否首页:"+pageInfo.isIsFirstPage());System.out.println("是否尾页:"+pageInfo.isIsLastPage());System.out.println("分页后的数据:"+pageInfo.getList());}}

五、运行结果

我这里使用了日志打印
在这里插入图片描述

标签: java maven mybatis

本文转载自: https://blog.csdn.net/weixin_46816383/article/details/123832688
版权归原作者 lichaohua! 所有, 如有侵权,请联系我们删除。

“Mybatis---一分钟学会Mybatis分页插件”的评论:

还没有评论