Mybatis-pageHelper for paging, mybatis-pagehelper
Mybatis-PageHelpera is a good third-party paging plug-in that supports many databases and almost all mainstream databases.
Github address: https://github.com/pagehelper/Mybatis-PageHelper
Address: https://gitee.com/free/Mybatis_PageHelper
Next, let's try to use this paging plug-in.
- Introduce third-party jar
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.0.0</version></dependency>
- Write interception configuration in application. xml
<Bean id = "demoSqlSessionFactory" class = "org. mybatis. spring. SqlSessionFactoryBean"> <! -- DataSource attribute specifies the connection pool to be used --> <property name = "dataSource" ref = "dataSource"/> <! -- ConfigLocation attribute specifies the core configuration file of mybatis --> <property name = "configLocation" value = "classpath: configuration. xml "/> <property name =" mapperLocations "value =" classpath: com/yihaomen/mybatis/model /*. xml "/> <property name =" typeAliasesPackage "value =" com. yihaomen. mybatis. model "/> <property name =" plugins "> <array> <bean class =" com. github. pagehelper. pageInterceptor "> <property name =" properties "> <value> helperDialect = mysql reasonable = true supportMethodsArguments = true params = count = countSql autoRuntimeDialect = true </value> </property> </bean> </array> </property> </bean>
3. Write the controller Layer
@RequestMapping("/pageList") public ModelAndView pageUserList(ModelAndView mv, @RequestParam(required = true, defaultValue = "1") Integer pageNum, @RequestParam(required = true, defaultValue = "3") Integer pageSize) {PageHelper.startPage(pageNum, pageSize);List<Article> list = articleService.getAllArticles();PageInfo<Article> pageInfo = new PageInfo<Article>(list);mv.addObject("list", list);mv.addObject("page", pageInfo);mv.setViewName("list2");return mv;}4. Write the list2.jsp page
<% @ Page language = "java" contentType = "text/html; charset = UTF-8 "pageEncoding =" UTF-8 "%> <% @ taglib prefix =" c "uri =" http://java.sun.com/jsp/jstl/core "%>
Https://gitee.com/huayicompany/springmvc-mybatis