[英]Data from mongodb when using Spring boot
I am using Spring boot with spring data. 我正在将Spring Boot与Spring数据一起使用。 I have a mongodb and has the following record inside it:
我有一个mongodb,里面有以下记录:
{ "_id" : ObjectId("5914281fae0fe519943367c7"), "_class" : "example.model.Customer", "username" : "test", "password" : "test", "message" : "test" }
I am trying to create a function in which it retrieves data from the record using their username and password. 我正在尝试创建一个函数,在其中使用用户名和密码从记录中检索数据。 This is what I have so far:
这是我到目前为止的内容:
public class findExistingUser {
@Autowired
MongoTemplate mongoTemplate;
public Customer findUser(String username, String password){
Query query = new Query();
query.addCriteria(Criteria.where("username").is(username).and("password").is(password));
return mongoTemplate.findOne(query, Customer.class, "customer");
}
}
But for some reason I get a null pointer exception. 但是由于某种原因,我得到了一个空指针异常。 When I debug, mongotemplate seems to be null.
当我调试时,mongotemplate似乎为空。
Below properties should be available in application.properties 以下属性应该在application.properties中可用
spring.data.mongodb.host=localhost spring.data.mongodb.port=27017 spring.data.mongodb.database=app1
And below dependecies should be available in pom 并且下面的依赖应该在pom中可用
org.springframework.data:spring-data-mongodb:1.10.0.RELEASE org.mongodb:mongodb-driver:3.4.1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.