最近學校實訓在寫一個初級的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="https://bbs.csdn.net/topics/#"><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就可以查所有資料,但是也沒有分頁顯示而是顯示出所有的在一頁,想問下到底是什么原因導致的,或者是我少寫了些什么,謝謝各位大佬!!!
uj5u.com熱心網友回復:
pageHelp 要在你的查詢之前 不然你先全部查詢了 在用pageHelper就沒有意義了,我沒遇到過這種情況,問題應該就出在這里吧uj5u.com熱心網友回復:
@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();
}
這是我修改后的,但是還是一次顯示了全部資料
uj5u.com熱心網友回復:
按道理應該不會有什么問題了,把你的xml組態檔發出來看看。uj5u.com熱心網友回復:
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="https://bbs.csdn.net/" />
<property name="suffix" value="https://bbs.csdn.net/topics/.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="https://bbs.csdn.net/topics/true"/>
<property name="dialect" value="https://bbs.csdn.net/topics/mysql"/>
<!-- 該引數默認為false -->
<!-- 設定為true時,會將RowBounds第一個引數offset當成pageNum頁碼使用 -->
<!-- 和startPage中的pageNum效果一樣-->
<property name="offsetAsPageNum" value="https://bbs.csdn.net/topics/true"/>
<!-- 該引數默認為false -->
<!-- 設定為true時,使用RowBounds分頁會進行count查詢 -->
<property name="rowBoundsWithCount" value="https://bbs.csdn.net/topics/true"/>
<!-- 設定為true時,如果pageSize=0或者RowBounds.limit = 0就會查詢出全部的結果 -->
<!-- (相當于沒有執行分頁查詢,但是回傳結果仍然是Page型別)-->
<property name="pageSizeZero" value="https://bbs.csdn.net/topics/true"/>
<!-- 3.3.0版本可用 - 分頁引數合理化,默認false禁用 -->
<!-- 啟用合理化時,如果pageNum<1會查詢第一頁,如果pageNum>pages會查詢最后一頁 -->
<!-- 禁用合理化時,如果pageNum<1或pageNum>pages會回傳空資料 -->
</plugin>
</plugins>
</configuration>
uj5u.com熱心網友回復:
PageHelper.startPage(pn, 10);List<Common> commonList = commonService.findCommon();
分頁有順序的,先startPage,然后在查詢資料,PageHelper是基于底層的mybatis分頁
uj5u.com熱心網友回復:
是先startPage才進行查詢的啊..
uj5u.com熱心網友回復:
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
uj5u.com熱心網友回復:
解決了嗎,我也是回傳了所有資料。uj5u.com熱心網友回復:
是不是commonService.findCommon()中用了多條查詢陳述句uj5u.com熱心網友回復:
https://blog.csdn.net/Sun_of_Rainy/article/details/83382085需要在組態檔配置,希望這個可以幫助到你
uj5u.com熱心網友回復:
<?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="https://bbs.csdn.net/topics/STDOUT_LOGGING"/>
<!--解決,查詢回傳結果含null沒有對應欄位值問題 -->
<setting name="callSettersOnNulls" value="https://bbs.csdn.net/topics/true"/>
</settings>
<plugins>
<!-- com.github.pagehelper為PageHelper類所在包名 -->
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 設定資料庫型別 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六種資料庫 -->
<property name="dialect" value="https://bbs.csdn.net/topics/sqlserver"/>
</plugin>
</plugins>
</configuration>
把這個設定到你的sqlSessionFactory,列印下你的sql陳述句,看看是不是查詢陳述句有問題
uj5u.com熱心網友回復:
session.setAttribute("pageInfo", page.getList);試試uj5u.com熱心網友回復:
簡單粗暴,如果今天還調整不好,直接用MySQL 的 limit 就好了沒必要在工具上浪費太多的時間
uj5u.com熱心網友回復:
PageHelper.startPage(pn, 10); 是針對持久化操作攔截的。需要跟mapper放到一起,你直接和server放到一起肯定沒有效果了。而且必須挨到一起放,這樣:
PageHelper.startPage(pn, 10);
xxxMapper.findCommon(xxx)
uj5u.com熱心網友回復:
還在用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)更多特性等待著你
uj5u.com熱心網友回復:
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());
把代碼寫成這樣,試試
uj5u.com熱心網友回復:
這位仁兄說的對
uj5u.com熱心網友回復:
最后解決了嗎?和mapper放到一起就可以了?uj5u.com熱心網友回復:
網上到處都是這個控制元件的用法,它的官網上就有例子,抄過來就可以用。轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/118803.html
標籤:Web 開發
