添加依赖:
<!-- Mongodb 整合 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
application.properties
#mongodb
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=mydb
牛刀小试:
//springboot 已经整合了mongodb 直接注入即可
@Autowired
private MongoTemplate mongoTemplate;
@RequestMapping("mongo")
@ResponseBody
public String mongoTest(){
User user = new User();
user.setId(9);
user.setBrithday(new Date());
user.setGender("man");
user.setCreateTime(new Date());
user.setAge(18);
user.setNickname("司马达");
Map<Integer,User> map = new HashMap<>();
map.put(1,user );
map.put(2,user);
mongoTemplate.save(user);
mongoTemplate.save(map);
return "saver mongodb is OK";
}
@RequestMapping("mongofind")
@ResponseBody
public List<User> findList(){
List<User> all = mongoTemplate.findAll(User.class);
User byId = mongoTemplate.findById(9, User.class);
List<Map> all1 = mongoTemplate.findAll(Map.class);
LOGGER.info("查询mongodb 数据库得到{}:{}",byId,all1);
return all;
}