zl程序教程

您现在的位置是:首页 >  其他

当前栏目

深入实践Spring Boot2.3.2 文档建模

2023-03-14 10:14:57 时间

2.3.2 文档建模

MongoDB是文档型数据库,使用MongoDB也可以像使用关系型数据库那样为文档建模。如代码清单2-15所示,为用户文档建模,它具有用户名、密码、用户名称、邮箱和注册日期等字段,有一个用来保存用户角色的数据集,还定义了一个构造函数,可以很方便地用来创建一个用户实例。

代码清单2-15 用户文档建模

@Document(collection = "user")

public class User {

    @Id

    private String userId;

    @NotNull @Indexed(unique = true)

    private String username;

    @NotNull

    private String password;

    @NotNull

    private String name;

    @NotNull

    private String email;

    @NotNull

    private Date registrationDate = new Date();

    private Set<String> roles = new HashSet<>();

 

    public User() { }

 

    @PersistenceConstructor

    public User(String userId, String username, String password, String name, String email,

            Date registrationDate, Set<String> roles) {

        this.userId = userId;

        this.username = username;

        this.password = password;

        this.name = name;

        this.email = email;

        this.registrationDate = registrationDate;

        this.roles = roles;

 

    }

……