- 浏览: 420642 次
- 性别:
- 来自: 杭州
文章分类
- 全部博客 (269)
- 原创 (7)
- Java (51)
- Java Concurrency (2)
- IDE (16)
- Linux (46)
- Database (23)
- NoSQL (35)
- Web服务器 (23)
- Log日志 (11)
- HTTP (11)
- HTML (2)
- XML (1)
- Test (7)
- Mina (0)
- Amoeba (4)
- Cobar (1)
- 序列化 (2)
- Python (5)
- PHP (1)
- Socket通信 (1)
- Network (3)
- Struts (2)
- Web前端 (10)
- Maven (6)
- SVN (15)
- Json (1)
- XMPP (2)
- Go (1)
- Other (4)
- 未整理 (5)
最新评论
-
u012374672:
[color=darkred][/color][flash=2 ...
Mongo的ORM框架的学习Morphia(annotations) -
b_l_east:
很有问题啊
利用redis的transaction功能,实现分布式下加锁
转自:http://topmanopensource.iteye.com/blog/1437649
针对Mongo数据库访问,morphia提供了访问的基本的接口便于开发人员实现。
源代码如下:
DAO接口类:
package com.google.code.morphia.dao; import java.util.List; import com.google.code.morphia.Datastore; import com.google.code.morphia.Key; import com.google.code.morphia.query.Query; import com.google.code.morphia.query.QueryResults; import com.google.code.morphia.query.UpdateOperations; import com.google.code.morphia.query.UpdateResults; import com.mongodb.DBCollection; import com.mongodb.WriteConcern; import com.mongodb.WriteResult; public interface DAO<T, K> { /** Starts a query for this DAO entities type*/ public Query<T> createQuery(); /** Starts a update-operations def for this DAO entities type*/ public UpdateOperations<T> createUpdateOperations(); /** The type of entities for this DAO*/ public Class<T> getEntityClass(); /** Saves the entity; either inserting or overriding the existing document */ public Key<T> save(T entity); /** Saves the entity; either inserting or overriding the existing document */ public Key<T> save(T entity, WriteConcern wc); /** Updates the first entity matched by the constraints with the modifiers supplied.*/ public UpdateResults<T> updateFirst(Query<T> q, UpdateOperations<T> ops); /** Updates all entities matched by the constraints with the modifiers supplied.*/ public UpdateResults<T> update(Query<T> q, UpdateOperations<T> ops); /** Deletes the entity */ public WriteResult delete(T entity); /** Deletes the entity * @return */ public WriteResult delete(T entity, WriteConcern wc); /** Delete the entity by id value */ public WriteResult deleteById(K id); /** Saves the entities given the query*/ public WriteResult deleteByQuery(Query<T> q); /** Loads the entity by id value*/ public T get(K id); /** Finds the entities Key<T> by the criteria {key:value}*/ public List<T> findIds(String key, Object value); /** Finds the entities Ts*/ public List<T> findIds(); /** Finds the entities Ts by the criteria {key:value}*/ public List<T> findIds(Query<T> q); /** checks for entities which match criteria {key:value}*/ public boolean exists(String key, Object value); /** checks for entities which match the criteria*/ public boolean exists(Query<T> q); /** returns the total count*/ public long count(); /** returns the count which match criteria {key:value}*/ public long count(String key, Object value); /** returns the count which match the criteria*/ public long count(Query<T> q); /** returns the entity which match criteria {key:value}*/ public T findOne(String key, Object value); /** returns the entity which match the criteria */ public T findOne(Query<T> q); /** returns the entities */ public QueryResults<T> find(); /** returns the entities which match the criteria */ public QueryResults<T> find(Query<T> q); /** ensures indexed for this DAO */ public void ensureIndexes(); /** gets the collection */ public DBCollection getCollection(); /** returns the underlying datastore */ public Datastore getDatastore(); }
DAO底层实现类:
package com.google.code.morphia.dao; import java.lang.reflect.ParameterizedType; import java.util.ArrayList; import java.util.List; import com.google.code.morphia.Datastore; import com.google.code.morphia.DatastoreImpl; import com.google.code.morphia.Key; import com.google.code.morphia.Morphia; import com.google.code.morphia.query.Query; import com.google.code.morphia.query.QueryResults; import com.google.code.morphia.query.UpdateOperations; import com.google.code.morphia.query.UpdateResults; import com.mongodb.DBCollection; import com.mongodb.Mongo; import com.mongodb.WriteConcern; import com.mongodb.WriteResult; /** * @author Olafur Gauti Gudmundsson * @author Scott Hernandez */ @SuppressWarnings({ "unchecked", "rawtypes" }) public class BasicDAO<T, K> implements DAO<T, K> { protected Class<T> entityClazz; protected DatastoreImpl ds; public BasicDAO(Class<T> entityClass, Mongo mongo, Morphia morphia, String dbName) { initDS(mongo, morphia, dbName); initType(entityClass); } public BasicDAO(Class<T> entityClass, Datastore ds) { this.ds = (DatastoreImpl) ds; initType(entityClass); } /** * <p> Only calls this from your derived class when you explicitly declare the generic types with concrete classes </p> * <p> * {@code class MyDao extends DAO<MyEntity, String>} * </p> * */ protected BasicDAO(Mongo mongo, Morphia morphia, String dbName) { initDS(mongo, morphia, dbName); initType(((Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0])); } protected BasicDAO(Datastore ds) { this.ds = (DatastoreImpl) ds; initType(((Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0])); } protected void initType(Class<T> type) { this.entityClazz = type; ds.getMapper().addMappedClass(type); } protected void initDS(Mongo mon, Morphia mor, String db) { ds = new DatastoreImpl(mor, mon, db); } /** * Converts from a List<Key> to their id values * * @param keys * @return */ protected List<?> keysToIds(List<Key<T>> keys) { ArrayList ids = new ArrayList(keys.size() * 2); for (Key<T> key : keys) ids.add(key.getId()); return ids; } /** The underlying collection for this DAO */ public DBCollection getCollection() { return ds.getCollection(entityClazz); } /* (non-Javadoc) * @see com.google.code.morphia.DAO#createQuery() */ public Query<T> createQuery() { return ds.createQuery(entityClazz); } /* (non-Javadoc) * @see com.google.code.morphia.DAO#createUpdateOperations() */ public UpdateOperations<T> createUpdateOperations() { return ds.createUpdateOperations(entityClazz); } /* (non-Javadoc) * @see com.google.code.morphia.DAO#getEntityClass() */ public Class<T> getEntityClass() { return entityClazz; } /* (non-Javadoc) * @see com.google.code.morphia.DAO#save(T) */ public Key<T> save(T entity) { return ds.save(entity); } /* (non-Javadoc) * @see com.google.code.morphia.DAO#save(T, com.mongodb.WriteConcern) */ public Key<T> save(T entity, WriteConcern wc) { return ds.save(entity, wc); } /* (non-Javadoc) * @see com.google.code.morphia.DAO#updateFirst(com.google.code.morphia.query.Query, com.google.code.morphia.query.UpdateOperations) */ public UpdateResults<T> updateFirst(Query<T> q, UpdateOperations<T> ops) { return ds.updateFirst(q, ops); } /* (non-Javadoc) * @see com.google.code.morphia.DAO#update(com.google.code.morphia.query.Query, com.google.code.morphia.query.UpdateOperations) */ public UpdateResults<T> update(Query<T> q, UpdateOperations<T> ops) { return ds.update(q, ops); } /* (non-Javadoc) * @see com.google.code.morphia.DAO#delete(T) */ public WriteResult delete(T entity) { return ds.delete(entity); } /* (non-Javadoc) * @see com.google.code.morphia.DAO#delete(T, com.mongodb.WriteConcern) */ public WriteResult delete(T entity, WriteConcern wc) { return ds.delete(entity, wc); } /* (non-Javadoc) * @see com.google.code.morphia.DAO#deleteById(K) */ public WriteResult deleteById(K id) { return ds.delete(entityClazz, id); } /* (non-Javadoc) * @see com.google.code.morphia.DAO#deleteByQuery(com.google.code.morphia.query.Query) */ public WriteResult deleteByQuery(Query<T> q) { return ds.delete(q); } /* (non-Javadoc) * @see com.google.code.morphia.DAO#get(K) */ public T get(K id) { return ds.get(entityClazz, id); } /* (non-Javadoc) * @see com.google.code.morphia.DAO#findIds(java.lang.String, java.lang.Object) */ public List<T> findIds(String key, Object value) { return (List<T>) keysToIds(ds.find(entityClazz, key, value).asKeyList()); } /* (non-Javadoc) * @see com.google.code.morphia.DAO#findIds() */ public List<T> findIds() { return (List<T>) keysToIds(ds.find(entityClazz).asKeyList()); } /* (non-Javadoc) * @see com.google.code.morphia.DAO#findIds(com.google.code.morphia.query.Query) */ public List<T> findIds(Query<T> q) { return (List<T>) keysToIds(q.asKeyList()); } /* (non-Javadoc) * @see com.google.code.morphia.DAO#exists(java.lang.String, java.lang.Object) */ public boolean exists(String key, Object value) { return exists(ds.find(entityClazz, key, value)); } /* (non-Javadoc) * @see com.google.code.morphia.DAO#exists(com.google.code.morphia.query.Query) */ public boolean exists(Query<T> q) { return ds.getCount(q) > 0; } /* (non-Javadoc) * @see com.google.code.morphia.DAO#count() */ public long count() { return ds.getCount(entityClazz); } /* (non-Javadoc) * @see com.google.code.morphia.DAO#count(java.lang.String, java.lang.Object) */ public long count(String key, Object value) { return count(ds.find(entityClazz, key, value)); } /* (non-Javadoc) * @see com.google.code.morphia.DAO#count(com.google.code.morphia.query.Query) */ public long count(Query<T> q) { return ds.getCount(q); } /* (non-Javadoc) * @see com.google.code.morphia.DAO#findOne(java.lang.String, java.lang.Object) */ public T findOne(String key, Object value) { return ds.find(entityClazz, key, value).get(); } /* (non-Javadoc) * @see com.google.code.morphia.DAO#findOne(com.google.code.morphia.query.Query) */ public T findOne(Query<T> q) { return q.get(); } /* (non-Javadoc) * @see com.google.code.morphia.DAO#find() */ public QueryResults<T> find() { return createQuery(); } /* (non-Javadoc) * @see com.google.code.morphia.DAO#find(com.google.code.morphia.query.Query) */ public QueryResults<T> find(Query<T> q) { return q; } /* (non-Javadoc) * @see com.google.code.morphia.DAO#getDatastore() */ public Datastore getDatastore() { return ds; } public void ensureIndexes() { ds.ensureIndexes(entityClazz); } }
使用DAO如下:
package com.easyway.mongodb.morphia; import java.util.List; import com.easyway.mongodb.morphia.basic.Hotel; import com.google.code.morphia.Morphia; import com.google.code.morphia.dao.BasicDAO; import com.google.code.morphia.query.UpdateOperations; import com.mongodb.Mongo; /** * 数据访问层类的使用 * @Title: TODO * @Description: 实现TODO * @Copyright:Copyright (c) 2011 * @Company:易程科技股份有限公司 * @Date:2012-3-2 * @author * @version 1.0 */ public class HotelDAO extends BasicDAO<Hotel, String> { public HotelDAO(Morphia morphia, Mongo mongo, String dbName) { super(mongo, morphia, dbName); } /** * 统计四星级以上酒店数量 * @return */ public long countHotel(){ return count(createQuery().field("stars").greaterThanOrEq(4)); } /** * 查询酒店 * @return */ public List<Hotel> queryHotelPhone(){ return createQuery().field("phoneNumbers").sizeEq(1).asList(); } /** * 查询酒店 * @param hotel */ public List<Hotel> queryHotel(Hotel hotel){ return find(createQuery().filter("stars", 4).order("address.address_street")).asList(); } /** * 修改酒店信息 * @param hotel */ public void batchUpdateHotel(){ UpdateOperations<Hotel> mods = createUpdateOperations().inc("stars", 1); update(createQuery().filter("stars", 4), mods); } }
发表评论
-
mongodb 地理位置处理
2016-05-16 13:39 1387我只记录我用到的部分,没有完整分析mongodb对地理位置 ... -
Redis配置文件redis.conf
2014-11-14 14:10 1839# Redis configuration file ex ... -
Redis高可用部署及监控
2014-11-12 13:25 1075一、 Re ... -
JCS官方文档的简单笔记,仅供自己参考
2014-09-26 20:08 7491. 基本配置 jcs.default=DCjcs.de ... -
JCS基本配置
2014-09-26 19:39 9171、默认的内存缓存 ... -
NoSQL解决方案比较(MongoDB vs Redis, Tokyo Cabinet, and Berkeley DB)
2013-09-30 14:20 1311NoSQL解决方案比较 NoSQL Solution: E ... -
morphia与spring的整合
2012-12-07 15:06 1458转自: http://www.blogjava.net/wat ... -
Mongo的ORM框架的学习Morphia(十五)Morphia+spring整合
2012-12-07 15:06 1627转自:http://topmanopensource.itey ... -
Mongo的ORM框架的学习Morphia(十二) morphia的Query和Update
2012-12-07 15:06 1837转自:http://topmanopensource.itey ... -
Mongo的ORM框架的学习Morphia(十) morphia应用
2012-12-05 14:47 1433转自:http://topmanopensource.itey ... -
Mongo的ORM框架的学习Morphia(九) morphia简单使用
2012-12-05 14:44 1343转自 http://topmanopensource.itey ... -
Mongo的ORM框架的学习Morphia(annotations)
2012-12-05 14:33 2503一:@Entity的使用 @Entity ... -
Instagram的Redis实践(内存占用优化)
2012-11-30 10:43 1170转自:http://blog.nosqlfan.com/htm ... -
SQL 和Mongo 对比图表
2012-11-28 14:54 2180参看官方说明: http://www.mongodb ... -
MongoDB 入门指南、示例
2012-11-23 10:38 827转自:http://www.cnblogs.com/hoojo ... -
mongodb中使用MapReduce
2012-11-23 10:12 1189MapReduce函数的用法如下: db.users.ma ... -
python的redis用法
2012-11-22 15:48 1140#! /usr/bin/env python #coding ... -
Python连接redis
2012-11-22 15:46 5580一、Redis是流行的NOSQL内存数据库,以Key-Valu ... -
【pipeline】【分布式的id生成器】【分布式锁【watch】【multi】】【redis分布式】
2012-08-29 10:42 1345转自 http://www.bwkeji.com/a/wang ... -
利用redis的transaction功能,实现分布式下加锁
2012-08-29 09:57 2346package memcached; import ja ...
相关推荐
Mongo的ORM框架的学习Morphia
mongo-2.7.3.jar和morphia-0.99.jar
spring MVC morphia mongo 整合的例子 网上下载的例子 自己调试保证绝对能运行
mongo 数据库
这个压缩文件包含mongo-2.7.3.jar和morphia-0.99.jar是morphia框架必须的jar
java mongodb orm 实现-基于mongodb-driver开发的ORM
详细介绍 Web项目Mongo数据库连接 内加驱动包 超值下载
spring-mongodb-morphia springdata-mongo morphia mongodb 学习
Mongo数据库的安装,MongoDB学习笔记(一) MongoDB介绍及安装.docx
使用go开发的项目源码、数据集
MongoModel - Ruby ODM用于与MongoDB数据库交互
实现将mongo数据库注册成系统服务同时设置自动启动命令。
没下载积分了,传个文档挣下积分,Mongo数据库和Nginx容器的部署。
metersphere连接mongo数据库所需驱动包
MongoVUE mongo 数据库 工具
总结了mongo数据库的映射框架morphia的基本用法。包括查询,更新等,是一份不错的mongodb操作入门文档。
实现一个luamongo的demo 直接在lua就可以操作mongo数据库
lucid-mongo, 面向adonis框架的Mongodb ODM 清晰 Mongolucid是mongo查询生成器和 ORM 。 它还支持数据库迁移,种子和工厂作为 @adonis/lucid.
mongo数据库
MongoVUE是一个MongoDB在windwos下的客户端管理工具 MongoVUE是一个创新的MongoDB桌面应用Windows操作系统,给你一个优雅的、高度可用的GUI界面来使用MongoDB。现在有一个更少的担心在管理你的网络数据。 MongoVUE使...