Hibernate实现mysql数据库 分页报错,100分救助
final String hql = "from SysWebsiteInfo where 1=1 ";
List<SysWebsiteInfo> list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(final Session session) throws HibernateException, SQLException
{
final Query query = session.createQuery(hql);
query.setMaxResults(10); //如果去掉这一行
query.setFirstResult(1); //再去掉这一行 可以正常查出来
return query.list();
}
});
----------------------------------------------------------------------
Struts Problem Report
Struts has detected an unhandled exception:
Messages: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'limit 1 10 syswebsite0_.id as id0_, syswebsite0_.address as address0_, syswebsit' at line 1
FreeMarker template error!
Method public java.lang.String org.hibernate.exception.NestableRuntimeException.getMessage(int) threw an exception when invoked on org.hibernate.exception.SQLGrammarException: could not execute query
The problematic instruction:
----------
==> ${msg[0]} [on line 68, column 29 in org/apache/struts2/dispatcher/error.ftl]
----------
Java backtrace for programmers:
----------
freemarker.template.TemplateModelException: Method public java.lang.String org.hibernate.exception.NestableRuntimeException.getMessage(int) threw an exception when invoked on org.hibernate.exception.SQLGrammarException: could not execute query
at freemarker.ext.beans.SimpleMethodModel.exec(SimpleMethodModel.java:130)
at freemarker.ext.beans.SimpleMethodModel.get(SimpleMethodModel.java:138)
at freemarker.core.DynamicKeyName.dealWithNumericalKey(DynamicKeyName.java:111)
at freemarker.core.DynamicKeyName._getAsTemplateModel(DynamicKeyName.java:90)
at freemarker.core.Expression.getAsTemplateModel(Expression.java:89)
at freemarker.core.Expression.getStringValue(Expression.java:93)
at freemarker.core.DollarVariable.accept(DollarVariable.java:76)
at freemarker.core.Environment.visit(Environment.java:209)
at freemarker.core.MixedContent.accept(MixedContent.java:92)
at freemarker.core.Environment.visit(Environment.java:209)
at freemarker.core.IfBlock.accept(IfBlock.java:82)
at freemarker.core.Environment.visit(Environment.java:209)
at freemarker.core.IteratorBlock$Context.runLoop(IteratorBlock.java:179)
at freemarker.core.Environment.visit(Environment.java:416)
at freemarker.core.IteratorBlock.accept(IteratorBlock.java:102)
at freemarker.core.Environment.visit(Environment.java:209)
at freemarker.core.MixedContent.accept(MixedContent.java:92)
at freemarker.core.Environment.visit(Environment.java:209)
at freemarker.core.IfBlock.accept(IfBlock.java:82)
at freemarker.core.Environment.visit(Environment.java:209)
at freemarker.core.MixedContent.accept(MixedContent.java:92)
at freemarker.core.Environment.visit(Environment.java:209)
at freemarker.core.Environment.process(Environment.java:189)
at freemarker.template.Template.process(Template.java:237)
at org.apache.struts2.dispatcher.Dispatcher.sendError(Dispatcher.java:748)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:505)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.NullPointerException
at freemarker.ext.beans.SimpleMemberModel.unwrapArguments(SimpleMemberModel.java:85)
at freemarker.ext.beans.SimpleMethodModel.exec(SimpleMethodModel.java:106)
... 39 more
[解决办法]
我这有个分页的用hibernate实现的 我可以发给你 QQ562470201
[解决办法]
主要有这样的问题,数据库与驱动的版本要对得上~仔细检查一下.
mysql :Ver 8.42 Distrib 5.1.54, for Win32 on ia32
mysql驱动:mysql-connector-java-5.0.4-bin.jar
代码为:
/*
* @pageNo 第几页
* @pageSize 每页数据条数
*/
@SuppressWarnings("unchecked")
public PageQueryResult findByPage(final String hql,final int pageNo, final int pageSize,final Map<Serializable, Serializable> queryMap) {
PageQueryResult page=new PageQueryResult(PageSettings.of(pageNo, pageSize));
final int totalRows = this.getTotalRows(hql,queryMap);
page.setTotalCount(totalRows);///记下总记录数
final int pageMaxNo = page.getPageMaxNo(pageSize,totalRows);
List list= (List) this.getHibernateTemplate().executeFind(new HibernateCallback() {
public List doInHibernate(Session session)
throws HibernateException, SQLException {
// 实际页码
int actualPage = (pageNo > pageMaxNo) ? pageMaxNo : pageNo;
// 计算实际每页的条数,如果请求的每页数据条数大于总条数, 则等于总条数
int actualSize = (pageSize > totalRows) ? totalRows : pageSize;
// 计算请求页码的第一条记录的索引值,如果
int startRow = (actualPage > 0) ? (actualPage - 1) * actualSize
: 0;
Query query = session.createQuery(hql).setProperties(queryMap);
// 设置第一条记录
query.setFirstResult(startRow);
query.setMaxResults(actualSize);
return query.list();
}
});
page.setResult(list);
return page;
}