今天在网上搜索了一些SpringBoot和PageHelper集成的一些例子,觉得有点难懂,还有些说要去启动类里面配置PageHelper插件,也有的说在application.properties里面设置一些属性,对于初学者这分页起来有点难,下面贡献一些我自己的代码,就只是简单的分页,没有设置一些额外的属性。
UserMapper.java
@Mapper
public interface UserMapper {
@Select("select id,name,age from user")
public List<User> selectUser();
}
2.PageService
public interface PageService {
public List<User> getPageList(int pageNo,int pageSize);
}
3.PageServiceImpl
@Service
public class PageServiceImpl implements PageService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> getPageList(int pageNo,int pageSize) {
PageHelper.startPage(pageNo, pageSize);
List<User> list=userMapper.selectUser();
return list;
}
}
4.Controller
@Controller
public class HelloController {
@Autowired
private PageService pageServiceImpl;
@RequestMapping("/")
@ResponseBody
public List<User> hh(int pageNo,int pageSize) {
return pageServiceImpl.getPageList(pageNo,pageSize);
}
}
5.启动类
@SpringBootApplication
public class Test {
public Test() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
SpringApplication.run(Test.class, args);
}
}
6.额外的一个依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
网上说要在启动类增加如下代码(我没试过,估计有什么玄机吧)
@Bean
public PageHelper pageHelper(){
System.out.println("开始配置数据分页插件");
PageHelper pageHelper = new PageHelper();
Properties properties = new Properties();
properties.setProperty("offsetAsPageNum","true");
properties.setProperty("rowBoundsWithCount","true");
properties.setProperty("reasonable","true");
properties.setProperty("dialect","mysql");
pageHelper.setProperties(properties);
return pageHelper;
}
或者在application.properties增加如下代码:
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql