注解方式集成MyBatis-PageHelper分页


1.后台引入pagehelper依赖  springboot版本为1.5.3

<!-- springboot-aop包,AOP切面注解,Aspectd等相关注解 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-aop</artifactId>
		</dependency>
<!-- 集成pageHelper分页插件 -->
		<dependency>
			     <groupId>com.github.pagehelper</groupId>
   	              <artifactId>pagehelper-spring-boot-starter</artifactId>
   	              <version>1.2.5</version>
		</dependency>

2.application.properties 加入配置

# 分页配置
pagehelper.helper-dialect: mysql
pagehelper.reasonable: true
pagehelper.support-methods-arguments: true
pagehelper.params: count=countSql

2.创建自定义注解

package com.huajie.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

//整合mybatis分页插件
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD })
public @interface ExtPageHelper {
	int pageSize() default 20;
}

3.Aop处理

package com.huajie.aspect;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huajie.annotation.ExtPageHelper;

/**
 * 统一处理分页
 */
@Component
@Aspect
public class PageHelperAspect {

	@Pointcut("@annotation(com.huajie.annotation.ExtPageHelper)")
	public void ServiceImpl() {
	}

	@SuppressWarnings("unchecked")
	@Around("ServiceImpl()")
	public <T> PageInfo<T>  doAround(ProceedingJoinPoint joinPoint) throws Throwable {
		// 初始化参数
		int page = 0;
		int pageSize = 0;
		// 获得当前访问的class
		Class<?> className = joinPoint.getTarget().getClass();
		// 获得访问的方法名
		String methodName = joinPoint.getSignature().getName();
		// 得到方法的参数的类型
		Class<?>[] argClass = ((MethodSignature) joinPoint.getSignature()).getParameterTypes();
		// 得到方法的参数的类型
		String[] parameterNames = ((MethodSignature) joinPoint.getSignature()).getParameterNames();
		// 获取参数 page 和 pageSize
		Object[] args = joinPoint.getArgs();
		int i = 0;
		for (String pName : parameterNames) {
			if (pName.equals("page")) {
				page = (int) args[i];
			}
			if (pName.equals("pageSize")) {
				pageSize = (int) args[i];
			}
			i++;
		}
		// 设置分页
		PageHelper.startPage(page, pageSize);
		try {
			// 得到访问的方法对象
			Method method = className.getMethod(methodName, argClass);
			method.setAccessible(true);
			// 判断是否存在@ExtDataSource注解
			if (method.isAnnotationPresent(ExtPageHelper.class)) {
				ExtPageHelper annotation = method.getAnnotation(ExtPageHelper.class);
				// 取出注解中的数据源名
				pageSize = annotation.pageSize();
				// List<User>
				// returnType = method.getReturnType();
				// Type genericSuperclass = returnType.getGenericSuperclass();
				// genericSuperclass.getClass().cast(obj);
			}
		} catch (Exception e) {
			throw new RuntimeException("动态设置分页属性报错", e);
		}
		List<T> reuslt = new ArrayList<T>();
		try {
			reuslt = (List<T>) joinPoint.proceed();
		} catch (Exception e) {
			throw new RuntimeException("返回类型不为List", e);
		}
		PageInfo<T> pageList = new PageInfo<T>(reuslt);
		return pageList;
	}
}

4.编写测试类

controller

package com.huajie.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.huajie.service.TestService;

/**
 * @author xiewenfeng
 * 分页测试
 */
@RestController
public class TestController {
	@Autowired
	private TestService testService;

	@RequestMapping("/indexTest")
	public Object index(int page,int pageSize) {
		return testService.index(page,pageSize);
	}
}

service

package com.huajie.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.huajie.annotation.ExtPageHelper;
import com.huajie.dao.HomePageMapper;
import com.huajie.entity.Menu;

@Service
public class TestService {
	
	@Autowired
	private HomePageMapper homePageMapper;
	
	@ExtPageHelper
	public Object index(int page, int pageSize) {
//		PageHelper.startPage(page, pageSize);
		List<Menu> findListMenus = homePageMapper.findListMenus("");
//		PageInfo<Menu> list = new PageInfo<Menu>(findListMenus);
		return findListMenus;
	}

}

mapper

package com.huajie.dao;

import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import com.huajie.entity.Menu;
import com.huajie.entity.TbBackNotice;

/**
 * @author Administrator 
 */
@Mapper
public interface HomePageMapper {

	@Select(" select fd_id,fd_name,fd_url,fd_orderno from base_menus order by fd_orderno ")
	public List<Menu> findListMenus(String url);

}

entity  这里引入了 lombok插件 ,如果没有,自己实现get set方法即可

package com.huajie.entity;

import lombok.Data;

/**
 * @author Administrator
 * 首页菜单
 */
@Data
public class Menu {
	private String fd_id;
	private String fd_name;
	private String fd_url;
	private String fd_orderno;
	private String cflag;//当前菜单标示
}
 
 
测试效果如下:

猜你喜欢

转载自blog.csdn.net/xiewenfeng520/article/details/80783584