关于pagehelper分页的问题

weixin_38521872 2017-09-11 04:08:04
最近学校实训在写一个初级的ssm项目,分页想用pagehelper但是用了怎么也不能完成分页
这是我的查询语句

<select
id="findCommon"
resultMap="CommonResultMap">
select <include refid="Common_Column_List" />
from common
<trim
prefix="where"
suffixOverrides="and">
<if test="name != null">
name = #{name,jdbcType=VARCHAR}
</if>
<if test="email != null">
email=#{email,jdbcType=VARCHAR}
</if>
<if test="qq != null">
qq=#{qq,jdbcType=VARCHAR}
</if>
<if test="idcard != null">
idcard=#{idcard,jdbcType=VARCHAR}
</if>
<if test="ethnic != null">
ethnic=#{ethnic,jdbcType=VARCHAR}
</if>
<if test="diploma != null">
diploma=#{diploma,jdbcType=VARCHAR}
</if>
<if test="bank != null">
bank=#{bank,jdbcType=VARCHAR}
</if>

</trim>

</select>

这是Controller

@RequestMapping(value = "findCommon", method = RequestMethod.GET)
public void findCommon(@RequestParam(value = "pn", defaultValue = "1") Integer pn, Common common,
HttpServletRequest request, HttpServletResponse response) throws IOException {
PrintWriter pw = response.getWriter();
System.out.println(common.getBank());
String result = "";
System.out.println("pn:" + pn);
HttpSession session = request.getSession();
List<Common> commonList = commonService.findCommon();
System.out.println("list:" + commonList.size());
PageHelper.startPage(pn, 10);
PageInfo<Common> page = new PageInfo<Common>(commonList, 5);

session.setAttribute("pageInfo", commonList);
result = "allok";
pw.write(result);
pw.flush();
pw.close();

}

这是前台涉及到分页的代码

<c:forEach items="${pageInfo }" var = "list">
<tr>
<td>${list.id}</td>
<td>${list.name}</td>
<td>${list.email}</td>
<td>${list.phone}</td>
<td>${list.qq}</td>
<td>${list.idcard}</td>
<td>${list.ethnic}</td>
<td>${list.diploma}</td>
<td>${list.bank}</td>
<td scope="col"><a href="#"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span></a></td>
</tr>
</c:forEach>

......

<script type="text/javascript">
function findCommon() {
var common = {

name : $("*[name='name']").val(),
email : $("*[name='email']").val(),
phone : $("*[name='phone']").val(),
qq : $("*[name='qq']").val(),
idcard : $("*[name='idcard']").val(),
ethnic : $("*[name='ethnic']").val(),
diploma : $("*[name='diploma']").val(),
bank : $("*[name='bank']").val()
};
$.ajax({
cache : true,
type : "GET",
url : "${myappurl}findCommon.do",
data : common,
async : true,
success : function(data) {
if (data == "allok") {
alert("查询成功");
} else {
alert("查询失败");
}
}
});
}
</script>

思路是点击查询按钮根据条件查出相应信息,但是点击后没反应浏览器报这个错误:
GET http://localhost:8080/issHR02/findCommon.do?name=&phone=&idcard=&diploma=&bank= net::ERR_CONNECTION_REFUSED
在地址栏手动传pn的值 ....?pn=1就可以查所有数据,但是也没有分页显示而是显示出所有的在一页,想问下到底是什么原因导致的,或者是我少写了些什么,谢谢各位大佬!!!

...全文
2848 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
ggw 2020-03-15
  • 打赏
  • 举报
回复
网上到处都是这个控件的用法,它的官网上就有例子,抄过来就可以用。
jncoder 2020-03-10
  • 打赏
  • 举报
回复
还在用PageHelper ?试试 [github sqlhelper](https://github.com/fangjinuo/sqlhelper)吧,
1)支持几乎所有的关系数据库,包括所有的国产数据库。
2)支持mybatis, mybatisplus, Spring-JDBC,Apache Commons-DBUtils, jfinal, mango, ebean 等数据库操作框架。
3)可以无缝从PageHelper切换过去,不需要修改代码
4) 自动识别数据库,0配置,支持多种数据库同时操作
5)支持自动转义 Like 子句参数
6)支持多种自定义排序方式
7)支持通用 批量操作
8)支持dump DDL
9) 支持子查询分页
10) 充分利用PreparedStatement,比pagehelper更高效
11)支持与Spring,SpringBoot快速集成
12)更多特性等待着你
_Ricky_ 2020-03-10
  • 打赏
  • 举报
回复
最后解决了吗?和mapper放到一起就可以了?
flying~closer 2020-03-10
  • 打赏
  • 举报
回复
引用 14 楼 Amber.Li 的回复:
PageHelper.startPage(pn, 10); 是针对持久化操作拦截的。需要跟mapper放到一起,你直接和server放到一起肯定没有效果了。 而且必须挨到一起放,这样: PageHelper.startPage(pn, 10); xxxMapper.findCommon(xxx)
这位仁兄说的对
licip 2020-03-10
  • 打赏
  • 举报
回复
HttpSession session = request.getSession(); PageHelper.startPage(pn, 10); List<Common> commonList = commonService.findCommon(); PageInfo<Common> page = new PageInfo<Common>(commonList); System.out.println("list:" + commonList.size()); 把代码写成这样,试试
AM18 2019-01-15
  • 打赏
  • 举报
回复
PageHelper.startPage(pn, 10); 是针对持久化操作拦截的。需要跟mapper放到一起,你直接和server放到一起肯定没有效果了。
而且必须挨到一起放,这样:
PageHelper.startPage(pn, 10);
xxxMapper.findCommon(xxx)
Sunlalalla 2019-01-14
  • 打赏
  • 举报
回复
https://blog.csdn.net/Sun_of_Rainy/article/details/83382085 需要在配置文件配置,希望这个可以帮助到你
m0_37652576 2019-01-14
  • 打赏
  • 举报
回复
是不是commonService.findCommon()中用了多条查询语句
码匠笔记 2019-01-14
  • 打赏
  • 举报
回复
简单粗暴,如果今天还调整不好,直接用MySQL 的 limit 就好了 没必要在工具上浪费太多的时间
hjy_v_v 2019-01-14
  • 打赏
  • 举报
回复
session.setAttribute("pageInfo", page.getList);试试
亲爱的Joe 2019-01-14
  • 打赏
  • 举报
回复
<?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>
<settings>
<!-- 打印查询语句 -->
<setting name="logImpl" value="STDOUT_LOGGING"/>
<!--解决,查询返回结果含null没有对应字段值问题 -->
<setting name="callSettersOnNulls" value="true"/>
</settings>
<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库 -->
<property name="dialect" value="sqlserver"/>
</plugin>
</plugins>
</configuration>


把这个设置到你的sqlSessionFactory,打印下你的sql语句,看看是不是查询语句有问题
微瞰技术 2018-03-09
  • 打赏
  • 举报
回复
解决了吗,我也是返回了所有数据。
weixin_38521872 2017-09-11
  • 打赏
  • 举报
回复
DEBUG

DEBUG 2017-09-11 21:32:12,025 org.springframework.web.servlet.DispatcherServlet: Rendering view [org.springframework.web.servlet.view.JstlView: name 'myhome'; URL [/myhome.jsp]] in DispatcherServlet with name 'myhos'
DEBUG 2017-09-11 21:32:12,025 org.springframework.web.servlet.view.InternalResourceView: Forwarding to resource [/myhome.jsp] in InternalResourceView 'myhome'
DEBUG 2017-09-11 21:32:12,026 org.springframework.web.servlet.FrameworkServlet: Successfully completed request
DEBUG 2017-09-11 21:32:27,024 org.springframework.web.servlet.DispatcherServlet: DispatcherServlet with name 'myhos' processing GET request for [/issHR02/findCommon.do]
DEBUG 2017-09-11 21:32:27,025 org.springframework.web.servlet.handler.AbstractHandlerMethodMapping: Looking up handler method for path /findCommon.do
DEBUG 2017-09-11 21:32:27,025 org.springframework.web.servlet.handler.AbstractHandlerMethodMapping: Returning handler method [public void com.hcy.controller.CommonController.findCommon(java.lang.Integer,com.hcy.model.Common,org.springframework.ui.Model) throws java.io.IOException]
DEBUG 2017-09-11 21:32:27,027 org.springframework.beans.factory.support.AbstractBeanFactory: Returning cached instance of singleton bean 'commonController'
DEBUG 2017-09-11 21:32:27,027 org.springframework.web.servlet.DispatcherServlet: Last-Modified value for [/issHR02/findCommon.do] is: -1
DEBUG 2017-09-11 21:32:27,029 org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl: Creating a new SqlSession
DEBUG 2017-09-11 21:32:27,029 org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl: SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@aed2757] was not registered for synchronization because synchronization is not active
DEBUG 2017-09-11 21:32:27,030 org.springframework.jdbc.datasource.DataSourceUtils: Fetching JDBC Connection from DataSource
DEBUG 2017-09-11 21:32:27,031 org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl: JDBC Connection [com.mysql.jdbc.Connection@768441d2] will not be managed by Spring
DEBUG 2017-09-11 21:32:27,031 org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl: ooo Using Connection [com.mysql.jdbc.Connection@768441d2]
DEBUG 2017-09-11 21:32:27,031 org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl: ==>  Preparing: select id,name,email,phone ,qq,idcard,ethnic,diploma,bank from common 
DEBUG 2017-09-11 21:32:27,032 org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl: ==> Parameters: 
DEBUG 2017-09-11 21:32:27,034 org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl: Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@aed2757]
DEBUG 2017-09-11 21:32:27,035 org.springframework.jdbc.datasource.DataSourceUtils: Returning JDBC Connection to DataSource
DEBUG 2017-09-11 21:32:27,036 org.springframework.web.servlet.DispatcherServlet: Rendering view [org.springframework.web.servlet.view.JstlView: name 'findCommon'; URL [/findCommon.jsp]] in DispatcherServlet with name 'myhos'
DEBUG 2017-09-11 21:32:27,036 org.springframework.web.servlet.view.AbstractView: Added model object 'common' of type [com.hcy.model.Common] to request in view with name 'findCommon'
DEBUG 2017-09-11 21:32:27,036 org.springframework.web.servlet.view.AbstractView: Added model object 'org.springframework.validation.BindingResult.common' of type [org.springframework.validation.BeanPropertyBindingResult] to request in view with name 'findCommon'
DEBUG 2017-09-11 21:32:27,036 org.springframework.web.servlet.view.AbstractView: Added model object 'pageInfo' of type [com.github.pagehelper.PageInfo] to request in view with name 'findCommon'
DEBUG 2017-09-11 21:32:27,036 org.springframework.web.servlet.view.AbstractView: Added model object 'org.springframework.validation.BindingResult.pageInfo' of type [org.springframework.validation.BeanPropertyBindingResult] to request in view with name 'findCommon'
DEBUG 2017-09-11 21:32:27,036 org.springframework.web.servlet.view.InternalResourceView: Forwarding to resource [/findCommon.jsp] in InternalResourceView 'findCommon'
DEBUG 2017-09-11 21:32:27,037 org.springframework.web.servlet.FrameworkServlet: Successfully completed request
weixin_38521872 2017-09-11
  • 打赏
  • 举报
回复
引用 5 楼 qq_21545211 的回复:
 PageHelper.startPage(pn, 10);  List<Common> commonList = commonService.findCommon(); 分页有顺序的,先startPage,然后在查询数据,PageHelper是基于底层的mybatis分页
是先startPage才进行查询的啊..
Ctrls8 2017-09-11
  • 打赏
  • 举报
回复
 PageHelper.startPage(pn, 10);  List<Common> commonList = commonService.findCommon(); 分页有顺序的,先startPage,然后在查询数据,PageHelper是基于底层的mybatis分页
weixin_38521872 2017-09-11
  • 打赏
  • 举报
回复
引用 3 楼 aw11100 的回复:
按道理应该不会有什么问题了,把你的xml配置文件发出来看看。
web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Homework8341</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
            classpath:applicationContext.xml
    </param-value>
  </context-param>
  <context-param>
    <param-name>contextConfigLocation2</param-name>
    <param-value>
            classpath:spring-mybatis.xml
    </param-value>
  </context-param>
  <session-config>
    <session-timeout>30</session-timeout>
  </session-config>
  <listener>
    <listener-class>
                org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>
  <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <async-supported>true</async-supported>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <servlet>
    <servlet-name>myhos</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>myhos</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>
myhos-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans.xsd    
        http://www.springframework.org/schema/context     
        http://www.springframework.org/schema/context/spring-context.xsd    
        http://www.springframework.org/schema/mvc     
        http://www.springframework.org/schema/mvc/spring-mvc.xsd"  
    default-autowire="byName">

	<!-- 扫描 注解 -->
	<context:component-scan base-package="com.hcy" />

	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/" />
		<property name="suffix" value=".jsp" />
	</bean>
	<context:annotation-config />
	<mvc:annotation-driven />
	
</beans>
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>
<!--mybatis分页插件  -->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PaginationInterceptor">
            <property name="reasonable" value="true"/>
            <property name="dialect" value="mysql"/>
        <!-- 该参数默认为false -->
        <!-- 设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用 -->
        <!-- 和startPage中的pageNum效果一样-->
        <property name="offsetAsPageNum" value="true"/>
        <!-- 该参数默认为false -->
        <!-- 设置为true时,使用RowBounds分页会进行count查询 -->
        <property name="rowBoundsWithCount" value="true"/>
        <!-- 设置为true时,如果pageSize=0或者RowBounds.limit = 0就会查询出全部的结果 -->
        <!-- (相当于没有执行分页查询,但是返回结果仍然是Page类型)-->
        <property name="pageSizeZero" value="true"/>
        <!-- 3.3.0版本可用 - 分页参数合理化,默认false禁用 -->
        <!-- 启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页 -->
        <!-- 禁用合理化时,如果pageNum<1或pageNum>pages会返回空数据 -->
        </plugin>
    </plugins>
     
</configuration>
aw11100 2017-09-11
  • 打赏
  • 举报
回复
按道理应该不会有什么问题了,把你的xml配置文件发出来看看。
weixin_38521872 2017-09-11
  • 打赏
  • 举报
回复
引用 1 楼 aw11100 的回复:
pageHelp 要在你的查询之前 不然你先全部查询了 在用pageHelper就没有意义了,我没遇到过这种情况,问题应该就出在这里吧


@RequestMapping(value = "findCommon", method = RequestMethod.GET)
public void findCommon(@RequestParam(value = "pn", defaultValue = "1") Integer pn, Common common,
HttpServletRequest request, HttpServletResponse response) throws IOException {
PrintWriter pw = response.getWriter();
System.out.println(common.getBank());
String result = "";
System.out.println("pn:" + pn);
HttpSession session = request.getSession();
PageHelper.startPage(pn, 10);

List<Common> commonList = commonService.findCommon();
PageInfo<Common> page = new PageInfo<Common>(commonList, 10);
System.out.println("list:" + commonList.size());

session.setAttribute("pageInfo", page);
result = "allok";
pw.write(result);
pw.flush();
pw.close();

}

这是我修改后的,但是还是一次显示了全部数据
aw11100 2017-09-11
  • 打赏
  • 举报
回复
pageHelp 要在你的查询之前 不然你先全部查询了 在用pageHelper就没有意义了,我没遇到过这种情况,问题应该就出在这里吧

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧