MyBatis外掛PageHelper的分頁使用教程
阿新 • • 發佈:2019-02-01
最近琢磨了一下這個分頁,分享使用步驟給大家
1.加入maven包
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>最新版本</version> </dependency>
2.配置xml
SqlMapConfig.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> <!--分頁外掛--> <plugins> <plugin interceptor="com.github.pagehelper.PageHelper"> <property name="dialect" value="mysql"/> </plugin> </plugins> </configuration>
引入配置
<!-- 讓spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 資料庫連線池 -->
<property name="dataSource" ref="dataSource" />
<!-- 載入mybatis的全域性配置檔案 -->
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
</bean>
具體使用
PageHelper.startPage(page, size); TestExample where = new TestExample(); TestExample.Criteria c=where.createCriteria(); c.andTitleLike("%"+key+"%"); List<Obiect> list=testMapper.selectByExample(where); PageInfo<Obiect> pageInfo = new PageInfo<Obiect>(list);
這個PageHelper具體就是在正常查詢過程中,插入上面的兩段紅色程式碼,這樣就完成了分頁查詢。
從pageInfo裡面就可以取出所需要的內容。