如果您有 Spring Boot 并且想要做同样的事情,那么有一个更好的解决方法,它只能通过使用本机SQL_CALC_FOUND_ROWS
和FOUND_ROWS
(它不适用于 MySQL 8)适用于 MySql 5/Maria;
这种集成的主要思想是 Spring-Data 的灵活性和覆盖默认值的能力,JpaRepositoryFactoryBean
如下所示
步骤 #1 - 创建自定义CustomJpaRepositoryFactoryBean
:
/**
*
* @author ehakawati
*
* @param <R>
* @param <T>
* @param <I>
*/
public class CustomJpaRepositoryFactoryBean<R extends JpaRepository<T, I>, T, I extends Serializable>
extends JpaRepositoryFactoryBean<R, T, I> {
public CustomJpaRepositoryFactoryBean(Class<? extends R> repositoryInterface) {
super(repositoryInterface);
}
@Override
protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) {
RepositoryFactorySupport repositoryFactorySupport = super.createRepositoryFactory(entityManager);
repositoryFactorySupport.setRepositoryBaseClass(CustomJpaRepository.class);
return repositoryFactorySupport;
}
}
步骤 #2 - 创建自定义CustomJpaRepository
/**
*
* @author ehakawati
*
* @param <T>
* @param <ID>
*/
public class CustomJpaRepository<T, ID extends Serializable> extends SimpleJpaRepository<T, ID>
implements Repository<T, ID> {
private final EntityManager em;
/**
*
* @param entityInformation
* @param entityManager
*/
public CustomJpaRepository(JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) {
super(entityInformation, entityManager);
this.em = entityManager;
}
/**
*
* @param domainClass
* @param entityManager
*/
public CustomJpaRepository(Class<T> domainClass, EntityManager entityManager) {
super(domainClass, entityManager);
this.em = entityManager;
}
/**
* Reads the given {@link TypedQuery} into a {@link Page} applying the given
* {@link Pageable} and {@link Specification}.
*
* @param query must not be {@literal null}.
* @param domainClass must not be {@literal null}.
* @param spec can be {@literal null}.
* @param pageable can be {@literal null}.
* @return
*/
protected <S extends T> Page<S> readPage(TypedQuery<S> query, final Class<S> domainClass, Pageable pageable,
@Nullable Specification<S> spec) {
if (pageable.isUnpaged()) {
return super.readPage(query, domainClass, pageable, spec);
}
query.setFirstResult((int) pageable.getOffset());
query.setMaxResults(pageable.getPageSize());
return PageableExecutionUtils.getPage(query.getResultList(), pageable, () -> getNativeCount());
}
/**
*
*/
protected long getNativeCount() {
final Query query = em.createNativeQuery("SELECT FOUND_ROWS() as `count`");
return ((BigInteger) query.getSingleResult()).longValue();
}
}
步骤#3 - PageableQueriesInterceptor
:
/**
*
* @author ehakawati
*
* @param <T>
* @param <ID>
*/
public class PageableQueriesInterceptor extends EmptyInterceptor {
private static final Pattern PATTERN = Pattern.compile(".*?limit \\?(, \\?)?$");
private static final long serialVersionUID = 1L;
@Override
public String onPrepareStatement(String sql) {
if (PATTERN.matcher(sql).find()) {
sql = sql.replaceFirst("select", "select SQL_CALC_FOUND_ROWS ");
}
return sql;
}
}
步骤 #4 - 启用PageableQueriesInterceptor
:
sprint.jpa.properties.hibernate.ejb.interceptor= com.****.****.******.betterpaging.PageableQueriesInterceptor
享受