1.springboot+pagehelper
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
----------
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.6</version>
</dependency>
@Bean
public PageHelper pageHelper() {
PageHelper pageHelper = new PageHelper();
Properties p = new Properties();
p.setProperty("offsetAsPageNum", "true");
p.setProperty("rowBoundsWithCount", "true");
p.setProperty("reasonable", "true");
pageHelper.setProperties(p);
return pageHelper;
}
2.springboot+pagehelper-spring-boot-starter
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
----------
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
##采用了自动配置,不需要再注入PageHelper
#pagehelper分页插件配置
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
package com.xiaolyuh;
import com.github.pagehelper.Page;
import com.xiaolyuh.domain.model.Person;
import com.xiaolyuh.page.PageInfo;
import com.xiaolyuh.service.PersonService;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonMapperTests {
private Logger logger = LoggerFactory.getLogger(PersonMapperTests.class);
@Autowired
private PersonService personService;
@Before
public void testInsert() {
Person person = new Person();
person.setName("测试");
person.setAddress("address");
person.setAge(10);
personService.insert(person);
Assert.assertNotNull(person.getId());
logger.debug(JSON.toJSONString(person));
}
@Test
public void testFindAll() {
List<Person> persons = personService.findAll();
Assert.assertNotNull(persons);
logger.debug(JSON.toJSONString(persons));
}
@Test
public void testFindByPage() {
Page<Person> persons = personService.findByPage(1, 2);
PageInfo<Person> pageInfo = new PageInfo<>(persons);
Assert.assertNotNull(persons);
logger.debug(pageInfo.toString());
logger.debug(JSON.toJSONString(pageInfo));
}
@Test
public void testCacheByPage() {
long begin = System.currentTimeMillis();
List<Person> persons = personService.findAll();
long ing = System.currentTimeMillis();
personService.findAll();
long end = System.currentTimeMillis();
logger.debug("第一次请求时间:" + (ing - begin) + "ms");
logger.debug("第二次请求时间:" + (end - ing) + "ms");
Assert.assertNotNull(persons);
logger.debug(persons.toString());
logger.debug(JSON.toJSONString(persons));
}
}