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 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;
}
}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使用,了解springcloud架构可以加求求:三五三六二四七二五九
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)
);