springboot 整合 mongodb
阿新 • • 發佈:2018-12-03
pom.xml:
<!--mongodb-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
application.properties:
#連線mongodb spring.data.mongodb.uri=mongodb://127.0.0.1:27017/mydb spring.data.mongodb.username=root spring.data.mongodb.password=123456
實體類:
package com.example.demo.entity; import org.springframework.data.mongodb.core.mapping.Document; /** * @author FastKing * @version 1.0 * @date 2018/11/5 15:57 **/ @Document(collection="test") public class Test { private String id; private String username; private String password; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
Controller:
@RestController
@RequestMapping("user")
public class UserController {
@Autowired
private MongoTemplate mongoTemplate;
@GetMapping("list")
public Object list(){
return mongoTemplate.findAll(Test.class);
}
}