开发者社区> 问答> 正文

SSH分页 无法执行第二,第三页查询?报错

使用的是SSH开发框架  在执行分页操作后点击第二页或者第三页想进行后续页面的选择是 myeclipse会自动跳错

报错的代码如下

protected List doList(final SessionImplementor session, final QueryParameters queryParameters)
			throws HibernateException {

		final boolean stats = getFactory().getStatistics().isStatisticsEnabled();
		long startTime = 0;
		if ( stats ) startTime = System.currentTimeMillis();

		List result;
		try {
			result = doQueryAndInitializeNonLazyCollections( session, queryParameters, true );
		}
		catch ( SQLException sqle ) {
			throw JDBCExceptionHelper.convert(
			        factory.getSQLExceptionConverter(),
			        sqle,
			        "could not execute query",
			        getSQLString()
				);
		}

		if ( stats ) {
			getFactory().getStatisticsImplementor().queryExecuted(
					getQueryIdentifier(),
					result.size(),
					System.currentTimeMillis() - startTime
				);
		}

		return result;
	}
// dao中根据页数检索编号的分页代码
	public List<WaterInfo> findAllPage(Integer first, Integer max) {
		try {
			String queryString = "from WaterInfo";
			System.out.print("dao_______");

			//Session session = getHibernateTemplate().getSessionFactory().openSession();
			//Query queryObject = session.createQuery(queryString);
			
			 Query queryObject = getSession().createQuery(queryString);
			
			//Query queryObject = getHibernateTemplate().getSessionFactory().openSession().createQuery(queryString);
			queryObject.setFirstResult(first);
			queryObject.setMaxResults(max);
			
			//session.close();
			
			 getSession().clear();
			 
			return queryObject.list();
		} catch (RuntimeException re) {
			throw re;
		}
	}

action中代码

public ActionForward findAllBook(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DynaActionForm dyForm = (DynaActionForm) form;// 定义一个动态ActionForm对象
		Integer page = (Integer) dyForm.get("page");// 取得页数
		if (page == null) {
			page = 1;
		}
		System.out.print(page+"action_______");
		Object[] o = waterInfoFacade.findBook(page, 10);
		System.out.println(page+"action after_______");
		Integer pageNum = (Integer) o[0];// 总页数
		List<WaterInfo> lb = (List<WaterInfo>) o[1];
		request.setAttribute("pageNum", pageNum);// 将pageNum放入Request对象中
		request.setAttribute("allBookList", lb);// 将lb放入Request对象中
		// 调用bookTypeFacade中findBook()方法
		return mapping.findForward("test");// 返回维护图书信息页面

	}

控制台的显示

我想请问下是不是hbm.xml或者application写错了 或者其他地方有问题造成难以执行程序 

展开
收起
爱吃鱼的程序员 2020-06-22 19:13:55 473 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    错误是啥?回复<aclass='referer'target='_blank'>@王瑞平:主键问题?就是我的hbn有问题?是否需要添加些什么?<idname="id"type="java.lang.Integer"><columnname="id"/><generatorclass="native"/></id><atarget='_blank'>@exa主键的问题,可能是数据类型问题我添加了控制台的图片和action中的代码希望可以把问题描述清楚些谢谢你的回复啊!程序运行但是点击第二页或者第三页后myeclipse跳出throwJDBCExceptionHelper.convert错误(不是出现在控制台的报错是myeclipse跳转到的loader.class文件中然后高亮此错误)不能运行查询第二页或者第三页hql语句

    从控制台输出来看问题出现在waterInfoFacade.findBook(page, 10);这个方法,代码需要贴出来看看


    您好我把代码贴出来了waterInfoDAO.findAllPage方法在上面也贴出来了这是方法的代码 DAO中的代码已上面贴出<preclass="brush:java;toolbar:true;auto-links:false;">publicObject[]findBook(Integerpage,Integermax){//设置总页数IntegerpageNum=0;if(waterInfoDAO.findAll().size()%max==0){pageNum=waterInfoDAO.findAll().size()/max;}else{pageNum=waterInfoDAO.findAll().size()/max+1;}List<WaterInfo>list=waterInfoDAO.findAllPage(max*(page-1),max);//将总页数和查出的List放到O中Object[]o=newObject[2];o[0]=pageNum;o[1]=list;returno;}

    初步判定是findAllBook方法的问题,你用以下代码试试:

    <preclass="brush:java;toolbar:true;auto-links:false;">Sessionsession=getHibernateTemplate().getSessionFactory().openSession();QueryqueryObject=session.createQuery(queryString);queryObject.setFirstResult(first);queryObject.setMaxResults(max);Listlist=queryObject.list();releaseSession(session);returnlist;

    改了以后试了一下还是这样会不会是我的application有问题?我在下面贴出来了

    application.xml 会不会是这个有问题 

    <preclass="brush:java;toolbar:true;auto-links:false;"><?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd"><!--定义数据源--><beanid="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"destroy-method="close"><!--指定连接数据库驱动--><propertyname="driverClassName"value="com.microsoft.jdbc.sqlserver.SQLServerDriver"/><!--指定连接数据库url--><propertyname="url"value="jdbc:microsoft:sqlserver://localhost:1433"/><!--指定连接数据库用户名,密码为空--><propertyname="username"value="sa"/><propertyname="password"value="sa"/></bean><!--定义Hibernate的sessionFactory--><beanid="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><!--指定数据源--><propertyname="dataSource"ref="dataSource"/><!--指定Hibernate映射文件--><propertyname="mappingResources"><list><value>sunyang/domain/LiberInfo.hbm.xml</value><value>domain/mayday/MayInfo.hbm.xml</value><value>domain/stationinfo/StationInfo.hbm.xml</value><value>domain/userinfo/UserInfo.hbm.xml</value><value>domain/maintaininfo/MaintainInfo.hbm.xml</value><value>domain/itemsetting/ItemInfo.hbm.xml</value><value>domain/data/DataInfo.hbm.xml</value><value>domain/water/WaterInfo.hbm.xml</value><value>domain/function/FunctionInfo.hbm.xml</value></list></property><propertyname="hibernateProperties"><props><!--指定使用方言--><propkey="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop><!--是否在控制台输出sql语句--><propkey="show_sql">false</prop><propkey="hibernate.hbm2ddl.auto">update</prop><propkey="hibernate.jdbc.batch_size">20</prop></props></property></bean><!--定义事务管理--><beanid="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><propertyname="sessionFactory"ref="sessionFactory"/></bean><!--定义事务管理拦截器--><beanid="transactionInterceptor"class="org.springframework.transaction.interceptor.TransactionInterceptor"><propertyname="transactionManager"ref="transactionManager"/><propertyname="transactionAttributes"><props><propkey="get*">PROPAGATION_REQUIRED,readOnly</prop><propkey="*">PROPAGATION_REQUIRED</prop></props></property></bean><!--定义代理自动管理事务--><beanid="ProxyCreator"class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"><!--指定需要Spring管理事务的Bean--><propertyname="beanNames"><list><value>sysLiberInfoFacade</value><value>mayInfoFacade</value><value>stationInfoFacade</value><value>userInfoFacade</value><value>maintainInfoFacade</value><value>itemInfoFacade</value><value>dataInfoFacade</value><value>waterInfoFacade</value><value>functionInfoFacade</value></list></property><!--调用事务管理拦截器--><propertyname="interceptorNames"><list><value>transactionInterceptor</value></list></property></bean><!--把DAO注入session工厂--><beanid="liberInfoDAO"class="sunyang.dao.LiberInfoDAOImpl"><propertyname="sessionFactory"><reflocal="sessionFactory"/></property></bean><beanid="mayInfoDAO"class="dao.mayday.MayInfoDAOImpl"><propertyname="sessionFactory"><reflocal="sessionFactory"/></property></bean><beanid="stationInfoDAO"class="dao.stationinfo.StationInfoDAOImpl"><propertyname="sessionFactory"><reflocal="sessionFactory"/></property></bean><beanid="userInfoDAO"class="dao.userinfo.UserInfoDAOImpl"><propertyname="sessionFactory"><reflocal="sessionFactory"/></property></bean><beanid="maintainInfoDAO"class="dao.maintaininfo.MaintainInfoDAOImpl"><propertyname="sessionFactory"><reflocal="sessionFactory"/></property></bean><beanid="itemInfoDAO"class="dao.itemsetting.ItemInfoDAOImpl"><propertyname="sessionFactory"><reflocal="sessionFactory"/></property></bean><beanid="dataInfoDAO"class="dao.data.DataInfoDAOImpl"><propertyname="sessionFactory"><reflocal="sessionFactory"/></property></bean><beanid="waterInfoDAO"class="dao.water.WaterInfoDAOImpl"><propertyname="sessionFactory"><reflocal="sessionFactory"/></property></bean><beanid="functionInfoDAO"class="dao.function.FunctionInfoDAOImpl"><propertyname="sessionFactory"><reflocal="sessionFactory"/></property></bean><!--把service注入给DAO--><beanid="sysLiberInfoFacade"class="sunyang.service.SysLiberInfoFacadeImpl"><propertyname="liberInfoDAO"ref="liberInfoDAO"></property></bean><beanid="mayInfoFacade"class="service.mayday.MayInfoFacadeImpl"><propertyname="mayInfoDAO"ref="mayInfoDAO"></property></bean><beanid="stationInfoFacade"class="service.stationinfo.StationInfoFacadeImpl"><propertyname="stationInfoDAO"ref="stationInfoDAO"></property></bean><beanid="userInfoFacade"class="service.userinfo.UserInfoFacadeImpl"><propertyname="userInfoDAO"ref="userInfoDAO"></property></bean><beanid="maintainInfoFacade"class="service.maintaininfo.MaintainInfoFacadeImpl"><propertyname="maintainInfoDAO"ref="maintainInfoDAO"></property></bean><beanid="itemInfoFacade"class="service.itemsetting.ItemInfoFacadeImpl"><propertyname="itemInfoDAO"ref="itemInfoDAO"></property></bean><beanid="dataInfoFacade"class="service.data.DataInfoFacadeImpl"><propertyname="dataInfoDAO"ref="dataInfoDAO"></property></bean><beanid="waterInfoFacade"class="service.water.WaterInfoFacadeImpl"><propertyname="waterInfoDAO"ref="waterInfoDAO"></property></bean><beanid="functionInfoFacade"class="service.function.FunctionInfoFacadeImpl"><propertyname="functionInfoDAO"ref="functionInfoDAO"></property></bean><!--把Action注入给service--><beanname="/back/sysLiberInfo"class="sunyang.webtier.SysLiberInfoAction"><propertyname="sysLiberInfoFacade"ref="sysLiberInfoFacade"></property></bean><beanname="/mayInfo"class="sunyang.webtier.MayInfoAction"><propertyname="mayInfoFacade"ref="mayInfoFacade"></property></bean><beanname="/back/stationInfo"class="sunyang.webtier.StationInfoAction"><propertyname="stationInfoFacade"ref="stationInfoFacade"></property></bean><beanname="/back/userInfo"class="sunyang.webtier.UserInfoAction"><propertyname="userInfoFacade"ref="userInfoFacade"></property></bean><beanname="/back/maintainInfo"class="sunyang.webtier.MaintainInfoAction"><propertyname="maintainInfoFacade"ref="maintainInfoFacade"></property></bean><beanname="/back/itemInfo"class="sunyang.webtier.ItemInfoAction"><propertyname="itemInfoFacade"ref="itemInfoFacade"></property></bean><beanname="/back/dataInfo"class="sunyang.webtier.DataInfoAction"><propertyname="dataInfoFacade"ref="dataInfoFacade"></property></bean><beanname="/back/waterInfo"class="sunyang.webtier.WaterInfoAction"><propertyname="waterInfoFacade"ref="waterInfoFacade"></property></bean><beanname="/back/functionInfo"class="sunyang.webtier.FunctionInfoAction"><propertyname="functionInfoFacade"ref="functionInfoFacade"></property></bean></beans>

    2020-06-22 19:14:12
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载