1. 程式人生 > >mybatis分頁外掛配置

mybatis分頁外掛配置

首先在pom.xml中新增依賴

<dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>4.2.1</version>
        </dependency>
        <dependency>
            <groupId>com.github.jsqlparser</groupId>
            <artifactId>jsqlparser</artifactId>
            <version>0.9.5</version>
        </dependency>

然後在spring-mybatis.xml中新增如下配置

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<!-- 自動掃描mapping.xml檔案 -->
		<property name="mapperLocations" value="classpath:com/anxyj/mapping/*.xml"></property>
		<property name="plugins">
			<array>
				<bean class="com.github.pagehelper.PageHelper">
					<property name="properties">
						<value>
							dialect=mysql
							offsetAsPageNum=true
							rowBoundsWithCount=true
							pageSizeZero=true
							reasonable=false
							supportMethodsArguments=false
							returnPageInfo=none
						</value>
					</property>
				</bean>
			</array>
		</property>
</bean>

運用:

public List<User> showAll(int page, int pageSize) {

PageHelper.startPage(page, pageSize);
List<User> users = userDao.selectAll();
return users;

}

PageHelper.startPage(1, 10);括號中的引數與sql語句中的limit不同,這裡表示展示第一頁的十條資料

效果