上一篇的文章连接:Spring MongoDB工具类 - 掘金 (juejin.cn)
这个工具类的想法呢,就是能够根据对象的属性相关注解直接构造成最终一个完整的条件。而不是原始的那样要写很多条件代码。
当初写的时候呢,只是为了简单的满足一下业务需求,能够方便的更好操作MongoDB相关业务,就写了上一篇文章,然后这一年时间陆陆续续也接收到了许多小伙伴的好友请求需要这个工具类的源码,我也放在了github上面,github.com/sifan-hongc…
由于一直在忙工作上的事,也没有时间对这个小工具类进行优化,今天晚上呢,就折腾出一点时间来重构了。
之前的handler主要集内部的两个Enum上来获取,小伙伴们估计修改起来也麻烦,就把他重构成一个register的形式来使用。能够更好的添加相关注解的handler。由于MongoDB也不是很太熟悉,目前就只有下面的2个相关注解的register类。一个对key构造成条件的注解,一个是运算符的注解。
java
复制代码
`package com.hongcha.mongodb.core;
import java.lang.annotation.Annotation;
import java.util.Map;
/**
*/
public interface ConditionsAnnotationHandlerRegister {
/**
*
*/
void registerHandler(Class<? extends Annotation> annotation, ConditionsAnnotationHandler conditionsAnnotationHandler);
/**
*
*/
Map<Class<? extends Annotation>, ConditionsAnnotationHandler> getAllRegisteredHandler();
ConditionsAnnotationHandler getHandler(Class<? extends Annotation> annotation);
}`
java
复制代码
`package com.hongcha.mongodb.core;
import java.lang.annotation.Annotation;
import java.util.Map;
/**
*/
public interface OperatorAnnotationHandlerRegister {
/**
*
*/
void registerHandler(Class<? extends Annotation> annotation, OperatorAnnotationHandler operatorAnnotationHandler);
/**
*/
Map<Class<? extends Annotation>, OperatorAnnotationHandler> getAllRegisteredHandler();
OperatorAnnotationHandler getHandler(Class<? extends Annotation> annotation);
}`
也提供BaseService,能够快速的对单表进行操作
java
复制代码
`package com.hongcha.mongodb.core.service;
import com.hongcha.mongodb.core.Page;
import com.hongcha.mongodb.core.annotation.ConditionsAnnotation;
import com.hongcha.mongodb.core.annotation.OperatorAnnotation;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import java.util.List;
/**
*
*/
public interface BaseMongoService<T> {
/**
*/
T getById(String id);
/**
*
*/
T checkByIdAngGet(String id, String message);
/**
*
*/
boolean insert(T t);
/**
*
*/
boolean insertBatch(List<T> list);
/**
*
*/
boolean deleteById(String id);
/**
*
*/
boolean delete(Object objectParam);
/**
*
*/
boolean delete(Query query);
/**
*
*/
boolean updateById(String id, Object objectUpdate);
/**
*
*/
boolean updateById(String id, Update update);
/**
*/
boolean update(Object objectParam, Object objectUpdate);
/**
*
*/
boolean update(Query query, Update update);
/**
*
*/
List<T> select();
/**
*
*/
List<T> select(Object objectParam);
/**
*
*/
<R> List<R> select(Object objectParam, Class<R> returnClass);
/**
*
*/
List<T> select(Query query);
/**
*
*/
<R> List<R> select(Query query, Class<R> returnClass);
T selectOne(Object objectParam);
/**
*
*/
<R> R selectOne(Object objectParam, Class<R> returnClass);
T selectOne(Query query);
/**
*
*/
<R> R selectOne(Query query, Class<R> returnClass);
Page<T> page(Object objectParam, Page<T> page);
/**
*
*/
<R> Page<R> page(Object objectParam, Page<R> page, Class<R> returnClass);
Page<T> page(Query query, Page<T> page);
/**
*
*/
<R> Page<R> page(Query query, Page<R> page, Class<R> returnClass);
long count();
/**
*
*/
long count(Object objectParam);
/**
*
*/
long count(Query query);
/**
*
*/
Page<T> aggregatePage(List<AggregationOperation> conditions, Page<T> page);
/**
*
*/
<R> Page<R> aggregatePage(List<AggregationOperation> conditions, Page<R> page, Class<R> clazz);
/**
*
*/
<R> Page<R> aggregatePage(List<AggregationOperation> conditions, Sort sort, Page<R> page, Class<R> clazz);
/**
*
*/
List<T> aggregateData(Aggregation aggregation);
/**
*
*/
<R> List<R> aggregateData(Aggregation aggregation, Class<R> outputType);
/**
*
*/
<R> AggregationResults<R> aggregate(Aggregation aggregation, Class<R> outputType);
}`
基本上BaseService覆盖了大部分简单的场景,也支持原生的写法。
目前支持的相关注解:
scss
复制代码
`package com.hongcha.mongodb.starter;
import com.hongcha.mongodb.core.*;
import com.hongcha.mongodb.core.annotation.*;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.util.StringUtils;
import java.lang.annotation.Annotation;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiFunction;
@Configurable
public class MongoHelperAutoConfig implements InitializingBean {
private static Map<Class<? extends Annotation>, ConditionsAnnotationHandler> DEFAULT_CONDITIONS = new HashMap<>();
private static Map<Class<? extends Annotation>, OperatorAnnotationHandler> DEFAULT_OPERATOR = new HashMap<>();
static {
DEFAULT_CONDITIONS.put(Eq.class, (criteria, value) -> skipNullHandler(criteria, value, Criteria::is));
DEFAULT_CONDITIONS.put(Gt.class, (criteria, value) -> skipNullHandler(criteria, value, Criteria::gt));
DEFAULT_CONDITIONS.put(Gte.class, (criteria, value) -> skipNullHandler(criteria, value, Criteria::gte));
DEFAULT_CONDITIONS.put(Lt.class, (criteria, value) -> skipNullHandler(criteria, value, Criteria::lt));
DEFAULT_CONDITIONS.put(Lte.class, (criteria, value) -> skipNullHandler(criteria, value, Criteria::lte));
DEFAULT_CONDITIONS.put(Ne.class, (criteria, value) -> skipNullHandler(criteria, value, Criteria::ne));
DEFAULT_CONDITIONS.put(In.class, (criteria, value) -> skipNullHandler(criteria, value, Criteria::in));
DEFAULT_CONDITIONS.put(Regex.class, (criteria, value) -> {
if (value == null)
return criteria;
String strValue = value.toString();
return skipNullHandler(criteria, strValue, Criteria::regex);
});
}
static {
DEFAULT_OPERATOR.put(AndOperator.class, ((criteriaLeft, criteriaRight) -> skipNullHandler(criteriaLeft, criteriaRight, Criteria::andOperator)));
DEFAULT_OPERATOR.put(OrOperator.class, ((criteriaLeft, criteriaRight) -> skipNullHandler(criteriaLeft, criteriaRight, Criteria::orOperator)));
DEFAULT_OPERATOR.put(NorOperator.class, ((criteriaLeft, criteriaRight) -> skipNullHandler(criteriaLeft, criteriaRight, Criteria::norOperator)));
}
@Autowired
ConditionsAnnotationHandlerRegister conditionsAnnotationHandlerRegister;
@Autowired
OperatorAnnotationHandlerRegister operatorAnnotationHandlerRegister;
private static <T> Criteria skipNullHandler(Criteria criteria, T value, BiFunction<Criteria, T, Criteria> biFunction) {
if (value == null)
return criteria;
if (value instanceof String) {
String s = (String) value;
if (!StringUtils.hasText(s))
return criteria;
}
if (value instanceof Collection) {
Collection c = (Collection) value;
if (c.isEmpty())
return criteria;
}
return biFunction.apply(criteria, value);
}
@ConditionalOnMissingBean
@Bean
public ConditionsAnnotationHandlerRegister conditionsAnnotationHandlerRegister() {
return new DefaultConditionsAnnotationHandlerRegister();
}
@ConditionalOnMissingBean
@Bean
public OperatorAnnotationHandlerRegister operatorAnnotationHandlerRegister() {
return new DefaultOperatorAnnotationHandlerRegister();
}
@ConditionalOnMissingBean
@Bean
public MongoHelper mongoDBHelper() {
return new MongoHelper();
}
@Override
public void afterPropertiesSet() throws Exception {
initConditionsAnnotationHandlerRegister(conditionsAnnotationHandlerRegister);
initOperatorAnnotationHandlerRegister(operatorAnnotationHandlerRegister);
}
private void initOperatorAnnotationHandlerRegister(OperatorAnnotationHandlerRegister register) {
DEFAULT_OPERATOR.forEach((annotation, handler) -> {
if (register.getHandler(annotation) == null) {
register.registerHandler(annotation, handler);
}
});
}
private void initConditionsAnnotationHandlerRegister(ConditionsAnnotationHandlerRegister register) {
DEFAULT_CONDITIONS.forEach((annotation, handler) -> {
if (register.getHandler(annotation) == null) {
register.registerHandler(annotation, handler);
}
});
}
}`
如果想自定义添加或者覆盖,只需要引入相关register的bean来调用registerHandler即可扩展。
小伙伴们想使用的话,可以查看上一篇文章的demo内容,或者咨询我。
在使用中出现bug的话或者想增加新功能,也可微信联系我: 13480901614
觉得这个工具类不错的话,麻烦大家点个赞、GitHub点个星星。github.com/sifan-hongc…
谢谢观看!!!
原网址: 访问
创建于: 2024-06-18 10:55:42
目录: default
标签: 无
未标明原创文章均为采集,版权归作者所有,转载无需和我联系,请注明原出处,南摩阿彌陀佛,知识,不只知道,要得到
最新评论