package com.hh.us.mongo.morphia.dao;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.annotation.PostConstruct;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.bson.types.ObjectId;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.jmkgreen.morphia.Datastore;
import com.github.jmkgreen.morphia.Key;
import com.github.jmkgreen.morphia.query.CriteriaContainer;
import com.github.jmkgreen.morphia.query.FieldEnd;
import com.github.jmkgreen.morphia.query.Query;
import com.github.jmkgreen.morphia.query.UpdateOperations;
import com.hh.us.mongo.morphia.entity.MorphiaEntity;
import com.hh.us.mongo.morphia.entity.Paging;
import com.hh.us.mongo.morphia.entity.PagingResult;
import com.hh.us.mongo.morphia.util.MongoPaginationUtil;
public class UsMongoBaseDaoImpl<E extends MorphiaEntity<I>, I> extends
MorphiaBasicDao<E, I> implements IUsMongoBaseDao<E, I> {
private static final Logger LOGGER = LoggerFactory.getLogger(UsMongoBaseDaoImpl.class);
private Datastore datastore;
public Datastore getDatastore() {
return datastore;
}
public void setDatastore(Datastore datastore) {
this.datastore = datastore;
}
@PostConstruct
public void replaceDatastore() {
super.setDatastore(datastore);
}
/** ----------查询--------- */
@Override
public long countByProperty(Map<String, Object> properties) {
Query<E> query = super.createQuery();
query = buildQuery(properties, null, null);
return query.countAll();
}
@Override
public List<E> findByProperty(Map<String, Object> conditions, int page,
int pageSize) {
if (page < 1) {
page = 1;
}
if (pageSize < 1) {
pageSize = 10;
}
int offset = (page - 1) * pageSize;
Paging paging = new Paging(offset, pageSize);
return buildQuery(conditions, paging, null).asList();
}
@Override
public List<E> findByProperty(Map<String, Object> properties) {
return buildQuery(properties, null, null).asList();
}
@Override
public List<E> findByProperty(Paging paging) {
return buildQuery(null, paging, null).asList();
}
@Override
public List<E> findByProperty(Map<String, Object> properties, String orderBy) {
return buildQuery(properties, null, orderBy).asList();
}
@Override
public List<E> findByProperty(Map<String, Object> properties,
Paging paging, String orderBy) {
return buildQuery(properties, paging, orderBy).asList();
}
@Override
public List<E> findByProperty(Map<String, Object> properties, Paging paging) {
return buildQuery(properties, paging, null).asList();
}
@Override
public E findOneByProperty(Map<String, Object> properties) {
return buildQuery(properties, null, null).get();
}
@Override
public E findOneByProperty(String property, Object value) {
return buildQuery(property, value, null, null).get();
}
@Override
public List<E> findByProperty(String property, Object value) {
Query<E> query = buildQuery(property, value, null, null);
return query.asList();
}
@Override
public List<E> containByProperty(String property, Object value) {
Query<E> query = buildContainQuery(property, value, null, null);
return query.asList();
}
@Override
public List<E> findByProperty(String property, Object value,
String... retriveFields) {
Query<E> query = buildQuery(property, value, null, null, retriveFields);
return query.asList();
}
@Override
public List<E> findByProperty(String property, Object value, String orderBy) {
return buildQuery(property, value, null, orderBy).asList();
}
@Override
public List<E> findByProperty(String property, Object value, Paging paging) {
return buildQuery(property, value, paging, null).asList();
}
@Override
public List<E> findByProperty(String property, Object value, Paging paging,
String orderBy) {
return buildQuery(property, value, paging, orderBy).asList();
}
protected Query<E> buildQuery(String property, Object value, Paging paging,
String orderBy, String... retriveFields) {
Query<E> query = super.createQuery();
if (value != null) {
query.filter(property, value);
}
if (paging != null) {
query.offset(paging.getOffset()).limit(paging.getLimit());
}
if (StringUtils.isNotBlank(orderBy)) {
query.order(orderBy);
}
if (retriveFields.length > 0) {
query.retrievedFields(true, retriveFields);
}
return query;
}
/**
* 扩展此方法 可以在MAP中传field 的相应方法 同时可以传and和or
* MAP中的key传入"* and"或者"* or",value传相应的MAP,就满足了与或的操作
* 待验证,可能除filter方法以外的其他方式会有一些BUG
*
* @param properties
* @param paging
* @param orderBy
* @return
*/
protected Query<E> buildQuery(Map<String, Object> properties,
Paging paging, String orderBy) {
Query<E> query = super.createQuery();
String[] defaultOperator = new String[] { "=", "==", "!=", "<>", ">",
"<", ">=", "<=", "in", "nin", "all", "size", "exists" };
if (properties != null && !properties.isEmpty()) {
Iterator<Entry<String, Object>> it = properties.entrySet()
.iterator();
while (it.hasNext()) {
Entry<String, Object> entry = it.next();
if (StringUtils.isNotBlank(entry.getKey())) {
String[] keyArray = StringUtils.split(entry.getKey(), " ");
if (keyArray.length > 1) {
if (ArrayUtils.contains(defaultOperator, keyArray[1])) {
query.filter(entry.getKey(), entry.getValue());
} else {
if ("*".equals(keyArray[0])) {
@SuppressWarnings("unchecked")
Map<String, Object> subProperties = (Map<String, Object>) entry
.getValue();
switch (keyArray[1]) {
case "and":
query.and(buildOpCriteriaByMap(
subProperties, query));
break;
case "or":
query.or(buildOpCriteriaByMap(
subProperties, query));
break;
default:
break;
}
} else {
try {
Class<?> paramClass = Iterable.class;
if (String.class.equals(entry.getValue()
.getClass())) {
paramClass = String.class;
}
if (Object.class.equals(entry.getValue()
.getClass())) {
paramClass = Object.class;
}
final Method method = FieldEnd.class
.getMethod(keyArray[1], paramClass);
method.invoke(query.field(keyArray[0]),
entry.getValue());
} catch (Exception e) {
try {
final Method method = FieldEnd.class
.getMethod(keyArray[1],
Object.class);
method.invoke(
query.criteria(keyArray[0]),
entry.getValue());
} catch (Exception e1) {
LOGGER.warn(e1.getMessage(), e1);
}
}
}
}
} else {
query.filter(entry.getKey(), entry.getValue());
}
}
}
}
if (paging != null) {
query.offset(paging.getOffset()).limit(paging.getLimit());
}
if (StringUtils.isNotBlank(orderBy)) {
query.order(orderBy);
}
return query;
}
private CriteriaContainer[] buildOpCriteriaByMap(
Map<String, Object> properties, Query<E> query) {
ArrayList<CriteriaContainer> criteriaList = buildCriteriaByMap(
properties, query);
CriteriaContainer[] arrayList = new CriteriaContainer[criteriaList
.size()];
criteriaList.toArray(arrayList);
return arrayList;
}
private ArrayList<CriteriaContainer> buildCriteriaByMap(
Map<String, Object> properties, Query<E> query) {
ArrayList<CriteriaContainer> criteriaList = new ArrayList<CriteriaContainer>();
Iterator<Entry<String, Object>> subIt = properties.entrySet()
.iterator();
while (subIt.hasNext()) {
Entry<String, Object> subEntry = subIt.next();
if (StringUtils.isNotBlank(subEntry.getKey())) {
String[] subKeyArray = StringU
没有合适的资源?快使用搜索试试~ 我知道了~
url-shortenerurl 短网址,短链
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
共41个文件
java:23个
properties:5个
xml:4个
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 82 浏览量
2023-06-14
18:45:31
上传
评论
收藏 392KB ZIP 举报
温馨提示
url 短网址,短链。让你长长的url链接变得短短的
资源推荐
资源详情
资源评论
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![thumb](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JAR.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 41 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/de50ac10e30e41b89db28203f97239be_qq_41701956.jpg!1)
Java程序员-张凯
- 粉丝: 1w+
- 资源: 6828
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)