Spring与Mybatis+PageHelper实现分页整合(三)

一.实现数据表的分页,这里用Mybatis分页插件-PageHepler实现

分页插件pagehelper.jar:

https://oss.sonatype.org/content/repositories/releases/com/github/pagehelper/pagehelper/ 
http://repo1.maven.org/maven2/com/github/pagehelper/pagehelper/ 
由于使用了sql解析工具,你还需要下载jsqlparser.jar

4.1.0及以后版本需要0.9.4版本

http://repo1.maven.org/maven2/com/github/jsqlparser/jsqlparser/0.9.4/ 
4.1.0以前版本需要0.9.1版本

http://repo1.maven.org/maven2/com/github/jsqlparser/jsqlparser/0.9.1/

二.在spring配置文件applicationContext.xml中配置PageHelper插件

<!--3 mybatis 会话工厂bean sqlSessionFactoryBean -->
	<bean id="sessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 数据源 -->
		<property name="dataSource" ref="dataSource" />
		<!-- 实体类别名 -->
		<property name="typeAliasesPackage" value="com.hlx.entity" />
		<!-- mapper sql映射文件路径 -->
		<property name="mapperLocations" value="classpath*:com/hlx/mapper/*Mapper.xml" />

		<!-- 配置mybatis-pageHelper插件 -->
		<property name="plugins">
			<array>
				<bean class="com.github.pagehelper.PageHelper">
					<property name="properties">
						<value>dialect=mysql</value>
					</property>
				</bean>
			</array>
		</property>

	</bean>

三.Dao接口

	List<Student> findCondition(Student student);// 根据条件查询数据

四.配置文件StudentMapper.xml

	<!-- 根据条件查询数据 -->
	<select id="findCondition" resultType="Student" parameterType="Student">
		select * from students
		<trim prefix="where" prefixOverrides="AND|OR">
			<if test="user!=null">
				user like concat('%',#{user},'%')
			</if>
			<if test="email!=null">
				and email like concat('%',#{email},'%')
			</if>
		</trim>
	</select>


五.Service接口

	PageInfo<Student> findPageList2(int pageIndex,int pageSize,Student student);// 查询所有的数据

六.实现Service接口类

@Override
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public PageInfo<Student> findPageList2(int pageIndex, int pageSize,
			Student student) {
		// 分页
		PageHelper.startPage(pageIndex, pageSize);

		// 判断student不能为空
		if (student != null) {
			// 调用业务方法
			List<Student> list = studentDao.findCondition(student);

			// 返回分页对象
			PageInfo<Student> pageInfo2 = new PageInfo<Student>(list);

			return pageInfo2;
		}
		return null;

	}

 七.Controller控制层

/**
	 * 根据条件分页数据
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String getAllPage2(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		
		//当前页
		int pageIndex=(request.getParameter("pageIndex")==null)?(1):(Integer.parseInt(request.getParameter("pageIndex")));
		
		//设置每页个数 默认是10条数据
		int pageSize=5;
		
		//请求参数值/
		String user=request.getParameter("user");
		String email=request.getParameter("email");
		
		//封装对象
		Student student=new Student(user,email);
		//
		
		
		// 调用业务方法
		PageInfo<Student> list = biz.findPageList2(pageIndex, pageSize,student);
		
		// 保存数据
		request.setAttribute("lists", list);
		request.setAttribute("stu", student);
		
		// 跳转路径
		return "list2.jsp";
		
	}

八.JSP页面

 <form action="getAllPage2.action" method="post">
   <fieldset>
    <legend>根据条件查询数据</legend>
    <div>
       user:<input type="text" name="user" value="${stu.user }"/>
       email:<input type="text" name="email" value="${stu.email }"/>
     <input type="submit" value="查询"/>
    </div>
  </fieldset>
	<!-- <form action="del2.action" method="post"> -->
		<div class="main">

			<table width="100%" border="1">
				<caption>
					<h1>学生信息</h1>
				</caption>
				<tr align="center">
					<td width="24"><input type="checkbox" name="checkbox"
						id="checkbox" /> <label for="checkbox"></label></td>
					<td width="26">id</td>
					<td width="90">user</td>
					<td width="157">email</td>
					<td width="222">date</td>
					<td width="35">age</td>
					<td width="112">操作</td>
				</tr>
				
				<c:if test="${lists.list.size()!=0}">
				
				<c:forEach var="stu" items="${lists.list}">
					<tr align="center">
						<td><input type="checkbox" name="checkbox2" id="checkbox2"
							value="${stu.id }" /> <label for="checkbox2"></label></td>
						<td>${stu.id }</td>
						<td>${stu.user }</td>
						<td>${stu.email }</td>
						<td><fmt:formatDate value="${stu.date }" type="date" />
						</td>
						<td>${stu.age }</td>
						<td><a href="javascript:fun(${stu.id })">删除</a>/<a
							href="find.action?id=${stu.id }">修改</a></td>
					</tr>
				</c:forEach>
			</table>
			<p>
				第${lists.pageNum }/${lists.lastPage }页   

				<c:if test="${!lists.isFirstPage}">
					<a href="javascript:funPage(${lists.firstPage })">首页</a>
					<a href="javascript:funPage(${lists.prePage })">上一页</a>
				</c:if>

				<!--显示所有的页数 -->
				<c:forEach items="${lists.navigatepageNums }" var="i">
					<a href="getAllPage2.action?pageIndex=${i}">${i}</a>
				</c:forEach>


				<c:if test="${!lists.isLastPage}">
					<a href="javascript:funPage(${lists.nextPage })">下一页</a>
					<a href="javascript:funPage(${lists.lastPage })">尾页</a>
				</c:if>

			</p>
    </c:if>
    <c:if test="${lists.list.size()==0}">
    	<tr align="center" style="color:red;"><td colspan="7">没有您要查找的数据</td></tr>
    </c:if>
			<p style="color:red;">${msg}</p>
			<p>
				<a href="studentAdd.jsp">添加</a> <input type="submit"
					value="选择删除多条数据">
			</p>
		</div>
	<!-- </form> -->
	
	 </form>

九 JS

function fun(id){
	if(window.confirm("您确定要删除吗?")){
		document.location.href="del.action?id="+id;
	}
	
}

/**
 * 分页跳转
 * @param index
 */
function funPage(index){
	document.forms[0].action="getAllPage2.action?pageIndex="+index; //指定路径
	document.forms[0].submit();  //提交
}

十.效果







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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值