package com.ita.config; import com.github.pagehelper.PageInterceptor; import org.apache.ibatis.plugin.Interceptor; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.mapper.MapperScannerConfigurer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import javax.sql.DataSource; import java.util.Properties; public class MyBatisConfig { /**配置PageInterceptor分頁插件*/ @Bean public PageInterceptor getPageInterceptor() { PageInterceptor pageIntercptor = new PageInterceptor(); Properties properties = new Properties(); properties.setProperty("value", "true"); pageIntercptor.setProperties(properties); return pageIntercptor; } /* 定義MyBatis的核心連接工廠bean, 等同於<bean class="org.mybatis.spring.SqlSessionFactoryBean"> 參數使用自動裝配的形式加載dataSource, 為set注入提供數據源,dataSource來源於JdbcConfig中的配置 */ @Bean public SqlSessionFactoryBean getSqlSessionFactoryBean(@Autowired DataSource dataSource,@Autowired PageInterceptor pageIntercptor){ SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean(); //等同於<property name="dataSource" ref="dataSource"/> ssfb.setDataSource(dataSource); Interceptor[] plugins={pageIntercptor}; ssfb.setPlugins(plugins); return ssfb; } }
1.在 pom.xml 中添加如下:
<properties> <pagehelper-version>5.0.0</pagehelper-version> <jsqlparser-version>0.9.5</jsqlparser-version> </properties> <!--pagehelper--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>${pagehelper-version}</version> </dependency> <!-- pagehelper的依賴包:jsqlparser --> <dependency> <groupId>com.github.jsqlparser</groupId> <artifactId>jsqlparser</artifactId> <version>${jsqlparser-version}</version> </dependency>
2.在 spring-mybatis.xml 中添加如下
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 自動掃描mapping.xml文件 --> <property name="mapperLocations" value="classpath*:mapping/**/*.xml"></property> <!--pageHelper--> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageInterceptor"> <property name="properties"> <!--使用下面的方式配置參數,一行配置一個 --> <value> helperDialect=postgresql reasonable=true supportMethodsArguments=true params=count=countSql autoRuntimeDialect=true </value> </property> </bean> </array> </property> </bean>
在 userDao 中
ArrayList<Users> getUserList();
在 mapping.xml 中
<select id="getUserList" resultType="com.hanpeng.entity.Users"> select * from tb_users </select>
在 userService 中
public void test(){
PageHelper.startPage(1, 5);
ArrayList<Users> list = userDao.getUserList();
PageInfo<Users> page = new PageInfo<Users>(list);
System.out.println("總數量:" + page.getTotal());
System.out.println("當前頁查詢記錄:" + page.getList().size());
System.out.println("當前頁碼:" + page.getPageNum());
System.out.println("每頁顯示數量:" + page.getPageSize());
System.out.println("總頁:" + page.getPages());
}
在 controller 中
@RequestMapping("pageHelperTest")
public void getList(){
userService.test();
}
REF
https://blog.csdn.net/qq_39200980/article/details/104895549
https://blog.csdn.net/qq_33609401/article/details/83749083
https://blog.csdn.net/qq_39765090/article/details/87174950
