我正在使用Sprind Data MongoDb,MongoTemplate和MongoOperations将数据保存到MongoDB中。
一切工作正常,没有错误或异常,但是仍然没有将数据插入数据库。我正在使用MongoOperations的save方法保存数据。
这是我的pom.xml的相关部分
<properties>
<spring.version>4.2.8.RELEASE</spring.version>
<springsecurity.version>3.2.0.RELEASE</springsecurity.version>
<log4j.version>1.2.17</log4j.version>
<commons.fileupload.version>1.3.1</commons.fileupload.version>
<commons.io.version>2.4</commons.io.version>
<springsocial.version>1.1.0.RELEASE</springsocial.version>
</properties>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.9.4.RELEASE</version>
</dependency>
这是我的DBOPerations类
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import com.bng.monitor.config.MongoConfig;
import com.bng.monitor.model.User;
//import org.springframework.context.support.GenericXmlApplicationContext;
public class DbOperations {
public static void main(String[] args) {
// For XML
//ApplicationContext ctx = new GenericXmlApplicationContext("SpringConfig.xml");
// For Annotation
ApplicationContext ctx =
new AnnotationConfigApplicationContext(MongoConfig.class);
MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate");
User user = new User("kv", "password123");
// save
mongoOperation.save(user);
// now user object got the created id.
System.out.println("1. user : " + user);
// query to search user
Query searchUserQuery = new Query(Criteria.where("username").is("kv"));
// find the saved user again.
User savedUser = mongoOperation.findOne(searchUserQuery, User.class);
System.out.println("2. find - savedUser : " + savedUser);
// update password
mongoOperation.updateFirst(searchUserQuery,
Update.update("password", "new password"),User.class);
// find the updated user object
User updatedUser = mongoOperation.findOne(searchUserQuery, User.class);
System.out.println("3. updatedUser : " + updatedUser);
// delete
mongoOperation.remove(searchUserQuery, User.class);
// List, it should be empty now.
List<User> listUser = mongoOperation.findAll(User.class);
System.out.println("4. Number of user = " + listUser.size());
}
}
这是我的模特班
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "users")
public class User {
@Id
private String id;
private String username;
private String password;
public User(String username, String password){
super();
this.username = username;
this.password = password;
}
public void setId(String id){
this.id = id;
}
public String getId(){
return this.id;
}
public void setUsername(String username){
this.username = username;
}
public String getUsername(){
return this.username;
}
public void sePassword(String password){
this.password = password;
}
public String gePassword(){
return this.password;
}
}
这是我运行DBOperations类时的输出
367 [main] INFO org.springframework.context.annotation.AnnotationConfigApplicationContext-刷新org.springframework.context.annotation.AnnotationConfigApplicationContext@5d6f64b1:启动日期[IST 2016年11月17日星期四];上下文层次结构的根
馆藏存在吗? :错误
1.用户:com.bng.monitor.model.User@342c38f8
2.查找-savedUser:空
3.updatedUser:null
4.用户数= 0
有人可以告诉我这是怎么回事吗?
最佳答案
问题出在User.java上。
实体类的构造函数需要使用@PersistenceConstructor进行注释。
@PersistenceConstructor用于标记从Mongo Server提取数据时用于创建实体的构造函数。
User.java中指定的集合名称错误:@Document(collection = "users")
因此,当我将集合名称更改为“用户”时,一切正常。
@Document(collection = "user")