欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring mongo 对象化CRUD

程序员文章站 2022-03-06 12:12:56
BaseServicepublic interface BaseService {List selectByMap(Map params);long selectCount(Map params);T save(T t);long update(T t);long deleteById(ID id);T getById(ID i...

BaseService

/**
 * @author zhuzi
 */
public interface BaseService<T, ID> {

	List<T> selectByMap(Map<String, Object> params);

	long selectCount(Map<String, Object> params);
	
	T save(T t);
	
	long update(T t);
	
	long deleteById(ID id);
	
	T getById(ID id);

}

BaseServiceImpl

/**
 * @author zhuzi
 */
public abstract class BaseServiceImpl<T, ID> implements BaseService<T, ID> {
	
	public abstract Query getQuery(Map<String, Object> params, boolean isCount);

	@SuppressWarnings("unchecked")
	public Class<T> getclass() {
		Type genType = getClass().getGenericSuperclass();  
		Type[] params = ((ParameterizedType) genType).getActualTypeArguments();  
		return (Class<T>) params[0];
	}
	
	public MongoTemplate getMongoTemplate() {
		return SpringContextUtil.getObject(MongoTemplate.class);
	}
	
	public List<T> selectByMap(Map<String, Object> params) {
		Query query = getQuery(params, false);
		List<T> list = getMongoTemplate().find(query, getclass());
		return list;
	}

	public long selectCount(Map<String, Object> params) {
		Query query = getQuery(params, true);
		return getMongoTemplate().count(query, getclass());
	}
	
	public T save(T t) {
		return getMongoTemplate().insert(t);
	}
	
	public long update(T t) {
		Query query = new Query();
		query.addCriteria(Criteria.where(getIdname()).is(getId(t)));
		
		UpdateResult result = getMongoTemplate().updateFirst(query, getUpdate(t), getclass());
		if(result != null) {
			return result.getModifiedCount();
		}
		return 0;
	}
	
	public Object getId(T t) {
		Field[] fs = getclass().getDeclaredFields();
		if(fs != null && fs.length > 0) {
			for(Field f : fs) {
				if(f.getAnnotation(Id.class) != null) {
					String key = f.getName();
					return ReflectMathodUtil.getValue(t, key);
				}
			}
		}
		return null;
	}

	public String getIdname() {
		Field[] fs = getclass().getDeclaredFields();
		if(fs != null && fs.length > 0) {
			for(Field f : fs) {
				if(f.getAnnotation(Id.class) != null) {
					return f.getName();
				}
			}
		}
		return null;
	}

	public Update getUpdate(T t) {
		Update update = new Update();
		Field[] fs = getclass().getDeclaredFields();
		boolean flag = false;
		if(fs != null && fs.length > 0) {
			for(Field f : fs) {
				if(!f.getName().equals(getIdname()) && !"serialVersionUID".equals(f.getName())) {
					String key = f.getName();
					Object value = ReflectMathodUtil.getValue(t, key);
					if(value != null) {
						flag = true;
						org.springframework.data.mongodb.core.mapping.Field af = f.getAnnotation(org.springframework.data.mongodb.core.mapping.Field.class);
						if(af != null) {
							update.set(af.value(), value);
						} else {
							update.set(key, value);
						}
					}
				}
			}
		}
		
		if(flag) {
			return update;
		}
		return null;
	}
	
	public long deleteById (ID id){
		try {
			Query query = new Query();
			query.addCriteria(Criteria.where(getIdname()).is(id));
			
			DeleteResult result = getMongoTemplate().remove(query, getclass());
			if(result != null) {
				return result.getDeletedCount();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}
	
	public T getById(ID id) {
		Map<String, Object> params = Maps.newHashMap();
		params.put(getIdname(), id);
		Query query = getQuery(params, false);
		List<T> list = getMongoTemplate().find(query, getclass());
		if(list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
	
}

 

本文地址:https://blog.csdn.net/u010379996/article/details/111983682