Mongodb是一个无架构的文档数据库,但在spring数据中,有必要定义实体类和存储库类,如下所示:
实体类:
@Document(collection = "users")
public class User implements UserDetails {
@Id private String userId;
@NotNull @Indexed(unique = true) private String username;
@NotNull private String password;
@NotNull private String name;
@NotNull private String email;
}
Run Code Online (Sandbox Code Playgroud)
存储库类:
public interface UserRepository extends MongoRepository<User, String> {
User findByUsername(String username);
}
Run Code Online (Sandbox Code Playgroud)
无论如何在spring数据mongodb中使用map not class,以便服务器可以接受任何动态JSON数据,然后将其存储在BSON中而不需要任何预先定义的类?