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、案例
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; } }
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; } }
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; } }
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); }