SSM-2-旅游网站,分页PageHelper,SpringSecurity安全框架的配置和使用,PageHelper分页插件的相关参数

本文详细介绍了在SSM框架下如何使用PageHelper进行分页,包括手动分页和PageHelper插件的使用,解析了相关API并展示了配置实例。此外,还阐述了SpringSecurity安全框架的基础知识,提供了入门使用的步骤,从项目创建到自定义登录页面的配置,帮助读者理解并掌握这两个关键组件。
摘要由CSDN通过智能技术生成

第一章:分页助手PageHelper

第一节:手动分页

1、页面入口

[外链图片转存失败(img-m9GqrM5X-1568920037564)(img\1.png)]

2、编写分页实体PageBean
package com.itheima.domain;

import java.util.List;

/**
 *
 * 分页的POJO对象
 *      当前页
 *      每页条数
 *      总条数
 *      总页数
 *      当前页数据
 *
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
public class PageBean <T>{
   

//    当前页 --- 页面传参
    private Integer currPage;
//    每页条数 -- 页面传参
    private Integer pageSize;
//    总条数 -- 数据库查询
    private Long totalCount;
//    总页数 -- 计算
        // Math.ceil(totalCount * 1.0 / pageSize)
    private Integer totalPage;
//    当前页数据 -- 数据库查询
    private List<T> list;

    public Integer getCurrPage() {
   
        return currPage;
    }

    public void setCurrPage(Integer currPage) {
   
        this.currPage = currPage;
    }

    public Integer getPageSize() {
   
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
   
        this.pageSize = pageSize;
    }

    public Long getTotalCount() {
   
        return totalCount;
    }

    public void setTotalCount(Long totalCount) {
   
        this.totalCount = totalCount;
    }

    public Integer getTotalPage() {
   
        return totalPage;
    }

    public void setTotalPage(Integer totalPage) {
   
        this.totalPage = totalPage;
    }

    public List<T> getList() {
   
        return list;
    }

    public void setList(List<T> list) {
   
        this.list = list;
    }
}

3、编写Controller
/**
     * 手动分页查询
     *
     * RequestParam; 请求参数绑定
     *      name:别名value, 指定页面参数的名称
     *      required: 是否必要参数
     * @return
     */
    @RequestMapping("/findAll2")
    public ModelAndView findAll2(
            @RequestParam(value = "currPage",required = false, defaultValue = "1")  Integer currPage ,
            @RequestParam(value = "pageSize",required = false, defaultValue = "5")Integer pageSize){
   
        //准备数据: 分页数据
        PageBean<Product> pageBean = productService.findByPage(currPage,pageSize);
        //创建ModelAndView对象
        ModelAndView modelAndView = new ModelAndView();
        //添加数据
        modelAndView.addObject("pageBean",pageBean);
        //指定页面
        modelAndView.setViewName("product-list");
        return modelAndView;
    }
4、编写Service

接口

    /**
     * 根据分页参数查询PageBean对象
     * @param currPage
     * @param pageSize
     * @return
     */
    PageBean<Product> findByPage(Integer currPage, Integer pageSize);

实现

@Override
    public PageBean<Product> findByPage(Integer currPage, Integer pageSize) {
   
        //创建PageBean对象
        PageBean<Product> pageBean = new PageBean<>();
        //封装PageBean
//    当前页 --- 页面传参
//        private Integer currPage;
        pageBean.setCurrPage(currPage);
//    每页条数 -- 页面传参
//        private Integer pageSize;
        pageBean.setPageSize(pageSize);
//    总条数 -- 数据库查询
//        private Long totalCount;
        Long totalCount = productDao.findTotalCount();
        pageBean.setTotalCount(totalCount);
//    总页数 -- 计算
        // Math.ceil(totalCount * 1.0 / pageSize)
//        private Integer totalPage;
        pageBean.setTotalPage((int)Math.ceil(totalCount * 1.0 / pageSize));
//    当前页数据 -- 数据库查询
//        private List<T> list;
        /** 每页展示5条数据
         * 第一页: 1   5
         * 第二页: 6   10
         * 第三页:11  15
         * 第n页: 5n-(5-1)    5n
         * currPage ===> n
         * pageSize ==> 5
         * 第n页:pageSize * currPage-(pageSize-1)=pageSize(currPage-1) +1
         *  currPage*pageSize
         */
        Integer startIndex = pageSize*(currPage-1) +1;
        Integer endIndex = currPage*pageSize;
        List<Product> productList = productDao.findByPage(startIndex, endIndex);
        pageBean.setList(productList);
        return pageBean;
    }
5、编写Dao
/**
     * 分页查询数据
     * @param startIndex
     * @param endIndex
     * @return
     */
    @Select("select t.* from (select p.*,rownum rn from product p) t where t.rn between #{param1} and #{param2}")
    List<Product> findByPage(Integer startIndex, Integer endIndex);
}
6、编写页面
<c:forEach items="${pageBean.list}" var="product">
  
</c:forEach>

<div class="box-footer">
					<div class="pull-left">
						<div class="form-group form-inline">
							总共${pageBean.totalPage} 页,共${pageBean.totalCount} 条数据。 每页
							<select id="pageSize" οnchange="gotoPage(1)" class="form-control">
								<option value="2">2</option>
								<option value="3">3</option>
								<option value="5" selected="selected">5</option>
								<option value="10">10</option>
							</select> 条
						</div>
					</div>

					<div class="box-tools pull-right">
						<ul class="pagination">
							<%--在超链接中访问js函数,必须加前缀:javascript--%>
							<li><a href="javascript:gotoPage(1)" aria-label="Previous">首页</a></li>
							<li><a href="javascript:gotoPage(${pageBean.currPage-1})">上一页</a></li>

							<%--begin:从哪儿开始--%>
							<%--end:到哪儿结束--%>
							<c:forEach begin="1" end="${pageBean.totalPage}" var="i">
								<li><a href="javascript:gotoPage(${i})">${i}</a></li>
							</c:forEach>
							<li><a href="javascript:gotoPage(${pageBean.currPage+1})">下一页</a></li>
							<li><a href="javascript:gotoPage(${pageBean.totalPage})" aria-label="Next">尾页</a></li>
						</ul>
					</div>

$("#pageSize option[value=${pageBean.pageSize}]").prop("selected","selected");

		function gotoPage(currPage){
   
		    //获取每页显示的条数
			var pageSize = $("#pageSize").val();
		    if(currPage < 1){
   
		        return ;
			}
			if(currPage > ${
   pageBean.totalPage}){
   
		        return ;
            }
		    location.href="${pageContext.request.contextPath}/product/findAll?currPage="+currPage+"&pageSize="+pageSize;
		}

第二节:分页助手PageHelper的使用

1、PageHelper简介

PageHelper是国内非常优秀的一款开源的mybatis分页插件,它支持基本主流与常用的数据库,例如mysql、oracle、mariaDB、DB2、SQLite、Hsqldb等。

网址:https://pagehelper.github.io/

本项目在 github 的项目地址:https://github.com/pagehelper/Mybatis-PageHelper

本项目在 gitosc 的项目地址:http://git.oschina.net/free/Mybatis_PageHelper

2、PageHelper的环境搭建

添加PageHelper坐标(之前的pom中已将包含该坐标)

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

配置mybatis的PageHelper插件,mybatis的配置已经集成到spring的配置文件中了,所以在配置SqlSessionFactory时指定插件

 <!--引入分页插件 - 方法1 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!--数据源-->
    <property name="dataSource" ref="dataSource"></property>
    <!--mybatis的其他配置 -->
    <property name="plugins">
        <array>
            <bean class="com.github.pagehelper.PageInterceptor">
                <property name="properties">
                    <props>
                        <!-- 分页的相关配置参数 详细参数解析见附录 -->
                        <prop key="helperDialect">oracle</prop>
                    </props>
                </property>
            </bean>
        </array>
    </property>
</bean>
______________________________________________________________________________
 <!--引入分页插件 - 方法2 -->
<!--引入mybatis的配置文件-->
<property name="configLocation" 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值