ssm使用分页插件 pageHelper整理公共页

本文介绍了如何在SSM项目中使用PageHelper分页插件,详细讲述了在Controller中的分页方法,以及如何封装公共分页类,并提供了分页按钮组件的页面。在实际应用中,可能需要根据需求在公共类中扩展更多功能。
摘要由CSDN通过智能技术生成
在一个项目中分页时必不可少的,但是多个功能都需要使用到分页,每次都需要写一遍分页的按钮组件、分页的一些相关属性很麻烦。这里将分页的一些按钮组件放在了一个公共的页面中,在使用时直接导入这个公共的页面,下面说一下代码的实现。
关于pageHepler的相关配置,网上说的很详细,这里不做过多的解释

导入的是这三个jar包

<!-- mybatis分页 -->
        <dependency>
            <groupId>com.github.miemiedev</groupId>
            <artifactId>mybatis-paginator</artifactId>
            <version>1.2.5</version>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.2</version>
        </dependency>
        <dependency>
            <groupId>com.github.jsqlparser</groupId>
            <artifactId>jsqlparser</artifactId>
            <version>0.9.6</version>
        </dependency>
首先是在想使用分页的页面导入公共页:
<!-- 分页 -->
//需要导入公共的分页页面
                            <jsp:include page="../common/include_page.jsp">
//在这里需要设置一些参数,value即要访问的路径,name为给这个值起一个名字
    <jsp:param value="/shop-admin/classify/classifyAll" name="pageTitle"/>
                            </jsp:include>

公共页稍后附上,先看在controller中的方法,即上面value要访问的路径

@Controller
@RequestMapping("/classify")
public class ClassifyController {

    @Autowired
    private ClassifyService classifyService;

    @Autowired
    private ClassifySubService classifySubService;
    //整理的公共类对象
    @Autowired
    private PageUtil pageUtil;
    /**
     * 
     * @param productClassify   操作的实体类
     * @param index     分页要跳转的页码,或者是当前页码
     * @param pageSize      每页显示的信息数目
     * @return
     */
    @RequestMapping("/classifyAll")
    public String classifyAll(ProductClassify productClassify,ModelMap modelMap,@RequestParam(required=true,defaultValue="1") Integer index,
            @RequestParam(required=false,defaultValue="10") Integer pageSize,HttpServletRequest request){
        //在执行查询的前一行加上这条代码即可执行分页的查询
        PageHelper.startPage(index, pageSize);
        //查询到的实体类集合
        Page<ProductClassify> classifyList=(Page<ProductClassify>)classifyService.selectAllClassify(productClassify);
        //这里调用了自己封装的公共方法
        pageUtil.setPageInfo(classifyList, index, pageSize,request);
        //将查询到的结果集传回页码
        modelMap.put("classifyList", classifyList);
        return "/classify/list-classify";
    }

下面附上自己封装的公共类,使用时每次调用其中的方法即可

/**
 * 分页工具类
 * @author Administrator
 *  Integer start   起始项的索引
 *  Integer end     总页数
 *  Integer total   结尾项的索引
 *  Integer totalCount      当前集合的总条数
 *  Integer index   当前页码数或者说要跳转的页面数
 */
@Component
public class PageUtil {
    Integer start;
    Integer end;
    Integer total;
    Integer totalCount;
    Integer index;

    public void setPageInfo(Page<?> list,Integer index,Integer pageSize,HttpServletRequest request){
        //如果要跳转的页码数小于零则跳转首页
        if(index<=0){
            index=1;
        }
        //获取末页数
        end=(int) list.getPages();
        //如果要跳转的页数大于末页,则跳转末页
        if(index>=end){
            index=end;
            total=(int) list.getTotal();
        }else{
            total=index*pageSize;
        }
        //该页起始项的索引
        start=(index-1)*pageSize+1;
        //获取总的信息数目
        totalCount=(int) list.getTotal();
        this.index=index;


        request.setAttribute("index", index);
        request.setAttribute("end", end);
        request.setAttribute("start", start);
        request.setAttribute("total", total);
        request.setAttribute("totalCount", totalCount);
    }
}

最后附上公共的分页按钮组件页面

<div class="row">
    <div class="col-sm-6">
        <div class="dataTables_info">显示
            ${start } 到 ${total }
            项,共 ${totalCount } 项</div>
    </div>
    <div class="col-sm-6">
        <div class="dataTables_paginate paging_simple_numbers">
            <ul class="pagination">
                <li class="paginate_button previous"><a href="<%=request.getParameter("pageTitle")%>?index=1">首页</a>
                <li class="paginate_button previous"><a href="<%=request.getParameter("pageTitle")%>?index=${index-1}">上一页</a>
                </li>

                    <li class="paginate_button">
                        <a href="<%=request.getParameter("pageTitle")%>?index=2">2</a>
                    </li>
                <li class="paginate_button next"><a href="<%=request.getParameter("pageTitle")%>?index=${index+1}">下一页</a></li>
                <li class="paginate_button previous"><a href="<%=request.getParameter("pageTitle")%>?index=${end}">末页</a>
            </ul>
        </div>
    </div>
</div>

前端的样式框架不用管,把class属性删掉即可
最后的结果
这里写图片描述
会有些小问题像已经跳转到末页之后再点击下一页会没有内容,如果再需要其他的功能可以自己在公共类中再添加

SSM(Spring + SpringMVC + MyBatis)中使用PageHelper进行分页查询是一种常见的做法。以下是使用PageHelper实现分页查询的步骤: 1. 首先,在项目的依赖管理中添加PageHelper的相关依赖。你可以在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>版本号</version> </dependency> ``` 请注意,将“版本号”替换为PageHelper的最新版本号。 2. 在Spring配置文件(例如applicationContext.xml)中配置PageHelper插件。添加以下配置: ```xml <bean id="pageHelper" class="com.github.pagehelper.PageHelper"> <property name="dialect" value="mysql"/> </bean> ``` 请注意,这里的dialect属性值可能需要根据你使用的数据库类型进行相应的设置。 3. 在你的DAO层接口中,添加使用PageHelper进行分页查询的方法。例如: ```java List<Entity> findEntitiesByPage(int pageNum, int pageSize); ``` 4. 在DAO层的XML映射文件中,使用PageHelper提供的插件进行分页查询。在查询语句之前添加以下配置: ```xml <select id="findEntitiesByPage" parameterType="map" resultMap="entityResultMap"> <!-- 此处为PageHelper提供的插件 --> <include refid="PageHelper.startPage"/> SELECT * FROM your_table <!-- 此处为PageHelper提供的插件 --> <include refid="PageHelper.endPage"/> </select> ``` 请注意,将"your_table"替换为你的实际表名,并且确保映射文件中已定义相应的结果映射。 5. 在Service层或Controller层调用DAO层的分页查询方法,传入码和每大小参数。 这样,你就可以使用PageHelper实现SSM分页查询了。记得在每次查询之后,需要手动清除ThreadLocal中的分页参数,以免对其他查询产生影响: ```java PageHelper.clearPage(); ``` 希望能对你有所帮助!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值