MongoDB对应Java实体类编写方法

1.文档结构

movie

{
        "_id" : ObjectId("*******2bd93db4516d6040ed"),
        "title" : "羞羞的铁拳",
        "like" : 101,
        "dislike" : 20,
        "author" : [
                "mike",
                "jam"
        ],
        "comment" : [
                {
                        "name" : "mike",
                        "text" : "good movie"
                }
        ]
}

2.POJO

上面的文档包含一个数组和内嵌文档。内嵌文档需要编写新的实体类,可以不用包含ID字段。

Movie.java

//这里对应的数据库集合名为movie,如果不填,默认为小写movie
@Document(collection="movie")
public class Movie {

    @Id
    private String id;

    private String title;
    private Integer like;
    private Integer dislike;
    private String[] author;
    private List<Comment> comment;

    .....

    getter and setter
}

Comment.java

//内嵌文档不用写集合名,加上@Document就行了
@Document
public class Comment {



    private String name;
    private String text;

    ......

    getter and setter


}

猜你喜欢

转载自blog.csdn.net/qq_32475739/article/details/78405336