1. 程式人生 > >Spring Boot下MongoDB實戰

Spring Boot下MongoDB實戰

一 安裝MongoDB

1 Docker安裝

安裝完成後,執行下面命令:

docker run -d -p 27017:27017 mongo

2 VirtualBox下做一次埠對映

3 下載MongoDB資料庫管理軟體Robomongo

下載地址

二 新建Spring Boot專案

新增依賴

<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>
</dependencies>

三 新建領域模型

1 Person原始碼

package com.wisely.ch8_6_1.domain;

import java.util.Collection;
import java.util.LinkedHashSet;

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;
    //此屬性在文件中的名稱為locs,locations屬性將以陣列形式存在當前資料記錄中
    @Field("locs")
    private Collection<Location> locations =  new LinkedHashSet<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;
    }
    

}

2 Location原始碼

package com.wisely.ch8_6_1.domain;

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;
    }
}

四 資料訪問

package com.wisely.ch8_6_1.dao;

import java.util.List;

import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;

import com.wisely.ch8_6_1.domain.Person;

public interface PersonRepository extends MongoRepository<Person, String> {
    //支援方法名查詢
    Person findByName(String name);
    
    //支援@Query查詢,查詢引數構造JSON字串即可
    @Query("{'age': ?0}")
    List<Person> withQueryFindByAge(Integer age);

}

五 控制器

package com.wisely.ch8_6_1.web;

import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.wisely.ch8_6_1.dao.PersonRepository;
import com.wisely.ch8_6_1.domain.Location;
import com.wisely.ch8_6_1.domain.Person;

@RestController
public class DataController {
    
    @Autowired
    PersonRepository personRepository;
    //測試儲存資料
    @RequestMapping("/save")
    public Person save(){
        Person  p = new Person("wyf",32);
        Collection<Location> locations =  new LinkedHashSet<Location>();
        Location loc1 = new Location("上海","2009");
        Location loc2 = new Location("合肥","2010");
        Location loc3 = new Location("廣州","2011");
        Location loc4 = new Location("馬鞍山","2012");
        locations.add(loc1);
        locations.add(loc2);
        locations.add(loc3);
        locations.add(loc4);
        p.setLocations(locations);
        
        return personRepository.save(p);
    }
    //測試方法名查詢
    @RequestMapping("/q1")
    public Person q1(String name){
        return personRepository.findByName(name);
    }
    //測試@Query查詢
    @RequestMapping("/q2")
    public List<Person> q2(Integer age){
        return personRepository.withQueryFindByAge(age);
    }

}

六 主類

@SpringBootApplication
public class Ch861Application {

    public static void main(String[] args) {
        SpringApplication.run(Ch861Application.class, args);
    }
}
@Configuration
//啟動MongoDB的支援
@EnableMongoRepositories
class AppConfig{

}

七 測試

1 測試儲存資料

頁面顯示結果如下:

2 測試方法名查詢

頁面顯示結果如下:

3 測試@Query查詢

頁面顯示結果如下: