mybatis的分页写法

飞一样的编程
飞一样的编程
擅长邻域:Java,MySQL,Linux,nginx,springboot,mongodb,微信小程序,vue

分类: ssm 专栏: ssm框架课 标签: mybatis分页写法

2023-01-07 12:42:24 720浏览

mybatis分页写法

1.传统的方式,也就是不集成插件,自己封装pageInfo

  • 封装了一个pageinfo的类,方便给前端直接展示用
@Data
public class PageInfo<T> {
    private Integer pageNo;//第几页
    private Integer pageSize;//页面尺寸(一页显示几条 )
    private Integer totalCount;//总的记录数
    private Integer pages;//总的页数(根据totalCount 和pageSize来计算的)
    private List<T> rows;//咱们的数据list


    public void setPages(){
        if(totalCount % pageSize ==0 ){//满页  比如10条,一页显示5条,
            pages=totalCount/pageSize;
        }else{
            pages=totalCount/pageSize+1;
        }
    }

}
  • 两个查询方法接口
//分页查询部门信息
   List<Department> getPage(@Param("department") Department department,@Param("offset") Integer offset, @Param("pageSize") Integer pageSize);

   //计算总的记录数
   Integer getCount(@Param("department") Department department);
  • 测试类
 @Test
    public void testPage(){
        //把pageinfo封装好返回前端页面
        Integer pageNo =3;
        Integer pageSize =3;
        PageInfo<Department> pageInfo = new PageInfo<>();
        pageInfo.setPageNo(pageNo);
        pageInfo.setPageSize(pageSize);

        List<Department> list = departmentMapper.getPage(new Department().setDname("c"), (pageNo - 1) * pageSize, pageSize);

        pageInfo.setRows(list);
        Integer count = departmentMapper.getCount(new Department().setDname("c"));//拿到总条数
        pageInfo.setTotalCount(count);
        pageInfo.setPages();

        System.out.println(pageInfo);

    }

2.采用pagehelper插件

官网:https://pagehelper.github.io/docs/howtouse/

快速入门

  • 引入jar包(两个),当然也可以用maven的方式引入

maven的方式引入

<!--*****************************引入分页插件********************************-->
    <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper</artifactId>
      <version>5.1.10</version>
    </dependency>
    <!-- pagehelper 依赖 -->
    <dependency>
      <groupId>com.github.jsqlparser</groupId>
      <artifactId>jsqlparser</artifactId>
      <version>2.1</version>
    </dependency>
    <!--*****************************引入分页插件********************************-->
  • 配置插件
  1. 可以在mybatisconfig里配置,但我不推荐
<!--
    plugins在配置文件中的位置必须符合要求,否则会报错,顺序如下:
    properties?, settings?,
    typeAliases?, typeHandlers?,
    objectFactory?,objectWrapperFactory?,
    plugins?,
    environments?, databaseIdProvider?, mappers?
-->
<plugins>
    <!-- com.github.pagehelper为PageHelper类所在包名 -->
    <plugin interceptor="com.github.pagehelper.PageInterceptor">
        <!-- 使用下面的方式配置参数,后面会有所有的参数介绍 -->
        <property name="param1" value="value1"/>
	</plugin>
</plugins>
  1. 采用在spring的配置文件里配置
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations">
            <list>
                <value>classpath:mapper/*.xml</value>
            </list>
        </property>
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <!--使用下面的方式配置参数,一行配置一个 -->
                        <props>
                            <prop key="helperDialect">mysql</prop>
                            <prop key="reasonable">true</prop>
                        </props>
                    </property>
                </bean>
            </array>
        </property>
    </bean>
  1. 测试类
  @Test
    public void testPageHelper(){
        PageHelper.startPage(4,3,"id desc");
        List<Department> departmentList = departmentMapper.getAll();

        PageInfo<Department> pageInfo = new PageInfo<>(departmentList,3);
        System.out.println(pageInfo);
    }

3.对比两种方式

用插件的优点:代码少了很多,考虑了很多安全性的问题,功能也比传统的强大

4.对于带有collection的分页注意

这种因为有重复数据,所以要改造成子查询的方式进行分页

  <resultMap id="employeeAndDepartmentAndSkill" type="com.jf3q.c55.datashow.pojo.Employee">
        <id property="id" column="id"></id>
        <result property="account" column="account"></result>
        <result property="realname" column="realname"></result>
        <result property="phone" column="phone"></result>
        <result property="sex" column="sex"></result>
        <result property="idcardPositive" column="idcardPositive"></result>
        <result property="idcardNegative" column="idcardNegative"></result>
        <result property="description" column="description"></result>
        <result property="faceimg" column="faceimg"></result>
        <result property="birthday" column="birthday"></result>
        <result property="did" column="did"></result>
        <association property="department" javaType="com.jf3q.c55.datashow.pojo.Department">
            <id property="id" column="did"></id>
            <result property="dname" column="dname"></result>
        </association>

        <collection property="skills" ofType="com.jf3q.c55.datashow.pojo.Skill" select="selectSkill" column="id"/>
    </resultMap>

在collection里加一个select="selectSkill" column="id"

子查询的代码

 <resultMap id="SkillMap" type="com.jf3q.c55.datashow.pojo.Skill">
        <id column="id" property="id"></id>
        <result property="uid" column="uid"></result>
        <result property="sname" column="sname"></result>
    </resultMap>
<!--子查询-->
    <select id="selectSkill"  resultMap="SkillMap" parameterType="integer"  >
        select * from skill s

        where s.uid = #{id}
    </select>

好博客就要一起分享哦!分享海报

此处可发布评论

评论(3展开评论

蓝色妖姬 能力:10

2023-07-12 17:09:41

害,学到最后,分页都忘记了,真的是除了limit,其他全忘,害,废了
蓝色妖姬 能力:10

2023-06-17 10:50:59

又忘记了,来温习,废了
蓝色妖姬 能力:10

2023-01-07 15:38:47

哈哈哈,老师厉害
点击查看更多评论

展开评论

您可能感兴趣的博客

客服QQ 1913284695