SpringBoot結合MongoDB簡單應用
阿新 • • 發佈:2018-12-24
1.docker安裝MongoDB
本案例在windows環境下安裝。
命令:
docker pull mongodb
2.執行docker容器
docker run -d -p 27017:27017 mongo
開啟virtualBox進行埠對映。如圖所示:
3. 下載安裝mongoDB資料庫管理軟體
Robomongo
4.搭建springBoot專案
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wangh</groupId>
<artifactId>springboot_mongodb</artifactId>
<version >0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot_mongodb</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId> spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
5.新建實體類(領域模型)
package com.wangh.springboot_mongodb.model;
import java.util.Collection;
import java.util.LinkedList;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
@Document //對映領域模型和mongodb文件
public class Person {
@Id //此屬性為文件Id
private String id;
private String name;
private Integer age;
@Field("locs") //此屬性在文件中的名稱為locs,locations屬性將以陣列形式存在當前資料記錄中
private Collection<Location> locations = new LinkedList<Location>();
public Person(String name, Integer age) {
super();
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 Collection<Location> getLocations() {
return locations;
}
public void setLocations(Collection<Location> locations) {
this.locations = locations;
}
}
package com.wangh.springboot_mongodb.model;
/**
* 工作過的地方
* @author Wanghao
*
*/
public class Location {
private String place;
private String year;
public Location(String place, String year) {
super();
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;
}
}
6. 資料訪問介面
package com.wangh.springboot_mongodb.repository;
import java.util.List;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;
import com.wangh.springboot_mongodb.model.Person;
public interface PersonRepository extends MongoRepository<Person, String> {
//1.支援方法名查詢
Person findByName(String name);
//2.支援@Query查詢,查詢引數構造JSON字串即可
@Query("{'age' : ?0}")
List<Person> withQueryFindByAge(Integer age);
}
7.控制器
package com.wangh.springboot_mongodb.controller;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wangh.springboot_mongodb.model.Location;
import com.wangh.springboot_mongodb.model.Person;
import com.wangh.springboot_mongodb.repository.PersonRepository;
@RestController
public class PersonController {
@Resource
private PersonRepository personRepository;
@RequestMapping("/save")
public List<Person> save(){
Person p1 = new Person("wanghao", 27);
Collection<Location> locations1 = new LinkedList<Location>();
Location l1 = new Location("北京", "2017");
Location l2 = new Location("上海", "2015");
Location l3 = new Location("廣州", "2010");
Location l4 = new Location("西安", "2008");
locations1.add(l1);
locations1.add(l2);
locations1.add(l3);
locations1.add(l4);
p1.setLocations(locations1);
Person p2 = new Person("wangzhen", 27);
Collection<Location> locations2 = new LinkedList<Location>();
locations2.add(l1);
locations2.add(l2);
locations2.add(l3);
locations2.add(l4);
p2.setLocations(locations2);
List<Person> ps = new ArrayList<Person>();
ps.add(p1);
ps.add(p2);
return personRepository.save(ps);
}
@RequestMapping("/all")
public List<Person> list(){
return personRepository.findAll();
}
@RequestMapping("/q1")
public Person q1(String name){
return personRepository.findByName(name);
}
@RequestMapping("/q2")
public List<Person> q2(Integer age){
return personRepository.withQueryFindByAge(age);
}
}