参考待整理:https://www.cnblogs.com/yzykkpl/p/10356258.html
annotation注解:
import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;/** * 自定义注解,实现主键自增 * @author sea * */@Target(ElementType.FIELD)@Retention(RetentionPolicy.RUNTIME)public @interface AutoIncKey {}
AutoIncKeyEventListener 注解监听器:
package com.sea.test.annotation;import java.lang.reflect.Field;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.mongodb.core.FindAndModifyOptions;import org.springframework.data.mongodb.core.MongoTemplate;import org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener;import org.springframework.data.mongodb.core.mapping.event.BeforeConvertEvent;import org.springframework.data.mongodb.core.query.Criteria;import org.springframework.data.mongodb.core.query.Query;import org.springframework.data.mongodb.core.query.Update;import org.springframework.stereotype.Component;import org.springframework.util.ReflectionUtils;/** * 监听器用于监听MongoEvent * ,该类继承AbstractMongoEventListener类,因为我们需要在JAVA对象转换成数据库对象的时候操作id字段实现id自增, * 所以覆盖onBeforeConvert方法(详见spring-data文档,https://docs.spring.io/spring-data/data-document/docs/current/reference/html/,5.11节) * @author sea * */@Componentpublic class AutoIncKeyEventListener extends AbstractMongoEventListener
序列id
SeqInfo
import java.io.Serializable;public class SeqInfo implements Serializable { private static final long serialVersionUID = 1L; private Long seqId; public Long getSeqId() { return seqId; } public void setSeqId(Long seqId) { this.seqId = seqId; } }