转自 http://topmanopensource.iteye.com/blog/1439448
package com.easyway.mongodb.morphia.basic; import java.net.UnknownHostException; import com.google.code.morphia.Datastore; import com.google.code.morphia.Morphia; import com.google.code.morphia.query.Query; import com.google.code.morphia.query.UpdateOperations; import com.mongodb.DB; import com.mongodb.Mongo; import com.mongodb.MongoException; /** * * @Title: TODO * @Description: 实现TODO * @Copyright:Copyright (c) 2011 * @Company:易程科技股份有限公司 * @Date:2012-3-2 * @author * @version 1.0 */ public class MorphiaHotelApp { // main // public static void main(String[] args) { try { Mongo m = new Mongo("localhost"); DB db = m.getDB("morphia_test"); Morphia morphia = new Morphia(); //设置映射累的类 morphia.map(Hotel.class) .map(Address.class) .map(TwiceKey.class) .map(RecursiveChild.class) .map(RecursiveParent.class); //创建一个Datastore对象 Datastore ds = morphia.createDatastore(m, "morphia_test"); Hotel hotel =Hotel.create(); hotel.setName("My Hotel123"); // hotel.setId(new ObjectId("4ea510c8b24d395248f1f97f")); hotel.setStars(90); Address address = new Address(); address.setStreet("123 Some street"); address.setCity("Some city"); address.setPostCode("123 456"); address.setCountry("2Some country"); // set address hotel.setAddress(address); hotel.getPhoneNumbers().add(new PhoneNumber(86,21,PhoneNumber.Type.PHONE)); hotel.getPhoneNumbers().add(new PhoneNumber(80,2,PhoneNumber.Type.FAX)); //保存 ds.save(hotel); //查询信息 Query q = ds.createQuery(Hotel.class).disableValidation() .disableValidation(); System.out.println(q.filter("id =", "4eb79c8cba4d913746120ae9").asList()); //根据条件删除 ds.delete(q); ds.save(hotel); //条件修改 UpdateOperations<Hotel> ops = ds.createUpdateOperations( Hotel.class).set("name", "New Name1"); ds.update(q, ops); RecursiveChild child=new RecursiveChild(); child.setName("longgangbai"); child.setPayforType(PayforType.network); ds.save(child); RecursiveChild tchild=ds.createQuery(RecursiveChild.class).filter("name =", "longgangbai").get(); RecursiveParent temp=new RecursiveParent(); temp.setChild(child); temp.setPayforType(PayforType.cash); ds.save(temp); TwiceKey key=new TwiceKey(); ds.save(key); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MongoException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
备注:
采用的Morphia的版本为morphia-0.99.1-SNAPSHOT.jar,mongo驱动版本为mongo-java-driver-2.7.0.jar。
本文写一个简单的实例:
package com.easyway.mongodb.morphia.basic; import com.google.code.morphia.annotations.Embedded; /** * * @Title: TODO * @Description: 实现TODO * @Copyright:Copyright (c) 2011 * @Company:易程科技股份有限公司 * @Date:2012-3-2 * @author * @version 1.0 */ @Embedded public class Address { private String street; private String city; private String postCode; private String country; private String state; private String zipcode; public String getState() { return state; } public void setState(String state) { this.state = state; } public String getZipcode() { return zipcode; } public void setZipcode(String zipcode) { this.zipcode = zipcode; } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getPostCode() { return postCode; } public void setPostCode(String postCode) { this.postCode = postCode; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } }
package com.easyway.mongodb.morphia.basic; import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.Vector; import org.bson.types.ObjectId; import com.google.code.morphia.annotations.Embedded; import com.google.code.morphia.annotations.Entity; import com.google.code.morphia.annotations.Id; import com.google.code.morphia.annotations.Transient; /** * 利用 Morphia 可以使用Mongo DB,集成到项目 的DAO *里面。最小成本地使用Nosql技术,满足实际的项目需求。 * * @Description: * @Copyright:Copyright (c) 2011 * @Company:易程科技股份有限公司 * @Date:2012-2-28 * @author * @version 1.0 */ @Entity(value="hotels",noClassnameStored=true) public class Hotel { private static final long serialVersionUID = 1L; public static Hotel create() { return new Hotel(); } public enum Type { BUSINESS, LEISURE } private String name; private Date startDate; private int stars; private boolean takesCreditCards; private Type type; private Set<String> tags; @Transient private String temp; @Embedded private Address address; @Embedded(concreteClass = java.util.Vector.class) private List<PhoneNumber> phoneNumbers; private Hotel() { super(); tags = new HashSet<String>(); phoneNumbers = new Vector<PhoneNumber>(); } /** The id for this instance */ @Id protected String id = new ObjectId().toString(); public String getId() { return id; } public void setId(String id) { this.id = id; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getStars() { return stars; } public void setStars(int stars) { this.stars = stars; } public Date getStartDate() { return startDate; } public void setStartDate(Date startDate) { this.startDate = startDate; } public boolean isTakesCreditCards() { return takesCreditCards; } public void setTakesCreditCards(boolean takesCreditCards) { this.takesCreditCards = takesCreditCards; } public Type getType() { return type; } public void setType(Type type) { this.type = type; } public Set<String> getTags() { return tags; } public void setTags(Set<String> tags) { this.tags = tags; } public List<PhoneNumber> getPhoneNumbers() { return phoneNumbers; } public void setPhoneNumbers(List<PhoneNumber> phoneNumbers) { this.phoneNumbers = phoneNumbers; } public String getTemp() { return temp; } public void setTemp(String temp) { this.temp = temp; } }
package com.easyway.mongodb.morphia.basic; public enum PayforType { network(0,"网络支付"),cash(1,"现金支付"); private int code; private String desc; private PayforType(int code,String name){ this.code=code; this.desc=name; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } }
package com.easyway.mongodb.morphia.basic; import com.google.code.morphia.annotations.Embedded; import com.google.code.morphia.annotations.Property; /** * Morphia支持枚举值的使用 * * @Description: * @Copyright:Copyright (c) 2011 * @Company:易程科技股份有限公司 * @Date:2012-3-2 * @author * @version 1.0 */ @Embedded public class PhoneNumber { public enum Type { PHONE, FAX } @Property private int countryCode; @Property private int localExtension; @Property private Type type; public PhoneNumber() { this.type = Type.PHONE; } public PhoneNumber( int countryCode, int localExtension, Type type ) { this.countryCode = countryCode; this.localExtension = localExtension; this.type = type; } public int getCountryCode() { return countryCode; } public void setCountryCode(int countryCode) { this.countryCode = countryCode; } public int getLocalExtension() { return localExtension; } public void setLocalExtension(int localExtension) { this.localExtension = localExtension; } public Type getType() { return type; } public void setType(Type type) { this.type = type; } @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final PhoneNumber other = (PhoneNumber) obj; if (this.countryCode != other.countryCode) { return false; } if (this.localExtension != other.localExtension) { return false; } if (this.type != other.type) { return false; } return true; } @Override public int hashCode() { int hash = 5; hash = 43 * hash + this.countryCode; hash = 43 * hash + this.localExtension; hash = 43 * hash + this.type.hashCode(); return hash; } }
package com.easyway.mongodb.morphia.basic; import org.bson.types.ObjectId; import com.google.code.morphia.annotations.Entity; import com.google.code.morphia.annotations.Id; import com.google.code.morphia.annotations.Reference; @Entity public class RecursiveChild { private static final long serialVersionUID = 1L; /** The id for this instance */ @Id protected String id = new ObjectId().toString(); private PayforType payforType; private String name; @Reference private RecursiveParent parent; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public PayforType getPayforType() { return payforType; } public void setPayforType(PayforType payforType) { this.payforType = payforType; }
package com.easyway.mongodb.morphia.basic; import org.bson.types.ObjectId; import com.google.code.morphia.annotations.Entity; import com.google.code.morphia.annotations.Id; import com.google.code.morphia.annotations.Reference; @Entity public class RecursiveParent { private static final long serialVersionUID = 1L; private PayforType payforType; @Reference private RecursiveChild child; /** The id for this instance */ @Id protected String id = new ObjectId().toString(); public String getId() { return id; } public PayforType getPayforType() { return payforType; } public void setPayforType(PayforType payforType) { this.payforType = payforType; } public void setId(String id) { this.id = id; } public RecursiveParent() { super(); } public RecursiveChild getChild() { return child; } public void setChild(RecursiveChild child) { this.child = child; } }