添加/编辑接口合并写法
Criteria criteria = new Criteria();
criteria.and("app_no").is(Constant.APP_NO);
if (StrUtil.isNotBlank(id)){
criteria.and("_id").is(new ObjectId(id));
}else{
criteria.and("_id").is(new ObjectId());
}
Update update = new Update();
update.set("type", type);
update.set("status", status);
update.setOnInsert("createDate", new Date());
update.currentDate("updateDate");
FindAndModifyOptions options = FindAndModifyOptions.options().upsert(true).returnNew(true);
Message message = mongoTemplate.findAndModify(new Query(criteria), update, options, Message.class);
Assert.notNull(message, "操作失败");
注意:
upsert
:不存在是否插入returnNew
:是否返回数据库中的新对象(可用于判断操作是否成功,不会null则表示操作成功)
升级版:
Criteria criteria = new Criteria();
if (StringUtils.hasLength(params.getId())) {
criteria.and("_id").is(new ObjectId(params.getId()));
} else {
criteria.and("_id").is(new ObjectId());
}
Update update = new Update();
update.set(FieldUtil.get(Company::getName), params.getName());
update.setOnInsert(FieldUtil.get(Company::getStatus), GlobalStatusEnum.STATUS_NORMAL.value());
update.setOnInsert(FieldUtil.get(Company::getCreateTime), new Date());
update.currentDate(FieldUtil.get(Company::getUpdateTime));
FindAndModifyOptions options = FindAndModifyOptions.options().upsert(true).returnNew(true);
Company modify = mongoTemplate.findAndModify(new Query(criteria), update, options, Company.class);
return Objects.nonNull(modify) ? Result.successMsg("成功") : Result.failMsg("失败");
去除文档_class
使用MongoTemplate时会发现数据库中多了一个_class字段,如果不需要,可以去除
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.convert.MongoConverter;
import javax.annotation.Resource;
/**
* 去除MongoDB数据中的_class
*/
@Configuration
public class MongoConfig implements ApplicationListener<ContextRefreshedEvent> {
@Resource
MongoTemplate mongoTemplate;
@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
MongoConverter converter = mongoTemplate.getConverter();
if (converter.getTypeMapper().isTypeKey("_class")) {
((MappingMongoConverter) converter).setTypeMapper(new DefaultMongoTypeMapper(null));
}
}
}
获取类的私有属性
用在MongoTemplate查询的时候,查询字段字段的情况,可以指定某个类
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
/**
* 类相关工具类
*/
public class ClassUtils {
/**
* 获取类私有字段名称
*/
public static String[] getPrivateFields(Class<?> cla) {
Field[] fields = cla.getDeclaredFields();
List<String> list = new ArrayList<>();
for (Field field : fields) {
if (field.getModifiers() == Modifier.PRIVATE) {
list.add(field.getName());
}
}
return list.toArray(new String[0]);
}
}
ObjectId序列号为String
在使用Java操作MongoDB时,主/外键会使用ObjectId类型,返回前端时需要序列化为String
注意:在使用Java操作MongoDB时,主键默认ObjectId
类型,在进行表关联的时候外键也需要为ObjectId
,主键、外键都需要定义为ObjectId
类型
而对于数据的响应,需要给的是字符类型,所以将JacksonConfig进行配置即可
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
/**
* 配置ObjectId序列号转化为String
*/
@Configuration
public class JacksonConfig implements InitializingBean {
@Resource
private ObjectMapper objectMapper;
@Override
public void afterPropertiesSet() {
SimpleModule simpleModule = new SimpleModule();
//指定 mongodb ObjectId 序列化方式
simpleModule.addSerializer(ObjectId.class, ToStringSerializer.instance);
objectMapper.registerModule(simpleModule);
}
}
通过Function获取字段名
解决update.set("type", type);
中字符问题,改为使用Lambda方式,这样更改了字段名会提示所有需要更改的地方,不至于对字符不可检查
import java.io.Serializable;
import java.lang.invoke.SerializedLambda;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.function.Function;
/**
* Java8通过Function函数获取字段名称(获取实体类的字段名称)
*/
public class FieldUtil {
/**
* 使Function获取序列化能力
*/
@FunctionalInterface
public interface SFunction<T, R> extends Function<T, R>, Serializable {
}
/**
* 获取实体类的字段名称
*/
public static <T> String get(SFunction<T, ?> fn) {
SerializedLambda serializedLambda = getSerializedLambda(fn);
// 从lambda信息取出method、field、class等
String fieldName = serializedLambda.getImplMethodName().substring("get".length());
fieldName = fieldName.replaceFirst(fieldName.charAt(0) + "", (fieldName.charAt(0) + "").toLowerCase());
System.out.println("--->" + fieldName);
return fieldName;
}
private static <T> SerializedLambda getSerializedLambda(SFunction<T, ?> fn) {
// 从function取出序列化方法
Method writeReplaceMethod;
try {
writeReplaceMethod = fn.getClass().getDeclaredMethod("writeReplace");
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
// 从序列化方法取出序列化的lambda信息
boolean isAccessible = writeReplaceMethod.isAccessible();
writeReplaceMethod.setAccessible(true);
SerializedLambda serializedLambda;
try {
serializedLambda = (SerializedLambda) writeReplaceMethod.invoke(fn);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
writeReplaceMethod.setAccessible(isAccessible);
return serializedLambda;
}
}
通用批量删除
@Override
public Result<String> comBatchDel(List<ComDeleteByIdReqVo> params, String tableName) {
// 请求的id
List<ObjectId> collect = params.stream().map(c -> new ObjectId(c.getId())).collect(Collectors.toList());
Update update = new Update();
update.set("updateTime", new Date());
update.set("status", GlobalStatusEnum.STATUS_DELETE.value());
long count = mongoTemplate.updateMulti(
new Query(Criteria.where("_id").in(collect)),
update,
tableName
).getMatchedCount();
return count > 0 ? Result.successMsg("成功") : Result.failMsg("失败");
}