SpringBoot---MongoDB
1、概述
1.1、SpringBoot对MongoDB的支持 ,位于 org.springframework.boot.autoconfigure.mongo;
主要 配置 数据库连接、MongoTemplate;
可以使用 “spring.data.mongodb” 为前缀的属性 来 配置MongoDB相关的信息;
1.2、SpringBoot 为我们提供了一些默认属性,如 :端口27017、服务器localhost、默认数据库为test...
1.3、SpringBoot 为我们 开启了对Repository的支持(自动配置了@EnableMongoRepositories);
2、使用
1.1、使用MongoDB 只需要引入spring-boot-starter-data-mongodb依赖,无需任何配置;
1.2、案例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | package com.an.mongo.entity; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.mapping.Field; import java.util.LinkedList; import java.util.List; /** * @description: * @author: anpeiyong * @date: Created in 2019/12/2 18:38 * @since: */ //@Document 映射数据模型 与 MongoDB的文档 @Document public class Person { //@Id 表示这个属性为文档的id @Id private String id; private String name; private Integer age; //@Field 表示此属性 为文档中的名称 ,loc属性将以数组形式 存在 当前数据记录中 @Field (value = "loc" ) private List<Location> locationList= new LinkedList<Location>(); public Person(String id,String name,Integer age){ this .id=id; this .name=name; this .age=age; } public String getId() { return id; } public void setId(String id) { this .id = id; } public String getName() { return name; } public void setName(String name) { this .name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this .age = age; } public List<Location> getLocationList() { return locationList; } public void setLocationList(List<Location> locationList) { this .locationList = locationList; } @Override public String toString() { return "id:" +id+ "name:" +name+ "age:" +age; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | package com.an.mongo.entity; /** * @description: * @author: anpeiyong * @date: Created in 2019/12/2 18:44 * @since: */ public class Location { private String place; private String year; public Location(String place,String year){ this .place=place; this .year=year; } public String getPlace() { return place; } public void setPlace(String place) { this .place = place; } public String getYear() { return year; } public void setYear(String year) { this .year = year; } @Override public String toString() { return "place:" +place+ "year:" +year; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | package com.an.mongo.controller; import com.an.mongo.dao.PersonRepository; import com.an.mongo.entity.Location; import com.an.mongo.entity.Person; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.LinkedList; import java.util.List; /** * @description: * @author: anpeiyong * @date: Created in 2019/12/2 19:00 * @since: */ @RestController public class MongoController { @Autowired private PersonRepository personRepository; @RequestMapping (value = "/save" ) public Person save(){ Person person= new Person( "2" , "an" , 18 ); List<Location> locationList= new LinkedList<Location>(); Location location1= new Location( "beijing" , "1999" ); Location location2= new Location( "nanjing" , "1998" ); Location location3= new Location( "hangzhou" , "1997" ); Location location4= new Location( "suzhou" , "1996" ); Location location5= new Location( "shanxi" , "1995" ); locationList.add(location1); locationList.add(location2); locationList.add(location3); locationList.add(location4); locationList.add(location5); person.setLocationList(locationList); return personRepository.save(person); } @RequestMapping (value = "/getByName/{name}" ) public Person getByName( @PathVariable (value = "name" ) String name){ Person person=personRepository.getByName(name); System.out.println(person); return person; } @RequestMapping (value = "/getByAge/{age}" ) public List<Person> getByAge( @PathVariable (value = "age" ) Integer age){ List<Person> personList=personRepository.withQueryFindByAge(age); System.out.println(personList); return personList; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package com.an.mongo.dao; import com.an.mongo.entity.Person; import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.data.mongodb.repository.Query; import java.util.List; /** * @description: * @author: anpeiyong * @date: Created in 2019/12/2 18:58 * @since: */ public interface PersonRepository extends MongoRepository<Person,String> { Person getByName(String name); //@Query 查询参数构造JSON字符串 @Query (value = "{'age':?0}" ) List<Person> withQueryFindByAge(Integer age); } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)