提交 8227c7fe 编写于 作者: chenxiang@lab.ibiz5.com's avatar chenxiang@lab.ibiz5.com

NoSQL:MongoDB适配

上级 6fa0008b
package cn.ibizlab.util.filter;
import org.springframework.data.mongodb.core.query.Query;
public class MongoQueryContext extends SearchContextBase implements ISearchContext {
private Query searchCond = new Query();
public Query getSearchCond(){
return this.searchCond;
}
}
package cn.ibizlab.util.repository;
import cn.ibizlab.util.filter.MongoQueryContext;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface IBZMongoRepository<T, ID> extends MongoRepository<T, ID> {
<S extends T> Page<T> query(MongoQueryContext mongoQueryContext);
}
package cn.ibizlab.util.repository;
import cn.ibizlab.util.filter.MongoQueryContext;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
import org.springframework.data.mongodb.repository.support.SimpleMongoRepository;
import org.springframework.data.support.PageableExecutionUtils;
import java.util.List;
public class IBZMongoSimpleRepository<T, ID> extends SimpleMongoRepository<T, ID> implements IBZMongoRepository<T, ID> {
private final MongoOperations mongoOperations;
private final MongoEntityInformation<T, ID> entityInformation;
public IBZMongoSimpleRepository(MongoEntityInformation<T, ID> metadata, MongoOperations mongoOperations) {
super(metadata, mongoOperations);
this.entityInformation = metadata;
this.mongoOperations = mongoOperations;
}
@Override
public <S extends T> Page<T> query(MongoQueryContext mongoQueryContext) {
Query query = mongoQueryContext.getSearchCond();
query.with(mongoQueryContext.getPageable());
if(mongoQueryContext.getPageSort()!=null)
query.with(mongoQueryContext.getPageSort());
List<T> list = mongoOperations.find(query, entityInformation.getJavaType(), entityInformation.getCollectionName());
return PageableExecutionUtils.getPage(list, mongoQueryContext.getPageable(),
() -> mongoOperations.count(query, entityInformation.getJavaType(), entityInformation.getCollectionName()));
}
}
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册