pageHelper分页插件的使用

it2023-09-10  74

pagehelper分页插件介绍

(1)pagehelper是什么? 针对Mybatis提供分页插件,将分页查询简化 (2)依赖配置 pagehelper (3)配置插件plugin 配置方法有两种

1 : mybatis核心配置文件,在application中引入 mybatis核心配置文件2 : 在application中配置

pagehelper分页插件使用步骤

导入pageHelper依赖 <!--引入pageHelper分页插件 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.0.0</version> </dependency> 配置插件

配置方式1:mybatis配置文件中配置

<plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <property name="reasonable" value="true"/> </plugin> </plugins>

同时在spring中引入mybatis的配置

<!-- session工厂--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- com.wzx.domain.Person person--> <property name="typeAliasesPackage" value="com.wzx.domain"/> 这里引入配置文件 ==>><property name="configLocation" value="classpath:SqlMapConfig.xml"/> </bean>

配置方式2:spring配置文件中中配置

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- com.wzx.domain.Person person--> <property name="typeAliasesPackage" value="com.wzx.domain"/> <!-- <property name="configLocation" value="classpath:SqlMapConfig.xml"/>--> <!-- PageHelper配置 --> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageInterceptor"> <property name="properties"> <!--使用下面的方式配置参数,一行配置一个 --> <!-- pageNum<=0 时会查询第一页 --> <!-- 指定数据库方言 --> <value> reasonable=true helperDialect=mysql </value> </property> </bean> </array> </property> </bean>

3.使用插件

@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class TestPageHelper { @Autowired private IDepartmentService departmentService; private static final Logger log = LoggerFactory.getLogger(TestPageHelper.class); @Test public void test01(){ //调用分页插件只要两行代码 PageHelper.startPage(1,10);//参1:单前页数 参2 每页记录数 //我们只需要做一个最简单的查询 List<Department> list = departmentService.findAllDepartments(); //将查询结果放入PageInfo中 PageInfo<Department> pageInfo = new PageInfo<>(list); log.info("test01 pageInfo="+pageInfo); } }
最新回复(0)