首页

关于定义mongodb数据库的MongoDAO的基础接口及实现类代码示例

标签:mongodb数据访问层,MongoDAO,mongo     发布时间:2018-12-10   

一、前言

基于mongodb的源码包定义其数据访问层MongoDAO接口及其实现BaseMongoDAO类,详情参下面代码示例部分。

二、代码示例

1. MongoDAO接口

import org.springframework.data.mongodb.core.DbCallback;@b@import org.springframework.data.mongodb.core.MongoTemplate; @b@@b@public interface MongoDAO{@b@@b@	 <R> R executeInSession( DbCallback<R>  action);@b@	 @b@	 MongoTemplate getMongoTemplate();@b@	@b@}

2.  BaseMongoDAO、EntityObjectHandler实现类

import java.beans.IntrospectionException;@b@import java.beans.Introspector;@b@import java.beans.PropertyDescriptor;@b@import java.lang.annotation.Annotation;@b@import java.lang.reflect.Field;@b@import java.lang.reflect.Method;@b@import java.lang.reflect.ParameterizedType;@b@import java.lang.reflect.Type;@b@@b@import org.apache.commons.logging.Log;@b@import org.apache.commons.logging.LogFactory;@b@import org.springframework.beans.FatalBeanException;@b@import org.springframework.beans.factory.InitializingBean;@b@import org.springframework.context.support.ApplicationObjectSupport;@b@import org.springframework.data.annotation.Id;@b@@b@public class EntityObjectHandler<T> extends ApplicationObjectSupport  implements InitializingBean{@b@	@b@	protected Log logger=LogFactory.getLog(this.getClass());@b@@b@	private Class<T> entityClass;@b@	@b@	@b@	private String idPropertyName;@b@	@b@	private PropertyDescriptor idPropertyDescriptor;@b@	@b@@b@	protected Class<T> getEntityClass() {@b@		return entityClass;@b@	}@b@@b@	@b@@b@	@SuppressWarnings("unchecked")@b@	private Class<T> resolveEntityClassByGeneric(){@b@		Type type=this.getClass().getGenericSuperclass();@b@		if(type!=null && type instanceof ParameterizedType){@b@			 ParameterizedType ptype=(ParameterizedType)type;@b@			 Type[] types=ptype.getActualTypeArguments();@b@			 if(types!=null && types.length>0){@b@				 Type genericClass = types[0];@b@				 if(genericClass!=null && genericClass instanceof Class){@b@					 Class<T> genericClazz= (Class<T>)genericClass;@b@					 String genericClazzName=genericClazz.getName();@b@					 if(!(genericClazz.isInterface() || genericClazzName.startsWith("java."))){@b@						 return genericClazz;@b@					 }@b@				 }@b@			 }@b@		}@b@		return null;@b@	}@b@@b@	@Override@b@	public void afterPropertiesSet() throws Exception {@b@		if(entityClass==null){@b@			entityClass=resolveEntityClassByGeneric();@b@		}@b@		if(entityClass!=null){@b@			PropertyDescriptor[] propertyDescriptors=null;@b@			try {@b@				propertyDescriptors = Introspector.getBeanInfo(entityClass).getPropertyDescriptors();@b@			} catch (IntrospectionException e) {@b@				throw new FatalBeanException(e.getMessage(),e);@b@			}@b@			this.idPropertyDescriptor=resolveIdProperty(propertyDescriptors,entityClass);@b@			if(idPropertyDescriptor!=null){@b@				idPropertyName=idPropertyDescriptor.getName();@b@			}@b@		}@b@		@b@	}@b@	@b@@b@@b@	private PropertyDescriptor resolveIdProperty(PropertyDescriptor[] properties,Class<?> entityClass){@b@		if(properties==null){@b@			return null;@b@		}@b@		return findPropertyDescriptor(properties,entityClass,Id.class.getSimpleName());@b@	}@b@	@b@@b@	@b@	private PropertyDescriptor findPropertyDescriptor(PropertyDescriptor[] properties,Class<?> entityClass,String annotationName){@b@		if(properties==null){@b@			return null;@b@		}@b@		PropertyDescriptor find=null;@b@		for(PropertyDescriptor property:properties){@b@			try {@b@				Field field=entityClass.getDeclaredField(property.getName());@b@				if(field!=null && isIncludedAnnotation(field.getDeclaredAnnotations(),annotationName)){@b@					find=property;@b@					break;@b@				}@b@			} catch (Exception e) {@b@			} @b@			Method readMethod=property.getReadMethod();@b@			if(readMethod!=null && isIncludedAnnotation(readMethod.getDeclaredAnnotations(),annotationName)){@b@				find=property;@b@				break;@b@			}@b@			Method writeMethod=property.getWriteMethod();@b@			if(writeMethod!=null && isIncludedAnnotation(writeMethod.getDeclaredAnnotations(),annotationName)){@b@				find=property;@b@				break;@b@			}@b@		}@b@		if(find!=null ){@b@			if(find.getWriteMethod()==null){@b@				throw new FatalBeanException("Id property<"+find.getName()+"> no wirte method.");@b@			}@b@			if(find.getReadMethod()==null){@b@				throw new FatalBeanException("Id property<"+find.getName()+"> no read method.");@b@			}@b@			return find;@b@		}@b@		return null;@b@		@b@	}@b@	@b@	private boolean isIncludedAnnotation(Annotation annotations[],String annotationName){@b@		if(annotations==null || annotations.length==0){@b@			return false;@b@		}@b@		boolean flag=false;@b@		for(Annotation annotation:annotations){@b@			String target=annotation.annotationType().getSimpleName();@b@			if(target.equals(annotationName)){@b@				flag=true;@b@				break;@b@			}@b@		}@b@		return flag;@b@	}@b@	@b@	@b@	protected String getIdPropertyName(boolean required) {@b@		if(idPropertyName==null && required){@b@			throw new java.lang.IllegalArgumentException("not spec id property by annotation:"+Id.class.getName());@b@		}@b@		return idPropertyName;@b@	}@b@	@b@	protected String getIdPropertyName() {@b@		return getIdPropertyName(true);@b@	}@b@@b@@b@	protected Object getIdValue(T entity){@b@		if(this.idPropertyDescriptor==null){@b@			throw new java.lang.IllegalArgumentException("IdPropertyDescriptor be null.");@b@		}@b@		Method readMethod=idPropertyDescriptor.getReadMethod();@b@		if(readMethod==null){@b@			throw new java.lang.IllegalArgumentException("IdProperty<"+this.idPropertyName+"> no read method.");@b@		}@b@		Object idValue=null;@b@		try {@b@			idValue=readMethod.invoke(entity);@b@		} catch (Exception e) {@b@		} @b@		if(idValue==null){@b@			throw new java.lang.IllegalArgumentException("IdProperty<"+this.idPropertyName+"> value be null by entity="+entity);@b@		}@b@		return idValue;@b@	}@b@@b@	@b@	@b@}
import java.util.ArrayList;@b@import java.util.Arrays;@b@import java.util.Collection;@b@import java.util.List;@b@import java.util.Map;@b@@b@import org.springframework.beans.FatalBeanException;@b@import org.springframework.beans.factory.BeanFactoryUtils;@b@import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;@b@import org.springframework.context.ConfigurableApplicationContext;@b@import org.springframework.data.domain.Sort;@b@import org.springframework.data.domain.Sort.Direction;@b@import org.springframework.data.mongodb.core.DbCallback;@b@import org.springframework.data.mongodb.core.DocumentCallbackHandler;@b@import org.springframework.data.mongodb.core.MongoTemplate;@b@import org.springframework.data.mongodb.core.aggregation.Aggregation;@b@import org.springframework.data.mongodb.core.aggregation.AggregationOperation;@b@import org.springframework.data.mongodb.core.aggregation.AggregationResults;@b@import org.springframework.data.mongodb.core.aggregation.GroupOperation;@b@import org.springframework.data.mongodb.core.aggregation.TypedAggregation;@b@import org.springframework.data.mongodb.core.query.Criteria;@b@import org.springframework.data.mongodb.core.query.Query;@b@import org.springframework.data.mongodb.core.query.Update;@b@@b@import com.mongodb.DBCollection;@b@import com.mongodb.DBObject;@b@import com.mongodb.MongoException;@b@import com.mongodb.WriteResult;@b@@b@public  class BaseMongoDAO<T> extends EntityObjectHandler<T> implements MongoDAO{@b@@b@	@b@	private MongoTemplate _mongoTemplate;@b@	@b@	private String collectionName;@b@	@b@	@b@	protected void _add(T... entities) {@b@		if(entities==null || entities.length==0){@b@			return ;@b@		}@b@		this.getMongoTemplate().insert(Arrays.asList(entities), getCollectionName());@b@	}@b@@b@	@b@	protected void _add(Collection<T> entities) {@b@		if(entities==null || entities.size()==0){@b@			return ;@b@		}@b@		this.getMongoTemplate().insert(entities, getCollectionName());@b@	}@b@@b@	protected void _add(T entity) {@b@		this.getMongoTemplate().insert(entity,getCollectionName());@b@	}@b@	@b@	protected void _save(T entity) {@b@		this.getMongoTemplate().save(entity,getCollectionName());@b@	}@b@	@b@	@b@	@b@	protected int _updateMulti(Criteria criteria, T entity) {@b@		Update update=null;@b@		if(entity!=null){@b@			DBObject obj =(DBObject)this.getMongoTemplate().getConverter().convertToMongoType(entity);@b@			update=Update.fromDBObject(obj);@b@		}@b@		return _updateMulti(criteria,update);@b@	}@b@@b@	@b@@b@	protected int _updateMulti(Criteria criteria,Update update) {@b@		if(logger.isDebugEnabled()){@b@			logger.debug("UpdateMulti,criteria="+(criteria==null?null:criteria.getCriteriaObject())@b@					+"\nUpdate="+(update==null?null:update.toString()));@b@		}@b@		//@b@		WriteResult result=this.getMongoTemplate().updateMulti(criteria==null?null:Query.query(criteria)@b@				, update,this.getEntityClass(), this.getCollectionName());@b@		int count=result.getN();@b@		if(logger.isDebugEnabled()){@b@			logger.debug("UpdateMulti,result="+result);@b@		}@b@		return count;@b@	}@b@	@b@	protected int _upsert(Criteria criteria, T entity) {@b@		Update update=null;@b@		if(entity!=null){@b@			DBObject obj =(DBObject)this.getMongoTemplate().getConverter().convertToMongoType(entity);@b@			update=Update.fromDBObject(obj);@b@		}@b@		return _upsert(criteria,update);@b@	}@b@	@b@	protected int _upsert(Criteria criteria,Update update) {@b@		if(logger.isDebugEnabled()){@b@			logger.debug("Upsert,criteria="+(criteria==null?null:criteria.getCriteriaObject())@b@					+"\nUpdate="+(update==null?null:update.toString()));@b@		}@b@		//@b@		WriteResult result=this.getMongoTemplate().upsert(criteria==null?null:Query.query(criteria)@b@				, update,this.getEntityClass(), this.getCollectionName());@b@		int count=result.getN();@b@		if(logger.isDebugEnabled()){@b@			logger.debug("Upsert,result="+result);@b@		}@b@		return count;@b@	}@b@@b@	public <R> R executeInSession( DbCallback<R>  action){@b@		return this.getMongoTemplate().executeInSession(action);@b@	}@b@@b@	protected boolean _exists(Criteria criteria){@b@		if(logger.isDebugEnabled()){@b@			logger.debug("Exists,criteria="+(criteria==null?null:criteria.getCriteriaObject()));@b@		}@b@		Query query=null;@b@		if(criteria!=null){@b@			query=Query.query(criteria);@b@		}@b@		return this.getMongoTemplate().exists(query, this.getEntityClass(),this.getCollectionName());@b@	}@b@	@b@	@b@	protected List<T> _list(Criteria criteria) {@b@		return _list(criteria,-1,-1,null);@b@	}@b@	@b@	@b@	protected List<T> _list(Criteria criteria, int skip, int limitSize) {@b@		return _list(criteria,skip,limitSize,null);@b@	}@b@@b@@b@	@b@	protected List<T> _listAndDesc(Criteria criteria, int skip, int limitSize,@b@			String... orderBy) {@b@		return _list(criteria,skip,limitSize,(orderBy==null?null:new Sort(Direction.DESC, orderBy)));@b@	}@b@@b@@b@	@b@	protected List<T> _listAndAsc(Criteria criteria, int skip, int limitSize,@b@			String... orderBy) {@b@		return _list(criteria,skip,limitSize,(orderBy==null?null:new Sort(Direction.ASC, orderBy)));@b@	}@b@@b@@b@	@b@	protected List<T> _listAndDesc(Criteria criteria, String... orderBy) {@b@		return _list(criteria,-1,-1,(orderBy==null?null:new Sort(Direction.DESC, orderBy)));@b@	}@b@@b@@b@@b@	protected List<T> _listAndAsc(Criteria criteria, String... orderBy) {@b@		return _list(criteria,-1,-1,(orderBy==null?null:new Sort(Direction.ASC, orderBy)));@b@	}@b@@b@	@b@@b@	@b@@b@	protected List<T> _list(Criteria criteria, int skip, int limitSize,@b@			Sort sort) {@b@		if(logger.isDebugEnabled()){@b@			logger.debug("List,skip="+skip+",limitSize="+limitSize@b@					+",Criteria="+(criteria==null?null:criteria.getCriteriaObject())+",Sort="+sort);@b@		}@b@		Query query=new Query();@b@		if(criteria!=null){@b@			query.addCriteria(criteria);@b@		}@b@		if(sort!=null){@b@			query.with(sort);@b@		}@b@		if(skip>0)query.skip(skip);@b@		if(limitSize>0)query.limit(limitSize);@b@		return this.getMongoTemplate().find(query, this.getEntityClass(), getCollectionName());@b@	}@b@	@b@	protected List<T> _listQuery(Query query) {@b@		return this.getMongoTemplate().find(query, this.getEntityClass(), getCollectionName());@b@	}@b@@b@	protected void _listQuery(Query query,DocumentCallbackHandler dch) {@b@		 this.getMongoTemplate().executeQuery(query, this.getCollectionName(), dch);@b@	}@b@	@b@@b@	protected boolean _updateById(Object id,T entity) {@b@		String idPropertyName=this.getIdPropertyName();@b@		if(id==null){@b@			throw new MongoException("id is null.");@b@		}@b@		return _update(Criteria.where(idPropertyName).is(id),entity);@b@	}@b@	@b@	protected boolean _updateById(T entity) {@b@		String idPropertyName=this.getIdPropertyName();@b@		Object idValue=getIdValue(entity);@b@		return _update(Criteria.where(idPropertyName).is(idValue),entity);@b@	}@b@@b@	protected boolean _updateByProperty( String whereProperty,@b@			Object whereValue,T dto) {@b@		if(whereProperty==null || whereValue==null){@b@			throw new MongoException("Parameter is null.");@b@		}@b@		return _update(Criteria.where(whereProperty).is(whereValue),dto);@b@	}@b@	@b@	@b@	protected boolean _update(Criteria criteria,Update update) {@b@		return _updateMulti(criteria,update)>0;@b@	}@b@@b@	protected boolean _updateFirst(Criteria criteria,Update update) {@b@		if(logger.isDebugEnabled()){@b@			logger.debug("UpdateFirst,Criteria="+(criteria==null?null:criteria.getCriteriaObject())@b@					+"\nUpdate="+(update==null?null:update.toString()));@b@		}@b@		Query query=null;@b@		if(criteria!=null){@b@			query=Query.query(criteria);@b@		}@b@		WriteResult result=this.getMongoTemplate().updateFirst(query, update, this.getEntityClass(),this.getCollectionName());@b@		if(logger.isDebugEnabled()){@b@			logger.debug("UpdateFirst,result="+result);@b@		}@b@		return result.getN()==1;@b@	}@b@	@b@	@b@	protected T _getAndUpdate(Criteria criteria , T entity) { @b@		if(entity==null){@b@			throw new MongoException("entity is null");@b@		}@b@		DBObject obj =(DBObject)this.getMongoTemplate().getConverter().convertToMongoType(entity);@b@		return _getAndUpdate(criteria,Update.fromDBObject(obj));@b@	}@b@	@b@	protected T _getAndRemove(Criteria criteria) {@b@		if(logger.isDebugEnabled()){@b@			logger.debug("GetAndRemove,Criteria="+(criteria==null?null:criteria.getCriteriaObject()));@b@		}@b@		Query query=null;@b@		if(criteria!=null){@b@			query=Query.query(criteria);@b@		}@b@		return this.getMongoTemplate().findAndRemove(query, this.getEntityClass(),this.getCollectionName());@b@	}@b@	@b@	protected List<T> _listAndRemove(Criteria criteria) {@b@		if(logger.isDebugEnabled()){@b@			logger.debug("ListAndRemove,Criteria="+(criteria==null?null:criteria.getCriteriaObject()));@b@		}@b@		Query query=null;@b@		if(criteria!=null){@b@			query=Query.query(criteria);@b@		}@b@		return this.getMongoTemplate().findAllAndRemove(query, this.getEntityClass(),this.getCollectionName());@b@	}@b@	@b@	@b@	protected T _getAndUpdate(Criteria criteria, Update update) {@b@		if(logger.isDebugEnabled()){@b@			logger.debug("GetAndUpdate,Criteria="+(criteria==null?null:criteria.getCriteriaObject())@b@					+"\nUpdate="+(update==null?null:update.toString()));@b@		}@b@		Query query=null;@b@		if(criteria!=null){@b@			query=Query.query(criteria);@b@		}@b@		return this.getMongoTemplate().findAndModify(query, update, this.getEntityClass(),this.getCollectionName());@b@	}@b@@b@@b@@b@@b@@b@	protected boolean _update(Criteria criteria,T dto) {@b@		DBObject obj =(DBObject)this.getMongoTemplate().getConverter().convertToMongoType(dto);@b@		return _updateFirst(criteria,Update.fromDBObject(obj));@b@	}@b@	@b@	@b@@b@	@b@	protected T _getByProperty(String whereProperty,Object whereValue) {@b@		return _get(Criteria.where(whereProperty).is(whereValue));@b@	}@b@@b@@b@@b@	protected T _getById(Object id) {@b@		String idPropertyName=this.getIdPropertyName();@b@		return _get(Criteria.where(idPropertyName).is(id));@b@	}@b@@b@@b@@b@@b@	protected T _get(Criteria criteria) {@b@		if(logger.isDebugEnabled()){@b@			logger.debug("Get,Criteria="+(criteria==null?null:criteria.getCriteriaObject()));@b@		}@b@		Query query=null;@b@		if(criteria!=null){@b@			query=Query.query(criteria);@b@		}@b@		return this.getMongoTemplate().findOne(query, this.getEntityClass(),this.getCollectionName());@b@	}@b@	@b@	protected List<T> _query(Query query) {@b@		if(logger.isDebugEnabled()){@b@			logger.debug("Query="+query);@b@		}@b@		List<T> result= this.getMongoTemplate().find(query, this.getEntityClass(), getCollectionName());@b@		return result;@b@	}@b@	@b@	@b@	@b@	protected MongoPagination<T> _paginatedQuery(Query query,MongoPagination<T> page) {@b@		int skip=0; @b@		int pageNo=page.getPageNo();@b@		int limitSize=page.getPageLimitSize();@b@		if(pageNo >= 1 && limitSize > 0){ @b@			skip=(pageNo - 1) * limitSize;@b@		}@b@		Sort sort=null;@b@		String[] orderBy=page.getOrderBy();@b@		if(orderBy!=null && orderBy.length>0){@b@			sort=new Sort(page.isDesc()?Direction.DESC:Direction.ASC, orderBy);@b@		}@b@		if(query==null){@b@			query=new Query();@b@		}@b@		if(sort!=null){@b@			query.with(sort);@b@		}@b@		if(logger.isDebugEnabled()){@b@			logger.debug("Pagination,Query="+query);@b@		}@b@		long totalSize=page.getTotalSize();@b@		if(totalSize<0){@b@			totalSize=this.getMongoTemplate().count(query,this.getCollectionName());@b@			page.setTotalSize(totalSize);@b@		}@b@		if(skip>0)query.skip(skip);@b@		if(limitSize>0)query.limit(limitSize);@b@		List<T> result= this.getMongoTemplate().find(query, this.getEntityClass(), getCollectionName());@b@		page.setPojos(result);@b@		return page;@b@	}@b@@b@	protected MongoPagination<T> _paginated(Criteria criteria,@b@			MongoPagination<T> page) { @b@		if(criteria==null)criteria=new Criteria();@b@		return _paginatedQuery(Query.query(criteria),page);@b@	}@b@	@b@	protected MongoPagination<T> _paginated(MongoPagination<T> page) { @b@		return _paginatedQuery(Query.query(new Criteria()),page);@b@	}@b@	@b@	@b@@b@	protected long _count(Criteria criteria) {@b@		Query query=null;@b@		if(criteria!=null){@b@			query=Query.query(criteria);@b@		}else{@b@			query=new Query();@b@		}@b@		return _countQuery(query);@b@	}@b@	@b@	protected long _countQuery(Query query) {@b@		if(logger.isDebugEnabled()){@b@			logger.debug("Size,Query="+query.toString());@b@		}@b@		return this.getMongoTemplate().count(query, this.getEntityClass());@b@	}@b@@b@	@b@	protected boolean _removeById(Object id) {@b@		 return _remove(Criteria.where(this.getIdPropertyName()).is(id))==1;@b@	}@b@@b@	protected int _removeByProperty(String whereProperty,Object whereValue) {@b@		return _remove(Criteria.where(whereProperty).is(whereValue));@b@	}@b@@b@	@b@@b@	protected int _remove(Criteria criteria) {@b@		if(logger.isDebugEnabled()){@b@			logger.debug("Remove,Criteria="+(criteria==null?null:criteria.getCriteriaObject()));@b@		}@b@		Query query=null;@b@		if(criteria!=null){@b@			query=Query.query(criteria);@b@		}else{@b@			query=new Query();@b@		}@b@		WriteResult result=this.getMongoTemplate().remove(query, this.getEntityClass(), getCollectionName());@b@		if(logger.isDebugEnabled()){@b@			logger.debug("Remove,result="+result);@b@		}@b@		return result.getN();@b@	}@b@@b@	@Override@b@	public final void afterPropertiesSet() throws Exception {@b@		super.afterPropertiesSet();@b@		this._mongoTemplate=initMongoTemplate();@b@		init();@b@	}@b@	@b@	protected void init()throws Exception{@b@		@b@	}@b@@b@	protected static final  String DEF_MONGO_TEMPLATE_BEAN_NAME="def_mongodb_template";@b@	@b@	private final  Object lock=new Object();@b@	@b@	protected final MongoTemplate initMongoTemplate(){@b@		MongoTemplate _mongoTemplate=null;@b@		ConfigurableApplicationContext context=(ConfigurableApplicationContext)this.getApplicationContext();@b@		ConfigurableListableBeanFactory beanFactory=context.getBeanFactory();@b@		if(beanFactory.containsBean(DEF_MONGO_TEMPLATE_BEAN_NAME)){@b@			_mongoTemplate=beanFactory.getBean(DEF_MONGO_TEMPLATE_BEAN_NAME,MongoTemplate.class);@b@		}@b@		if(_mongoTemplate==null){@b@			Map<String, MongoTemplate> matchingBeans=beanFactory.getBeansOfType(MongoTemplate.class);@b@			if(matchingBeans!=null && matchingBeans.size()==1){@b@				_mongoTemplate=(MongoTemplate)matchingBeans.values().toArray()[0];@b@			}@b@		}@b@		ConfigurableListableBeanFactory parentBeanFactory=(ConfigurableListableBeanFactory)beanFactory.getParentBeanFactory();@b@		if(_mongoTemplate==null && parentBeanFactory!=null){@b@			Map<String, MongoTemplate> matchingBeans=BeanFactoryUtils.beansOfTypeIncludingAncestors(@b@					parentBeanFactory,MongoTemplate.class,true,false);@b@			if(matchingBeans!=null && matchingBeans.size()==1){@b@				_mongoTemplate=(MongoTemplate)matchingBeans.values().toArray()[0];@b@			}@b@		}@b@		if(_mongoTemplate==null){@b@			throw new  FatalBeanException("mongoTemplate can't resolved.");@b@		}@b@		if(collectionName==null){@b@			Class<T> entityClass=this.getEntityClass();@b@			if(entityClass==null){@b@				throw new java.lang.IllegalArgumentException("entityClass can't be resolved.");@b@			}@b@			collectionName=_mongoTemplate.getCollectionName(entityClass);@b@		}@b@		if(logger.isInfoEnabled()){@b@			logger.info("MongodbCollection,name="+collectionName+",dtoClazz="+this.getEntityClass().getName()@b@					+(",idPropertyName="+this.getIdPropertyName(false))@b@			);@b@		}@b@		DBCollection collection=_mongoTemplate.getCollection(collectionName);@b@		if(logger.isInfoEnabled()){@b@			logger.info("MongodbCollection inited,name="+collectionName+",size="+collection.count()@b@					+",ensuredIndexes="+_mongoTemplate.indexOps(collectionName).getIndexInfo());@b@		}@b@		return _mongoTemplate;@b@	}@b@@b@	public MongoTemplate getMongoTemplate() {@b@		if(this._mongoTemplate==null){@b@			synchronized(lock){@b@				if(this._mongoTemplate==null){@b@					this._mongoTemplate=initMongoTemplate();@b@				}@b@			}@b@		}@b@		return _mongoTemplate;@b@	}@b@	@b@	protected GroupOperation groupOperation(String... fields){@b@		return Aggregation.group(fields);@b@	}@b@	@b@	@b@	@b@	@b@	@b@	protected <R> List<R> _groupBy(GroupOperation groupOperation,Criteria criteria,Class<R> outputType){@b@		List<AggregationOperation> operations= new ArrayList<AggregationOperation>(7);@b@		operations.add(groupOperation);@b@		if(criteria!=null){@b@			operations.add(Aggregation.match(criteria));@b@		}@b@		TypedAggregation<T> aggregationType =Aggregation.newAggregation(this.getEntityClass(),operations);@b@		AggregationResults<R> sumResult = this.getMongoTemplate().aggregate(aggregationType,this.getCollectionName(), outputType);@b@		return sumResult.getMappedResults();@b@	}@b@@b@@b@	@b@@b@	public void setMongoTemplate(MongoTemplate mongoTemplate) {@b@		this._mongoTemplate = mongoTemplate;@b@	}@b@@b@	@b@	@b@	@b@	protected Criteria where(String key) {@b@		return new Criteria(key);@b@	}@b@@b@@b@	public String getCollectionName() {@b@		this.getMongoTemplate(); @b@		if(collectionName==null){@b@			throw new NullPointerException("collectionName is null.");@b@		}@b@		return collectionName;@b@	}@b@@b@@b@	public void setCollectionName(String collectionName) {@b@		this.collectionName = collectionName;@b@	} @b@}