Mybatis分页之PageHelper插件+layui实现分页展示

1.准备一个layui页面,这个可以去layui官网去找,也可以去layuimini去找
layuimini网址:layuimini后台模板
在这里插入图片描述
2.然后建立一个项目,把页面放进去
3.添加依赖


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

4.在mybatis-config.xml里配置

 <!--配置SqlSessionFactoryBean-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <!--下面这这个配置的是 mapper.xml所在路径-->
        <property name="mapperLocations" value="classpath:mapper/*.xml"></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>

4.2.layui接收数据格式如图
在这里插入图片描述

4.2.这里整合了layui,为了满足其数据接收格式,我封装了一个vo

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResultData<T> {
    //返回状态码
     private int code;
     //返回消息
     private String msg;
     //返回总数据
     private long count;
     //返回的具体对象
     private List<T> data;
}

5.controller

@RequestMapping("/findCourseType.action")
    @ResponseBody
    public ResultData findClassType(int  page , int limit){
        ResultData<CourseType>  courseType =  courseTypeService.findCourseTypes(page , limit);
        return courseType;
    }

6.serviceimpl

  @Override
    public ResultData<CourseType> findCourseTypes(int page , int limit) {
        //使用Mybatis的分页插件PageHelper
        Page<CourseType> pages = PageHelper.startPage(page, limit);
        //查询数据
        List<CourseType> courseType = courseTypeMapper.findCourseTypes();

        ResultData<CourseType> courseTypeList = new ResultData<>();
        courseTypeList.setCode(0);
        courseTypeList.setMsg("");
        courseTypeList.setCount(pages.getTotal());
        courseTypeList.setData(courseType);

        return courseTypeList;
    }

7.dao

 Page<CourseType> findCourseTypes();

8.sql

 <!--查询课程类型信息-->
    <select id="findCourseTypes" parameterType="com.qf.qfedu.pojo.CourseType" resultType="com.qf.qfedu.pojo.CourseType">
        select * from t_classType
        <where>
            <if test="classTypeId != null">
                classTypeId=#{classTypeId}
            </if>
            <if test="classTypeName != null and classTypeName != ''">
                AND classTypeName=#{classTypeName}
            </if>
            <if test="classParentId != null">
                AND classParentId=#{classParentId}
            </if>
            <if test="isDeleteClassType != null ">
                AND isDeleteClassType=#{sDeleteClassType}
            </if>
        </where>
    </select>

结果图
在这里插入图片描述

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值