日韩精品欧美激情国产一区_中文无码精品一区二区三区在线_岛国毛片AV在线无码不卡_亞洲歐美日韓精品在線_使劲操好爽好粗视频在线播放_日韩一区欧美二区_八戒八戒网影院在线观看神马_亚洲怡红院在线色网_av无码不卡亚洲电影_国产麻豆媒体MDX

PageHelper的使用

時(shí)間:2020-03-05 23:14:14 類型:JAVA
字號:    

   PageHelper是一款好用的開源免費(fèi)的第三方物理分頁插件,這是一個(gè)基于MyBatis開源的分頁插件,使用非常方便,支持各種復(fù)雜的單表、多表分頁查詢,讓你在寫sql時(shí)無需考慮分頁問題,PageHelper幫你搞定

 1. 導(dǎo)入坐標(biāo)

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

2.  在將配置放在mybatis的配置文件中

      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>
    <!-- 配置分頁插件 -->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            <!-- 設(shè)置數(shù)據(jù)庫類型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六種數(shù)據(jù)庫-->
            <property name="helperDialect" value="mysql"/>
        </plugin>
    </plugins>
</configuration>

  然后在spring配置文件中加載mybatis的配置

<!-- 配置mybatis工廠,同時(shí)指定數(shù)據(jù)源,并與MyBatis完美整合 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 自動(dòng)掃描mapping.xml文件 -->  
        <property name="mapperLocations" value="classpath:mybatis/mapper/*.xml" />
         <!-- 加載mybatis配置文件 -->
   		 <property name="configLocation" value="classpath:mybatis-config.xml"></property>
    </bean>

控制器中調(diào)用

@RequestMapping("select")
	public String selectStudent(
			@RequestParam(defaultValue = "1") Integer page ,
			@RequestParam(defaultValue="2")  Integer pageSize,
			Student student, Model model) 
	{
		PageHelper.startPage(page,pageSize);//開始分頁
		List<Student> lists = studentService.selectStudent(student);
		PageInfo<Student> pageInfo = new PageInfo<Student>(lists);//封裝分頁數(shù)據(jù)
		model.addAttribute("pageInfo",pageInfo);
		return "studentlist";
	}

studentlist視圖文件中:

<c:forEach items="${pageInfo.list }" var="list">
    ${list.names }&nbsp;&nbsp;&nbsp;&nbsp;
    ${list.id }&nbsp;&nbsp;&nbsp;&nbsp;
    ${list.sex }<br>
    </c:forEach>
    
    <p>當(dāng)前${pageInfo.pageNum}頁,共${pageInfo.pages}頁,總共${pageInfo.total}條記錄</p>
    <c:if test="${!pageInfo.isFirstPage}">
  		  <a href="${pageContext.request.contextPath }/student/select?page=1">第一頁</a>
	</c:if>
	<c:if test="${pageInfo.hasPreviousPage}">
		<a href="${pageContext.request.contextPath }/student/select?page=${pageInfo.pageNum-1}">上一頁</a>
	</c:if>
	
	 <c:forEach items="${pageInfo.navigatepageNums }" var="n">
	 	<a href="${pageContext.request.contextPath }/student/select?page=${n }">${n }</a>
	</c:forEach>
	
	<c:if test="${pageInfo.hasNextPage}">
    	<a href="${pageContext.request.contextPath }/student/select?page=${pageInfo.pageNum+1}">下一頁</a>
	</c:if>
	<c:if test="${!pageInfo.isLastPage}">
  		  <a href="${pageContext.request.contextPath }/student/select?page=${pageInfo.pages}">末頁</a>
	</c:if>


<