MongoDB orm框架的注意事項(xiàng)及簡(jiǎn)單使用
首先spring自帶了mongodb的orm,spring data mongodb,但是這個(gè)框架非常難用,最令人抓狂的是每個(gè)文檔都要帶一個(gè) _class 字段,因?yàn)檫@個(gè)是string的,所以占用不少空間,而且去除也比較麻煩。故而使用 zfoo orm 框架
Ⅰ. 簡(jiǎn)介
基于MongoDB的orm框架,提供POJO對(duì)象和MongoDB數(shù)據(jù)庫之間的映射
Ⅱ. 注意事項(xiàng)
- POJO對(duì)象的屬性必須提供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)的主鍵是一個(gè)字符串,比較占空間
- 數(shù)據(jù)庫使用自研的orm框架,比如一個(gè)實(shí)體類UserEntity,映射到數(shù)據(jù)庫中的集合為user,首字母小寫,去掉Entity
- 基于 caffeine 的高性能數(shù)據(jù)緩存
- 語法校驗(yàn),如對(duì)沒有加上get和set的字段自動(dòng)語法提示
Ⅲ. 使用方法
1. IAccessor接口,為數(shù)據(jù)訪問接口
- 插入數(shù)據(jù)到數(shù)據(jù)庫,會(huì)以對(duì)象的id()方法的返回值作為主鍵
OrmContext.getAccessor().insert(obj)
- 刪除數(shù)據(jù)庫中的數(shù)據(jù),會(huì)以對(duì)象的id()方法的返回值作為查找關(guān)鍵字,刪除以這個(gè)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表示操作哪個(gè)數(shù)據(jù)庫
- address表示數(shù)據(jù)庫的地址,支持分片的配置
- caches中的strategy表示一個(gè)緩存的策略,即將數(shù)據(jù)庫中的數(shù)據(jù)先讀入Orm中的EntityCaches緩存,如hundred這個(gè)策略表示,緩存數(shù)據(jù)庫中1000條數(shù)據(jù),10分鐘過期
- persisters中的strategy表示一個(gè)持久化的策略,如3s這個(gè)策略表示,將EntityCaches中的緩存數(shù)據(jù)每3s寫入到數(shù)據(jù)庫中一次,即使中途宕機(jī),也只損失3秒的數(shù)據(jù)
- EntityCaches這個(gè)注解表示將會(huì)被Orm管理,使用hundred策略,緩存的持久化策略為3s
Ⅳ. 教程
test下中包含了所有增刪改查的教程,運(yùn)行之前請(qǐng)先安裝MongoDB
以上就是MongoDB orm框架的注意事項(xiàng)及簡(jiǎn)單使用的詳細(xì)內(nèi)容,更多關(guān)于MongoDB orm框架的資料請(qǐng)關(guān)注本站其它相關(guān)文章!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。