作为一个开源数据库系统,MongoDB使用C++语言来构建,采用分布式文件存储技术。在高负载的情况下,添加更多的节点,可以保证服务器性能。MongoDB的目标是提供一个能够可扩展高性能的数据存储解决方案,为Web应用程序服务。MongoDB使用基于键值对的文档来存储数据结构。MongoDB 文档类似于 JSON 对象。字段值可以包含其他文档,数组及文档数组。
网上安装的教程也比较多,我在这里就不再多说了。
这里的配置类似于jdk的配置,不再多说!查看安装和启动数据库
为了节省篇幅,好多注释和空行都去掉了,大家见谅!
<!-- 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); } }
这里要熟悉mongodb的一些常规操作方法,多用多练习即可!!!!
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; }
以上是springboot整合mongodb的方法是什么的详细内容。更多信息请关注PHP中文网其他相关文章!