MongoDB orm框架的注意事項(xiàng)及簡單使用
首先spring自帶了mongodb的orm,spring data mongodb,但是這個框架非常難用,最令人抓狂的是每個文檔都要帶一個 _class 字段,因?yàn)檫@個是string的,所以占用不少空間,而且去除也比較麻煩。故而使用 zfoo orm 框架
Ⅰ. 簡介
基于MongoDB的orm框架,提供POJO對象和MongoDB數(shù)據(jù)庫之間的映射
Ⅱ. 注意事項(xiàng)
- POJO對象的屬性必須提供get和set方法,否則無法映射
- 不支持泛型
- 如果不想映射某屬性,直接加上transient關(guān)鍵字
- 目前支持基本數(shù)據(jù)屬性(byte,short,int,long,float,double,boolean),字符串String,List,Set集合屬性的映射,不支持Map
- 數(shù)據(jù)庫主鍵能用整數(shù)盡量用整數(shù),因?yàn)镸ongoDB默認(rèn)的主鍵是一個字符串,比較占空間
- 數(shù)據(jù)庫使用自研的orm框架,比如一個實(shí)體類UserEntity,映射到數(shù)據(jù)庫中的集合為user,首字母小寫,去掉Entity
- 基于 caffeine 的高性能數(shù)據(jù)緩存
- 語法校驗(yàn),如對沒有加上get和set的字段自動語法提示
Ⅲ. 使用方法
1. IAccessor接口,為數(shù)據(jù)訪問接口
- 插入數(shù)據(jù)到數(shù)據(jù)庫,會以對象的id()方法的返回值作為主鍵
OrmContext.getAccessor().insert(obj)
- 刪除數(shù)據(jù)庫中的數(shù)據(jù),會以對象的id()方法的返回值作為查找關(guān)鍵字,刪除以這個id()為主鍵的數(shù)據(jù)
OrmContext.getAccessor().delete(obj);
- 修改數(shù)據(jù)庫中的數(shù)據(jù)
OrmContext.getAccessor().update(obj);
2. IQuery接口,為數(shù)據(jù)復(fù)雜查詢接口
3. 緩存使用方法
例如有下列配置
<orm:config id="config" entity-package="com.zfoo.orm.**.entity"> <orm:host database="test" user="" password=""> <orm:address name="server0" url="127.0.0.1:27017"/> </orm:host> <!-- 緩存策略 --> <orm:caches> <orm:cache strategy="ten" size="10" expire-millisecond="600000"/> <orm:cache strategy="hundred" size="100" expire-millisecond="600000"/> <orm:cache strategy="thousand" size="1000" expire-millisecond="600000"/> <orm:cache strategy="threeThousand" size="3000" expire-millisecond="600000"/> <orm:cache strategy="tenThousand" size="10000" expire-millisecond="600000"/> </orm:caches> <!-- 持久化策略 --> <orm:persisters> <orm:persister strategy="cronDefault" type="cron" config="0,30 * * * * ?"/> <orm:persister strategy="cron3s" type="cron" config="0/3 * * * * ?"/> <orm:persister strategy="cron15s" type="cron" config="0/15 * * * * ?"/> <orm:persister strategy="cron30s" type="cron" config="0/30 * * * * ?"/> <orm:persister strategy="cron1m" type="cron" config="0 0/30 * * * ?"/> <orm:persister strategy="time30s" type="time" config="30000"/> </orm:persisters> </orm:config>
有下列注解
@EntityCaches(cacheStrategy = "tenThousand", persister = @Persister("time30s")) public class UserEntity implements IEntity<Long> { }
- database表示操作哪個數(shù)據(jù)庫
- address表示數(shù)據(jù)庫的地址,支持分片的配置
- caches中的strategy表示一個緩存的策略,即將數(shù)據(jù)庫中的數(shù)據(jù)先讀入Orm中的EntityCaches緩存,如hundred這個策略表示,緩存數(shù)據(jù)庫中1000條數(shù)據(jù),10分鐘過期
- persisters中的strategy表示一個持久化的策略,如3s這個策略表示,將EntityCaches中的緩存數(shù)據(jù)每3s寫入到數(shù)據(jù)庫中一次,即使中途宕機(jī),也只損失3秒的數(shù)據(jù)
- EntityCaches這個注解表示將會被Orm管理,使用hundred策略,緩存的持久化策略為3s
Ⅳ. 教程
test下中包含了所有增刪改查的教程,運(yùn)行之前請先安裝MongoDB
以上就是MongoDB orm框架的注意事項(xiàng)及簡單使用的詳細(xì)內(nèi)容,更多關(guān)于MongoDB orm框架的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
MongoDB客戶端工具NoSQL?Manager?for?MongoDB介紹
這篇文章介紹了MongoDB客戶端工具NoSQL?Manager?for?MongoDB,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06

MongoDB系列教程(六):java操作mongodb實(shí)例

MongoDB運(yùn)行狀態(tài)監(jiān)控、性能分析工具mongostat詳解

MongoDB服務(wù)端JavaScript腳本使用方法