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整合BasicDAOpublic 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/查看使用細節或原始碼