|
|
|
@ -12,6 +12,8 @@ import com.mongodb.client.model.Sorts;
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
import org.bson.Document;
|
|
|
|
|
import org.bson.conversions.Bson;
|
|
|
|
|
import org.bson.json.JsonMode;
|
|
|
|
|
import org.bson.json.JsonWriterSettings;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import org.springframework.data.mapping.context.MappingContext;
|
|
|
|
@ -20,6 +22,7 @@ import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
|
|
|
|
|
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
|
|
|
|
|
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
|
|
|
|
|
import org.springframework.data.mongodb.repository.support.SimpleMongoRepository;
|
|
|
|
|
import springfox.documentation.spring.web.json.Json;
|
|
|
|
|
|
|
|
|
|
import javax.persistence.Id;
|
|
|
|
|
import java.io.Serializable;
|
|
|
|
@ -47,6 +50,7 @@ public class BaseMongoRepositoryImpl<T, ID extends Serializable> extends SimpleM
|
|
|
|
|
private final MongoEntityInformation<T, ID> entityInformation;
|
|
|
|
|
private final MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mongoContext;
|
|
|
|
|
private final Class<T> entityClass;
|
|
|
|
|
private SnowflakeIdMaker snowflakeIdMaker;
|
|
|
|
|
|
|
|
|
|
public BaseMongoRepositoryImpl(MongoEntityInformation<T, ID> metadata, MongoOperations mongoOperations) {
|
|
|
|
|
super(metadata, mongoOperations);
|
|
|
|
@ -54,6 +58,7 @@ public class BaseMongoRepositoryImpl<T, ID extends Serializable> extends SimpleM
|
|
|
|
|
this.mongoOperations = mongoOperations;
|
|
|
|
|
this.entityClass = this.entityInformation.getJavaType();
|
|
|
|
|
mongoContext = mongoOperations.getConverter().getMappingContext();
|
|
|
|
|
snowflakeIdMaker = new SnowflakeIdMaker();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -99,7 +104,7 @@ public class BaseMongoRepositoryImpl<T, ID extends Serializable> extends SimpleM
|
|
|
|
|
Object val = idField.get(item);
|
|
|
|
|
if((type == long.class || type == Long.class) && (val == null || Long.parseLong(val.toString()) == 0)){
|
|
|
|
|
// long类型主键,以snowflake为主键
|
|
|
|
|
idField.set(item, new SnowflakeIdMaker().nextId());
|
|
|
|
|
idField.set(item, snowflakeIdMaker.nextId());
|
|
|
|
|
} else if(type == String.class && (val==null || "".equals(val))){
|
|
|
|
|
// String类型主键,以UUID为主键
|
|
|
|
|
idField.set(item, UUID.randomUUID().toString().replace("-", "").toLowerCase());
|
|
|
|
@ -222,8 +227,10 @@ public class BaseMongoRepositoryImpl<T, ID extends Serializable> extends SimpleM
|
|
|
|
|
private List<T> packObjectListFromDocument(List<Document> dList) {
|
|
|
|
|
//将获取的document转为对象
|
|
|
|
|
List<T> resultList = new ArrayList<>();
|
|
|
|
|
// 设置为宽松模式
|
|
|
|
|
JsonWriterSettings jsonWriterSettings = JsonWriterSettings.builder().outputMode(JsonMode.RELAXED).build();
|
|
|
|
|
for(Document d : dList){
|
|
|
|
|
resultList.add(JSONObject.parseObject(d.toJson(), entityClass));
|
|
|
|
|
resultList.add(JSONObject.parseObject(d.toJson(jsonWriterSettings), entityClass));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return resultList;
|
|
|
|
|