依赖架包
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.0.4</version>
</dependency>
mybatis-config.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.PageInterceptor">
<!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->
</plugin>
</plugins>
</configuration>
测试一下
@ContextConfiguration(locations = {"classpath:spring-mybatis.xml"})
public class TUser2MapperTest extends AbstractTestNGSpringContextTests{
@Autowired
TUser2Mapper usermapper;
@Test
public void countByExampleTest() {
TUser2Example example =new TUser2Example();
Criteria cri = example.createCriteria();
cri.andAgeBetween(1, 100);
cri.andAgeLessThan(1000);
Criteria cri2= example.createCriteria();
cri2.andUserNameNotLike("zhangsan%");
example.or(cri2);
//设置当前第几页,每页取几条数据
PageHelper.startPage(2, 2);
//usermapper.countByExample(example);
List<TUser2> list = usermapper.selectByExample(example);
PageInfo<TUser2> pageInfo=new PageInfo<>(list);
//获得当前页有几条数据
System.out.println(pageInfo.getPageNum());
}
}