首页 > 数据库技术 > 详细

MongoDB的ORM框架——Morphia

时间:2019-04-02 14:53:49      阅读:178      评论:0      收藏:0      [点我收藏+]

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() + "]");
        }

  注意:分页查询可用:.offset(  (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

 

MongoDB的ORM框架——Morphia

原文:https://www.cnblogs.com/vettel0329/p/10638123.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!