PageHelper下载地址:https://oss.sonatype.org/content/repositories/releases/com/github/pagehelper/pagehelper/
先看看我的实现分页的主要代码
@Controller
public class HistoryController {
@Autowired
private Historyservice historyservice;
@RequestMapping("showHistory")
@ResponseBody
public PageInfo showHistory(@RequestParam("pageNum")int pageNum){
int pageSize=10;
//引入分页查询,使用PageHelper分页功能
//在查询之前传入当前页,然后多少记录
PageHelper.startPage(pageNum,pageSize,"htime DESC");
//startPage后紧跟的这个查询就是分页查询
List historylist=historyservice.showHisotory();
//使用PageInfo包装查询结果,只需要将pageInfo交给页面就可以
PageInfo pageInfo = new PageInfo(historylist);
return pageInfo;
}
}
结果查出来并不理想:实际情况是没有分页,返回我所有数据库中数据并且把按照时间(我指定的字段)进行降序排列。
后来进行网上查阅,感谢各大网友的奉献精神!
发现自己并没有进行配置。所以接下来:
在mybatis-config.xml中配置一下内容:
视图为:
然后开始运行tomcat,出现错误:
错误内容如下:
严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in file [G:\eclipse java ee\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\ZJKou\WEB-INF\classes\spring\applicationContext-dao.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse config resource: ServletContext resource [/WEB-INF/classes/mybatis/sqlMapConfig.xml]; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.lang.RuntimeException: 初始化 dialect [mysql]时出错:null
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1486)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:524)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:589)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:383)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5141)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5664)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1702)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1692)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.core.NestedIOException: Failed to parse config resource: ServletContext resource [/WEB-INF/classes/mybatis/sqlMapConfig.xml]; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.lang.RuntimeException: 初始化 dialect [mysql]时出错:null
at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:434)
at org.mybatis.spring.SqlSessionFactoryBean.afterPropertiesSet(SqlSessionFactoryBean.java:340)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1545)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1483)
... 21 more
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.lang.RuntimeException: 初始化 dialect [mysql]时出错:null
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:109)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:92)
at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:428)
... 24 more
Caused by: java.lang.RuntimeException: 初始化 dialect [mysql]时出错:null
at com.github.pagehelper.util.SqlUtil.initDialect(SqlUtil.java:202)
at com.github.pagehelper.util.SqlUtil.setProperties(SqlUtil.java:286)
at com.github.pagehelper.PageHelper.setProperties(PageHelper.java:60)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.pluginElement(XMLConfigBuilder.java:143)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:100)
... 26 more
发现重点是:
初始化 dialect [mysql]时出错:null
于是通过再一次搜索,发现了还有一个jar包没有使用。
那就是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/
然后将下载的包添加进项目中:点击Add External JARs-》在自己文件中找到自己下载好的pageHelper.jar包-》Apply
最后成功的实现了排序分页,结果显示:
-----------------------------------------------华丽分割线----------------------------------------------------------
如果您在我之前所遇到的问题讲述没有看明白如何具体实现的,接下来我来总结一下。
一、需要下载两个jar包
1.PageHelper下载地址:https://oss.sonatype.org/content/repositories/releases/com/github/pagehelper/pagehelper/
(这个包的版本自己选择下载,里面也有pom等格式)
2.那就是sql解析工具--jsqlparser.jar
如果你下载的PageHelper4.1.0及以后版本需要jsqlparser0.9.4版本
http://repo1.maven.org/maven2/com/github/jsqlparser/jsqlparser/0.9.4/
如果你下载的PageHelper4.1.0以前版本需要jsqlparser0.9.1版本
http://repo1.maven.org/maven2/com/github/jsqlparser/jsqlparser/0.9.1/
然后两个包添加到项目中。
二、mybatis-config.xml中配置pageHelper的相关信息
三、代码中实现分页
That‘s ok!