像这样创建注释:
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface MongoUpdate {
String find() default "{}";
String update() default "{}";
String collection();
boolean multi() default false;
}
像这样的一个方面:
@Aspect
@Component
@SuppressWarnings("unchecked")
public class MongoUpdateAspect {
private static final Logger logger = LoggerFactory.getLogger(MongoUpdateAspect.class);
@Autowired
private MongoTemplate mongoTemplate;
@Pointcut("@annotation(com.ofb.commons.aop.common.MongoUpdate)")
public void pointCut() {
}
@Around("com.ofb.commons.aspect.MongoUpdateAspect.pointCut() && @annotation(mongoUpdate)")
public Object applyQueryUpdate(ProceedingJoinPoint joinPoint, MongoUpdate mongoUpdate) throws Throwable {
Object[] args = joinPoint.getArgs();
String findQuery = mongoUpdate.find();
String updateQuery = mongoUpdate.update();
String collection = mongoUpdate.collection();
boolean multiUpdate = mongoUpdate.multi();
for (int i = 0; i < args.length; i++) {
if (args[i] instanceof Collection) {
Collection collection1 = (Collection) args[i];
String replaceStr = (String) collection1.stream().map(object -> {
if (object instanceof Number) {
return object.toString();
} else {
return String.format("\"%s\"", object.toString());
}
}).collect(Collectors.joining(","));
findQuery = findQuery.replace(String.format("?%s", i), replaceStr);
updateQuery = updateQuery.replace(String.format("?%s", i), replaceStr);
} else if (args[i] instanceof Object[]) {
Object[] objects = (Object[]) args[i];
String replaceStr = Arrays.stream(objects).map(object -> {
if (object instanceof Number) {
return object.toString();
} else {
return String.format("\"%s\"", object.toString());
}
}).collect(Collectors.joining(","));
findQuery = findQuery.replace(String.format("?%s", i), replaceStr);
updateQuery = updateQuery.replace(String.format("?%s", i), replaceStr);
} else {
if (args[i] instanceof Number) {
findQuery = findQuery.replace(String.format("?%s", i), args[i].toString());
updateQuery.replace(String.format("?%s", i), args[i].toString());
} else {
findQuery = findQuery.replace(String.format("?%s", i), String.format("\"%s\"", args[i].toString()));
updateQuery =
updateQuery.replace(String.format("?%s", i), String.format("\"%s\"", args[i].toString()));
}
}
}
Query query = new BasicQuery(findQuery);
Update update = new BasicUpdate(updateQuery);
if (multiUpdate) {
mongoTemplate.updateMulti(query, update, collection);
} else {
mongoTemplate.updateFirst(query, update, collection);
}
return null;
}
}
这在 MongoRepository 实现的接口中不起作用,但您可以在服务层中创建一个空的主体方法
@MongoUpdate(find = {}, update = "{$push : {'offFeatures' : ?0}}", collection = "userPreference", multi = true)
public void offFeatures(String feature) {
}