webflux r2dbc mysql分页工具示例

webflux r2dbc mysql分页工具示例

1.分页工具类R2dbcPageAgent

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;
import org.springframework.data.relational.core.query.Query;

import reactor.core.publisher.Mono;

public class R2dbcPageAgent<T> {

	private Class<T> entityClass;

	private R2dbcEntityTemplate r2dbcEntityTemplate;

	public R2dbcPageAgent(R2dbcEntityTemplate r2dbcEntityTemplate, Class<T> entityClass) {
		this.r2dbcEntityTemplate = r2dbcEntityTemplate;
		this.entityClass = entityClass;
	}

	public Mono<Page<T>> findPageByQuery(Query query, int page,int size) {
		Pageable pageable=PageRequest.of(page-1, size);
		return r2dbcEntityTemplate.select(query.with(pageable), entityClass).collectList()
				.flatMap(r -> {
					return r2dbcEntityTemplate.count(query, entityClass).map(count -> {
						return new PageImpl<>(r, pageable, count);
					});
				});
	}

	public Mono<Page<T>> findPage(int page,int size) {
		return findPageByQuery(null, page,size);
	}
}

2.Repository层


import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;
import org.springframework.data.r2dbc.repository.R2dbcRepository;
import org.springframework.data.relational.core.query.Criteria;
import org.springframework.data.relational.core.query.Query;
import org.springframework.data.relational.core.query.Update;
import org.springframework.stereotype.Service;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

public interface CategoryRepository extends R2dbcRepository<TbCategory, Long>, CategoryInnerRepository {

	public static final String CATEGORY_ALL_SQL = "select id as category_id,category_name from XXX where is_del=10 order by category_sort desc,create_time desc";
	//简单的查询
	@org.springframework.data.r2dbc.repository.Query(CATEGORY_ALL_SQL)
	public Flux<CategoryAllModel> listAllMapper();
}

interface CategoryInnerRepository {

	public Mono<Page<TbCategory>> pageList(List<Criteria> criteriaList,int current,int size);
}

@Service
class CategoryInnerRepositoryImpl implements CategoryInnerRepository {
	@Autowired
	private R2dbcEntityTemplate r2dbcEntityTemplate;
	
	//自定义分页调用
	public Mono<Page<TbCategory>> pageList(List<Criteria> criteriaList,int current,int size) {
		Query query=Query.query(Criteria.from(criteriaList)).columns("id,category_name,create_person,category_sort,create_time")
				.sort(Sort.by(Order.desc("category_sort")).and(Sort.by(Order.desc("id"))));
		R2dbcPageAgent<TbCategory> page = new R2dbcPageAgent<>(r2dbcEntityTemplate, TbCategory.class);
		return page.findPageByQuery(query, current, size);
	}
}

3.service层

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.relational.core.query.Criteria;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@Service
public class CategoryService {
	@Autowired
	private CategoryRepository categoryRepository;

	public Mono<R2dbcPageResult<CategoryModel>> pageListService(CategoryQueryForm form) {
		List<Criteria> criteriaList = new ArrayList<Criteria>();
		criteriaList.add(Criteria.where("is_del").is(10));
		if (StringUtils.isNotBlank(form.getCategoryName())) {
			criteriaList.add(Criteria.where("category_name").like(form.getCategoryName() + "%"));
		}
		return categoryRepository.pageList(criteriaList,form.getCurrent(),form.getSize()).flatMap(t->{
			return Mono.just(new R2dbcPageResult<CategoryModel>().result(t,CategoryModel.class));
		});
	}
  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值