Spring-Data-MongoDB如何自定義id生成規(guī)則以及id自增示例

MongoDB默認(rèn)的ObjectId確實(shí)有其積極意義,但是有時(shí)候需求卻需要我們自定義id生成規(guī)則。
本文使用AOP注解的方式來(lái)實(shí)現(xiàn)id的自定義規(guī)則。
如果聲明在id字段上,那它就是自定義的id。也可以聲明在其他字段上,與ObjectId并存。


本文由作者三汪首發(fā)于簡(jiǎn)書(shū)。

原理分析

  • Spring Data MongoDB是有生命周期的。通過(guò)繼承org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener類(lèi)來(lái)實(shí)現(xiàn)監(jiān)聽(tīng)。
    本文中產(chǎn)生id的動(dòng)作在監(jiān)聽(tīng)到onBeforeConvert時(shí)觸發(fā)。
    7個(gè)生命周期列舉如下:
    onBeforeConvert,onBeforeSave,onAfterSave,onAfterLoad,onAfterConvert,onAfterDelete,onBeforeDelete

  • 通過(guò)自定義注解標(biāo)識(shí)根據(jù)自定義規(guī)則賦值的字段。通過(guò)org.springframework.util.ReflectionUtils提供的反射功能來(lái)獲取被自定義注解標(biāo)識(shí)的字段

  • 實(shí)現(xiàn)根據(jù)數(shù)據(jù)庫(kù)中已存在的id生成新的id(自增),使用單例控制并發(fā)。

示例

注意:

  • 本例中自定義的id規(guī)則為:年月日+序號(hào)。如:2017112801,2017112899
    示例中沒(méi)有對(duì)自定義id的長(zhǎng)度做控制。因此會(huì)出現(xiàn)20171128100的情況。
    如果需要做控制,請(qǐng)自行在保存方法中做校驗(yàn)。
  • 本文中自定義id的類(lèi)型為String。
    根據(jù)所參考的博文作者的說(shuō)法,自增ID的類(lèi)型不能定義成Long這種包裝類(lèi)。
    Mongotemplate的源碼里面對(duì)主鍵ID的類(lèi)型有限制。只能定義為原生類(lèi)型。

代碼:

ProjectDomain.java(實(shí)體類(lèi))
@ProjectCode即本例中自定義的注解。)
@Getter @Setter @NoArgsConstructor是Lombok提供的注解。如有疑問(wèn)請(qǐng)自行查閱Lombok相關(guān)知識(shí))
@ApiModelProperty是Swagger提供的注解。如果未使用Swagger請(qǐng)忽略)

@Document
@Getter
@Setter
@NoArgsConstructor
public class ProjectDomain {

    @Id 
    @ProjectCode
    @ApiModelProperty(value = "項(xiàng)目編號(hào)")
    private String code;//code字段在數(shù)據(jù)庫(kù)中還是會(huì)以_id的名字存在
    
    @ApiModelProperty(value = "項(xiàng)目名稱(chēng)")
    @Field(value="name")
    private String name;

    //其余字段略
}

ProjectCode.java(自定義注解類(lèi))

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.FIELD)  
@Retention(RetentionPolicy.RUNTIME)  
public @interface ProjectCode {

}

CreateProjectCode.java( 生成規(guī)則定義和實(shí)際生成在此類(lèi)中)

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

import com.wolfgy.demo.domain.ProjectDomain;

@Component
class CreateProjectCode {
    
    private static CreateProjectCode createProjectCode = null;
    private CreateProjectCode(){}
    
    static synchronized CreateProjectCode getInstance(){
        if(createProjectCode == null){
            synchronized (CreateProjectCode.class) {
                if(createProjectCode == null){
                    createProjectCode = new CreateProjectCode();
                }
            }
        }
        return createProjectCode;
    }
    
    synchronized String createProjectCode(MongoTemplate template){
        LocalDate date = LocalDate.now();
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyyMMdd");
        String dateStr = date.format(fmt);
        Query query = new Query(Criteria.where("code").regex(dateStr+"\\d*")).with(new Sort(Direction.DESC,"code"));
        List<ProjectDomain> list = template.find(query, ProjectDomain.class);
        if (list != null && !list.isEmpty()) {
            String numStr = list.get(0).getCode().substring(8);
            Integer numInteger = new Integer(numStr);
            int num = numInteger.intValue();
            return dateStr+( ++num<10 ? "0"+num : num ); 
        }else{
            return dateStr+"01";
        }
    }
}

SaveEventListener.java(事件監(jiān)聽(tīng)類(lèi))

import java.lang.reflect.Field;

import org.springframework.beans.factory.annotation.Autowired;
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.stereotype.Component;
import org.springframework.util.ReflectionUtils;

import com.wolfgy.demo.domain.ProjectDomain;

@Component
public class SaveEventListener extends AbstractMongoEventListener<ProjectDomain>{
    
    @Autowired
    private MongoTemplate template;
    
    @Override
    public void onBeforeConvert(BeforeConvertEvent<ProjectDomain> event) {
        final Object source = event.getSource();
        if (source != null) {
            ReflectionUtils.doWithFields(source.getClass(), new ReflectionUtils.FieldCallback() {               
                @Override
                public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
                    ReflectionUtils.makeAccessible(field);
                    if (field.isAnnotationPresent(ProjectCode.class)) {//判斷字段是否被自定義注解標(biāo)識(shí)
                        field.set(source,CreateProjectCode.getInstance().createProjectCode(template));//設(shè)置id
                    }
                }
            });
        }
    }
    

}

參考內(nèi)容


以上。
希望我的文章對(duì)你能有所幫助。
我不能保證文中所有說(shuō)法的百分百正確,
但我能保證它們都是我的理解和感悟以及拒絕直接復(fù)制黏貼(確實(shí)需要引用的部分我會(huì)附上源地址)。
有什么意見(jiàn)、見(jiàn)解或疑惑,歡迎留言討論。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容