Home >Java >javaTutorial >What is the method for integrating mongodb with springboot?
As an open source database system, MongoDB is built using C language and uses distributed file storage technology. Under high load conditions, adding more nodes can ensure server performance. The goal of MongoDB is to provide a scalable and high-performance data storage solution to serve web applications. MongoDB uses documents based on key-value pairs to store data structures. MongoDB documents are similar to JSON objects. Field values can contain other documents, arrays, and document arrays.
There are many online installation tutorials, so I won’t say more here.
The configuration here is similar to the jdk configuration, no more details! Check the installation and startup database
In order to save space, many comments and blank lines have been removed, everyone, please forgive me!
<!-- mongodb --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>
#################mongodb################ #spring.data.mongodb.uri=mongodb://username:password@localhost:27017/test spring.data.mongodb.uri=mongodb://localhost:27017/springboot
package com.lengmo.dao; import com.lengmo.entity.Student; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Update; import org.springframework.stereotype.Component; import java.util.List; @Component public class MongoDbDao { @Autowired private MongoTemplate mongoTemplate; public void save(Student s) { mongoTemplate.save(s); } public Student get(Query query) { return mongoTemplate.findOne(query,Student.class); } public List<Student> findAll() { return mongoTemplate.findAll(Student.class); } public void update(Query query, Update update) { mongoTemplate.updateMulti(query,update,Student.class); } public void delete(Query query) { mongoTemplate.remove(query,Student.class); } }
Here you need to be familiar with some common operating methods of mongodb, just use it more and practice more! ! ! !
package com.lengmo.service; import com.lengmo.dao.MongoDbDao; import com.lengmo.entity.Hobbies; import com.lengmo.entity.Student; import org.springframework.beans.factory.annotation.Autowired; 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 org.springframework.stereotype.Service; import java.util.List; @Service public class MongoDbService { @Autowired private MongoDbDao mongoDbDao; public void save() { Student s=new Student(); s.setName("lis"); s.setAge(17); s.setSex(1); s.setHeight(182); Hobbies h=new Hobbies(); h.setHname("swing"); s.setHobbies(h); mongoDbDao.save(s); } public Student get() { //and查询 /* Criteria criteriaName=Criteria.where("name").is("lisi"); Criteria criteriaAage=Criteria.where("age").is(17); Criteria andCriteria = new Criteria(); andCriteria.andOperator(criteriaName,criteriaAage); Query query=new Query(andCriteria);*/ //or查询 Criteria criteriaName=Criteria.where("name").is("lisi"); Criteria criteriaAage=Criteria.where("age").gt(16); Criteria orCriteria = new Criteria(); orCriteria.orOperator(criteriaName,criteriaAage); Query query=new Query(orCriteria); return mongoDbDao.get(query); } public List<Student> findAll() { return mongoDbDao.findAll(); } public void update() { Query query=new Query(Criteria.where("name").is("zhangsan")); Update update=new Update(); update.set("age",30); update.set("height",188); update.set("hobbies.hname","basketball"); mongoDbDao.update(query,update); } public void delete() { Query query=new Query(Criteria.where("name").is("zhangsan")); mongoDbDao.delete(query); } }
package com.lengmo.controller; import com.lengmo.entity.Student; import com.lengmo.service.MongoDbService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController @RequestMapping("/mongodb") public class MongoDbController { @Autowired private MongoDbService mongoDbService; @RequestMapping("/save") public void save(){ mongoDbService.save(); } @RequestMapping("/get") public Student get(){ return mongoDbService.get(); } @RequestMapping("/findAll") public List<Student> findAll(){ return mongoDbService.findAll(); } @RequestMapping("/update") public void update(){ mongoDbService.update(); } @RequestMapping("/delete") public void delete(){ mongoDbService.delete(); } }
package com.lengmo.entity; import lombok.Data; import javax.persistence.Id; @Data //之所以没有像 mysql那样需要@table标签去标注我们的表名与实体类的对应关系。是因为 mongodb默认类名即与表名对应。 public class Student{ @Id private String id; private String name; private Integer age; private Integer sex; private Integer height; private Hobbies hobbies; } package com.lengmo.entity; import lombok.Data; @Data public class Hobbies { private String hname; }
The above is the detailed content of What is the method for integrating mongodb with springboot?. For more information, please follow other related articles on the PHP Chinese website!