Springboot下PageHelper的封装
2019-04-10 本文已影响0人
七十二年蝉
先说思路,使用pagehelper分页十分方便,本文只做简单的封装,通过反射机制获取service的查询方法并执行,提升分页查询调用方便程度。
- 在springboot项目中引入pagehelper的pom依赖:
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
</dependency>
- 编写SpringUtil类,用于从Spring中获取Bean,用于后续的反射获取的方法的执行:
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class SpringUtils implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if(SpringUtils.applicationContext == null) {
SpringUtils.applicationContext = applicationContext;
}
}
private static ApplicationContext getApplicationContext() {
return applicationContext;
}
public static <T> T getBean(Class<T> clazz){
return getApplicationContext().getBean(clazz);
}
}
- 在springboot的入口处,初始化ApplicationContext,为SpringUtil获取ApplicationContext:
public static void main(String[] args) {
ApplicationContext applicationContext = SpringApplication.run(FoundationApplication.class, args);
new SpringUtils().setApplicationContext(applicationContext);
}
- 封装分页类,核心思想为:先获取要执行查询的service,查询方法名,参数类型列表,再通过反射执行查询,PageHelper会自动将查询结果分页。AbstractService为所有使用分页查询的Service的父类
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import org.cicada.chtp.foundation.common.utils.SpringUtils;
import org.cicada.chtp.foundation.service.AbstractService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
public class PageSelector<T> {
private AbstractService service;
private String selectMethodName;
private Class<?>[] paramTypes;
private static final Logger logger = LoggerFactory.getLogger("记录查询分页器(page helper)");
private PageSelector(AbstractService service, String selectMethodName, Class<?>[] paramTypes){
this.service = service;
this.selectMethodName = selectMethodName;
this.paramTypes = paramTypes;
}
public static<T> PageSelector<T> build(AbstractService service, String selectMethodName, Class<?>...paramTypes){
return new PageSelector<>(service, selectMethodName, paramTypes);
}
public PageInfo<T> selectByPage(int pageNum, int pageSize, Object...args) throws Throwable {
Method selectMethod = getSelectMethod();
PageInfo<T> pageInfo = null;
AbstractService instance;
instance = SpringUtils.getBean(service.getClass());
try {
PageMethod.startPage(pageNum, pageSize);
ArrayList<T> list = (ArrayList<T>)selectMethod.invoke(instance, args);
pageInfo = new PageInfo<>(list);
}
catch (IllegalAccessException ie){
logger.error(ie.getLocalizedMessage());
}
catch (InvocationTargetException e) {
Throwable t = e.getTargetException();
String selectMethodName = selectMethod.getName();
String errorMsg = "分页查询时发生错误,方法名: " + selectMethodName + "\n";
logger.error(errorMsg + t.getLocalizedMessage());
throw t;
}
return pageInfo;
}
private Method getSelectMethod(){
Method method = null;
try {
method = service.getClass().getDeclaredMethod(selectMethodName, paramTypes);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
return method;
}
}
- controller里使用分页查询,如Service的查询需要参数,在build中传入参数类型的.class,在selectByPage中传入实际参数:
PageSelector<User> pageSelector = PageSelector.build(userService, "getUserList");
try {
response.setData(pageSelector.selectByPage(pageNum, pageSize));
} catch (Throwable throwable) {
response.setServiceCode(Code.FAIL);
response.setMsg(throwable.getLocalizedMessage());
}