Morphia is a lightweight type-safe library for mapping Java objects to/from MongoDB
:
Morphia是一个安全轻量级的为
MongoDB设计的java持久化架构。
1.要使用Morphia,需要依赖下面JARs:
MongoDB java driver
Morphia Release (根据driver版本选择不同版本)
Optional (but needed if you use Maven)
CGLib
ProxyToys
如果项目中用到maven,需要在pom.xml中加入
<dependency>
<groupId>com.google.code.morphia</groupId>
<artifactId>morphia</artifactId>
<version>###</version>
</dependency>
<!-Optional Jars (for certain features) but required by maven for bulding. -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>[2.1_3,)</version>
<type>jar</type>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.thoughtworks.proxytoys</groupId>
<artifactId>proxytoys</artifactId>
<version>1.0</version>
<type>jar</type>
<optional>true</optional>
</dependency>
2.基于spring test 的morphia 基本操作
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:spring-beans-test.xml"})
public class MorphiaDaoTest {
@Resource
ResourceMethodDao resourceMethodDao;
@Resource
ParameterDefinitionDao parameterDefinitionDao;
@Before
public void checkNull(){
Assert.notNull(resourceMethodDao);
}
public void addObject(){
ResourceMethod method = new ResourceMethod();
method.setName("产品获取");
method.setJsBody("javascript:void(0)");
method.setCreator("liu");
method.setId(4);
method.setCreatedTime(new Date());
List list = getResouceDefinition();
method.setParameters(list);
Key<ResourceMethod> id = resourceMethodDao.save(method);
Assert.notNull(id);
}
public void query(){
Query<ResourceMethod> query = resourceMethodDao.createQuery();
//query.field("creator").equal("liu");
ParameterDefinition object = new ParameterDefinition();
object.setId(1);
query.field("parameters").hasThisElement(object);
// Query<ResourceMethod> query = resourceMethodDao.createQuery().filter("id >", 0).order("-id");//查询id大于0的,
//并按id降序排列
List list = resourceMethodDao.find(query).asList();
System.out.println(list);
}
public void delete(){
//ResourceMethod res = new ResourceMethod();
Query<ResourceMethod> query = resourceMethodDao.createQuery();
query.field("creator").equal("liu");
resourceMethodDao.deleteByQuery(query);
//resourceMethodDao.deleteById(id)
//resourceMethodDao.delete(res);//实体中至少要含有id
}
@Test
public void update(){
Query<ResourceMethod> query = resourceMethodDao.createQuery();
query.field("creator").equal("alan");
UpdateOperations<ResourceMethod> ops = resourceMethodDao.createUpdateOperations()
.set("creator", "long").inc("ResourceType", 12);
resourceMethodDao.update(query,ops);
//resourceMethodDao.updateFirst(query, ops);
}
private List<ParameterDefinition> getResouceDefinition() {
List<ParameterDefinition> list = new ArrayList<ParameterDefinition>();
ParameterDefinition para = new ParameterDefinition();
para.setId(1);
para.setName("name");
para.setType(FieldType.STRING);
parameterDefinitionDao.save(para);
list.add(para);
ParameterDefinition para1 = new ParameterDefinition();
para1.setId(2);
para1.setName("value");
para1.setType(FieldType.STRING);
parameterDefinitionDao.save(para1);//这里是@Reference方式引用,所以插入前必须保证字表中存在引用数据
list.add(para1);
return list;
}
DAO集成BasicDAO
public class ResourceMethodDao extends BasicDAO<ResourceMethod,Integer> {
protected ResourceMethodDao(Datastore ds) {
super(ds);
}
}
实体
@Entity
public class ResourceMethod implements Serializable {
private static final long serialVersionUID = -2260371987627421226L;
@Id
int id;
/** 方法名称 */
String name;
/** 资源类型 */
int ResourceType;
/** 返回数据结构类型 */
DataStructure returnStructure;
/** 是否系统方法 */
boolean system;
/** js实现的方法提, system=false时有效*/
String jsBody;
/** 创建日期 */
Date createdTime;
/** 创建人 */
String creator;
/** 更新日期 */
Date updatedTime;
/** 更新人 */
String updator;
/** 方法的参数列表 */
@Reference
List<ParameterDefinition> parameters;
......
}
@Entity
public class ParameterDefinition implements Serializable {
@Id
Integer id;
/** 参数名 */
String name;
/** 参数类型 */
FieldType type;
......
}
spring-beans-test.xml的内容
<?xml version="1.0" encoding="GBK"?>
<beans xmlns=" http://www.springframework.org/schema/beans"
xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="mongo" class="com.mongodb.Mongo">
<constructor-arg index="0" value="219.239.*.*"/>
<constructor-arg index="1" value="27017"/>
</bean>
<bean id="morphia" class="com.tmg.rescenter.core.dao.MorphiaBean">
<property name="mongo" ref="mongo"/>
<property name="databaseName" value="alan_test"/>
</bean>
<bean id="datastore" factory-bean="morphia" factory-method="getDatastore"/>
<!--下面是所有业务类dao bean定义-->
<bean id="parameterDefinitionDao" class="com.tmg.rescenter.core.dao.ParameterDefinitionDao">
<constructor-arg ref="datastore"/>
</bean>
<bean id="resourceMethodDao" class="com.tmg.rescenter.core.dao.ResourceMethodDao">
<constructor-arg ref="datastore"/>
</bean>
</beans>
3,注意点
@Embedded
在一个对象中引用别的对象,但是不会建立引用表,也不建立外关联,有冗余,但不用考虑主表和参照表的一致性,关联实体上标示@Embedded
@Reference在一个对象中引用别的对象,首先需要建立引用表数据,会通过id建立外关联,考虑主表和参照表的一致性,关联实体上标示@Entity如上
query.field("creator").equal("liu");和
query.filter("creator =","liu");是一样的效果
其他参看上面代码,或者登陆http://code.google.com/p/morphia/查看使用细节或源代码