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