1.引入pom
<dependency> <groupId>org.mongodb.morphia</groupId> <artifactId>morphia</artifactId> <version>1.3.2</version> </dependency>
2.創建Entity類
@Entity() public class Commodity { @Id private ObjectId id; @Indexed private String cmdtyCode; private String cmdtyName; @Embedded private List<Attribute> attributes; @Reference private CommodityInfo commodityInfo; public ObjectId getId() { return id; } public void setId(ObjectId id) { this.id = id; } public String getCmdtyCode() { return cmdtyCode; } public void setCmdtyCode(String cmdtyCode) { this.cmdtyCode = cmdtyCode; } public String getCmdtyName() { return cmdtyName; } public void setCmdtyName(String cmdtyName) { this.cmdtyName = cmdtyName; } public List<Attribute> getAttributes() { return attributes; } public void setAttributes(List<Attribute> attributes) { this.attributes = attributes; } public CommodityInfo getCommodityInfo() { return commodityInfo; } public void setCommodityInfo(CommodityInfo commodityInfo) { this.commodityInfo = commodityInfo; } }
@Entity() public class CommodityInfo { @Id private ObjectId id; private String color; private String style; public String getColor() { return color; } public void setColor(String color) { this.color = color; } public String getStyle() { return style; } public void setStyle(String style) { this.style = style; } public ObjectId getId() { return id; } public void setId(ObjectId id) { this.id = id; } }
public class Attribute { private String key; private String value; public String getKey() { return key; } public void setKey(String key) { this.key = key; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } }
注意: @Entity:聲明該類作為文檔將持久保存。在默認情況下,Morphia使用類名稱來命名集合
@Embedded:成員對象將被視為嵌入的(embedded)。它會顯示為集合中父文檔的子集
@Reference:說明對象是對另外一個集合中的文檔的引用
@Indexed:表明為此屬性增加索引
3.通過Datastore使用
a.創建Datastore對象
Morphia morphia = new Morphia(); morphia.mapPackage("com.wode.entity"); // entity所在包路徑 MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost:27017")); Datastore datastore = morphia.createDatastore(mongoClient, "morphia");
b.添加
//先添加@Reference引用的對象 CommodityInfo cmdtyInfo = new CommodityInfo(); cmdtyInfo.setColor("silver"); cmdtyInfo.setStyle("12"); datastore.save(cmdtyInfo); //再添加商品 Commodity cmdty = new Commodity(); cmdty.setCommodityInfo(cmdtyInfo); cmdty.setCmdtyCode("Ag"); cmdty.setCmdtyName("銀"); List<Attribute> attributes = new ArrayList<>(); Attribute attribute = new Attribute(); attribute.setKey("品質"); attribute.setValue("優"); attributes.add(attribute); cmdty.setAttributes(attributes); datastore.save(cmdty);
c.修改
Query<Commodity> query = datastore.createQuery(Commodity.class).filter("cmdtyCode = ", "Ag"); UpdateOperations<Commodity> updateOperations = datastore.createUpdateOperations(Commodity.class).set("cmdtyName", "銀00").set("cmdtyCode", "Ag00"); UpdateResults result = datastore.update(query, updateOperations); System.out.println(result.getUpdatedCount());
d.刪除
Query<Commodity> query = datastore.createQuery(Commodity.class).filter("cmdtyCode = ", "Ag00"); datastore.delete(query);
e.查詢
List<String> list = new ArrayList<>(); list.add("Ag11"); list.add("Ag12"); List<Commodity> resultList = datastore.createQuery(Commodity.class).filter("cmdtyCode in ", list).order("-cmdtyCode").asList(); for(Commodity cmdty : resultList){ System.out.println("cmdtyCode[" + cmdty.getCmdtyCode() + "], cmdtyName[" + cmdty.getCmdtyName() + "]"); }
注意:分頁查詢可用:query.offset( (pageIndex - 1) * pageSize ).limit( pageSize )
也可:query.asList( new FindOptions().skip( (pageIndex - 1) * pageSize ).limit( pageSize ) )
4.通過BasicDAO使用
a.創建BasicDAO的實現類
public class CmdtyDAO extends BasicDAO<Commodity, ObjectId> { public CmdtyDAO(MongoClient mongoClient, Morphia morphia, String dbName){ super(mongoClient, morphia, dbName); } }
b.初始化DAO
Morphia morphia = new Morphia(); morphia.mapPackage("com.wode.entity"); // entity所在包路徑 MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost:27017")); CmdtyDAO dao = new CmdtyDAO(mongoClient, morphia, "morphia");
c.使用基本同Datastore
//查單個 Query<Commodity> query = dao.createQuery().filter("cmdtyCode = ", "Ag11"); Commodity cmdty = dao.findOne(query); System.out.println("cmdtyCode[" + cmdty.getCmdtyCode() + "], cmdtyName[" + cmdty.getCmdtyName() + "]"); //查多個 List<String> list = new ArrayList<>(); list.add("Ag11"); list.add("Ag12"); query = dao.createQuery().filter("cmdtyCode in ", list).order("-cmdtyCode"); FindOptions options = new FindOptions(); options.skip(1).limit(1); //分頁 List<Commodity> resultList = dao.find(query).asList(options); for(Commodity tempCmdty : resultList){ System.out.println("cmdtyCode[" + tempCmdty.getCmdtyCode() + "], cmdtyName[" + tempCmdty.getCmdtyName() + "]"); }
5.復合查詢
a.且(and)和或(or)復合查詢
query.or( query.criteria("cmdtyCode").equal("Ag0"), query.and( query.criteria("cmdtyName").in(name), query.criteria("num").in(num) ), query.criteria("cmdtyCode").equal("Ag6") );
b.兩字段比較——where
query.or( new WhereCriteria("this.num >= this.num2"), new WhereCriteria("this.num == 3"), query.criteria("num").equal(1) );