日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

记一次mongoDB-@Document(collection = “XXX“)配置的探索

發(fā)布時間:2025/3/21 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 记一次mongoDB-@Document(collection = “XXX“)配置的探索 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

直接開門見山,大多數(shù)會用mongoDB開發(fā)的都知道實體類上加上

@Document(collection = “XXX”)

可以直接將操作指定到對應(yīng)的XXX 集合中,但是對應(yīng)第一次玩mongoDB的我卻渾然不知(留下沒有技術(shù)的眼淚)。起因是因為每次調(diào)用都需要指定collectionName,如下

Long totalCount = mongoOperations.count(query, reqDTO.getClass(),"refund_error"); List<OperatorBillingSummaryPO> list = mongoOperations.find(query, OperatorBillingSummaryPO.class,"operator_billing_summary");

如果不指定collectionName則用默認的類名(等下會講到源碼)
對于處女座的我而言看著特別不舒服,很想把他們?nèi)恳?guī)整化,于是便有了以下的探索

1、方案一

首先我第一個想到的是將MongoOperations(或者MongoTemplate)在業(yè)務(wù)代碼上所用到
操作(新增修改查詢那些)全部用統(tǒng)一MongoDBUtil工具類封裝,每次都通過工具類進行操作,collectionName 則通過枚舉匹配,或者傳入Class類的類名進行分解拆分(這種方法比較土),將RefundErrorInfoBO 轉(zhuǎn)成 refund_error 類似這樣,除此之外,也可以用ThreadLocal或者緩存的方式查詢,因為思路比較簡單就不貼代碼了。

2、方案二

由于方案一不合適,直接去spring-data-mongodb的官網(wǎng)搜(https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mongodb.mapping-usage.events),


于是發(fā)現(xiàn)有個生命周期事件,可通過繼承 AbstractMongoEventListener 事件監(jiān)聽器進行事件監(jiān)聽,不過只能拿到event事件的實體source、事件處理后的Document以及我朝思暮想的collectionName,但并不能設(shè)置具體值,MongoMappingEvent 類如下

public class MongoMappingEvent<T> extends ApplicationEvent {private static final long serialVersionUID = 1L;private final @Nullable Document document;private final @Nullable String collectionName;/*** Creates new {@link MongoMappingEvent}.** @param source must not be {@literal null}.* @param document can be {@literal null}.* @param collectionName can be {@literal null}.*/public MongoMappingEvent(T source, @Nullable Document document, @Nullable String collectionName) {super(source);this.document = document;this.collectionName = collectionName;}/*** @return {@literal null} if not set.*/public @Nullable Document getDocument() {return document;}/*** Get the collection the event refers to.** @return {@literal null} if not set.* @since 1.8*/public @Nullable String getCollectionName() {return collectionName;}/** (non-Javadoc)* @see java.util.EventObject#getSource()*/@SuppressWarnings({ "unchecked" })@Overridepublic T getSource() {return (T) super.getSource();} }

3、方案三

最直接的方法,也就是看源碼
首先從find方法入手
org.springframework.data.mongodb.core.MongoTemplate#find(org.springframework.data.mongodb.core.query.Query, java.lang.Class)

進入設(shè)置方法
org.springframework.data.mongodb.core.MongoTemplate#determineCollectionName

獲取PersistentEntity
org.springframework.data.mapping.context.MappingContext#getRequiredPersistentEntity(java.lang.Class<?>)
從Debug模式進入可以看到已經(jīng)獲取到collection了(此時已添加@Document(collection = “refund_error”)注解)

于是返回上層的設(shè)置方法
org.springframework.data.mapping.context.AbstractMappingContext#getPersistentEntity(org.springframework.data.util.TypeInformation<?>)
entity不為空,在此退出,可以看到entity是通過AbstractMappingContext內(nèi)置的HashMap常量persistentEntities獲取到的

persistentEntities是怎么設(shè)置進來的呢?點擊可以看到調(diào)用的put方法,在此方法下
org.springframework.data.mapping.context.AbstractMappingContext#addPersistentEntity(org.springframework.data.util.TypeInformation<?>)

entity還是被設(shè)置了,繼續(xù)往上看createPersistentEntity方法

createPersistentEntity有三個實現(xiàn)類,看MongoMappingContext的方法
org.springframework.data.mongodb.core.mapping.MongoMappingContext#createPersistentEntity
,此時還是被設(shè)置
繼續(xù)往上,終于找到了~

此方法的意思是使用給定的 TypeInformation 創(chuàng)建一個新的 BasicMongoPersistentEntity。如果@Document設(shè)置了collection則用設(shè)置的值,沒有則將集合名稱設(shè)為實體類型名,實體名則用org.springframework.data.mongodb.MongoCollectionUtils#getPreferredCollectionName獲取,并將首字母大寫字母轉(zhuǎn)為小寫。

至于org.springframework.data.mapping.context.AbstractMappingContext#addPersistentEntity(java.lang.Class<?>)什么時候調(diào)用的,則是在SpringAOP初始化bean時,會調(diào)用初始化方法afterPropertiesSet

進而進入
org.springframework.data.mapping.context.AbstractMappingContext#initialize
,再調(diào)用org.springframework.data.mapping.context.AbstractMappingContext#addPersistentEntity(java.lang.Class<?>)實現(xiàn)整個屬性的設(shè)置

思路比較簡單,有疏漏的望各位大佬指點,小弟先告退了

總結(jié)

以上是生活随笔為你收集整理的记一次mongoDB-@Document(collection = “XXX“)配置的探索的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。