我正在尝试通过spring框架从mongodb检索数据。
起初我做了返回类型Map<String, Object>
,但我决定更改为User
值对象。
以下是User VO的课程
@Document(collection = "user")
public class User {
@Id
@Field(value="id")
private String id;
@Field(value="name")
private String name;
@Field(value="password")
private String password;
@Field(value="professional")
private String professional;
@Field(value="email")
private String email;
@Field(value="gravatar")
private String gravatar;
@PersistenceConstructor
public User(String id, String name, String password, String professional, String email, String gravatar) {
super();
this.id = id;
this.name = name;
this.password = password;
this.professional = professional;
this.email = email;
this.gravatar = gravatar;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getProfessional() {
return professional;
}
public void setProfessional(String professional) {
this.professional = professional;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getGravatar() {
return gravatar;
}
public void setGravatar(String gravatar) {
this.gravatar = gravatar;
}
};
以及@repository
检索数据
@Repository
public class MongoMemberDao implements CommonDao<String, Map<String, Object>, Exception> {
@Autowired
MongoTemplate template;
final String COLLECTION_NAME = "user";
@SuppressWarnings("unchecked")
@Override
public Map<String, Object> read(String key) throws Exception {
Query findQuery = new Query();
findQuery.addCriteria(Criteria.where("id").is(key));
return template.findOne(findQuery, Map.class, COLLECTION_NAME);
}
public User readByDocument(String id) throws Exception {
Query findOneQuery = new Query();
findOneQuery.addCriteria(Criteria.where("id").is(id));
return template.findOne(findOneQuery, User.class, COLLECTION_NAME);
}
};
read
方法返回正常,但readByDocument
不返回(返回null
而非User
实例)。我看了official document。但我没有得到任何线索。
仅供参考,两者的参数Query
看起来相同。
Query: { "id" : "system"}, Fields: null, Sort: null
readByDocument
会返回null
感谢。
----编辑
关注我的数据库配置
@Configuration
public class MongoConfig extends AbstractMongoConfiguration {
private final String MONGO_URL = "127.0.0.1";
private final Integer MONGO_PORT = 27017;
@Override
protected String getDatabaseName() {
return "tfarm";
}
@Override
// @Bean
public Mongo mongo() throws Exception {
return new MongoClient(MONGO_URL, MONGO_PORT);
}
}
我将此添加到WebApplictaionInitializer
工具。
答案 0 :(得分:1)
目前的解决方案
我在官方网站上找到了
所以我改变了我的VO ......
@Document(collection = "user")
public class User {
@Id
private ObjectId _id;
@Field(value="id")
private String id;
@Field(value="name")
private String name;
@Field(value="password")
private String password;
@Field(value="professional")
private String professional;
@Field(value="email")
private String email;
@Field(value="gravatar")
private String gravatar;
@PersistenceConstructor
public User(String id, String name, String password, String professional, String email, String gravatar) {
super();
this.id = id;
this.name = name;
this.password = password;
this.professional = professional;
this.email = email;
this.gravatar = gravatar;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public ObjectId get_id() {
return _id;
}
public void set_id(ObjectId _id) {
this._id = _id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getProfessional() {
return professional;
}
public void setProfessional(String professional) {
this.professional = professional;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getGravatar() {
return gravatar;
}
public void setGravatar(String gravatar) {
this.gravatar = gravatar;
}
};
添加了ObjectId
。或者,只需删除@Id
注释也可以正常工作。但是
@Id
@Field(value="id")
String id;
不起作用。谢谢你的帮助。