第十一章 springboot + mongodb(簡單查詢)


1、mongodb在mac上的安裝

  • 下載mongodb,https://www.mongodb.org/
  • 解壓縮到一個指定文件夾,如:/Users/enniu1/Desktop/zjg/mongodb-osx-x86_64-3.2.6(這是我的mongodb的版本)
  • 配置PATH
    • 輸入命令:"vi ~/.bash_profile"
    • 添加如下兩句配置:
      1 export MONGO_HOME=/Users/enniu1/Desktop/zjg/mongodb-osx-x86_64-3.2.6
      2 export PATH=$PATH:$MONGO_HOME/bin
      View Code
  • 創建數據目錄
    • 輸入命令:"sudo mkdir -p /data/db"
  • 賦予數據目錄權限
    • 輸入命令:"sudo chmod 777 /data/db"
  • 啟動
    • 輸入命令:"mongod"
  • 退出:Ctrl+c

注意兩個錯:

參考:https://docs.mongodb.org/manual/tutorial/install-mongodb-on-os-x/

 

2、代碼(4個部分)

2.1、com.xxx.firstboot.domain.Customer

 1 package com.xxx.firstboot.domain;
 2 
 3 import org.springframework.data.annotation.Id;
 4 
 5 /**
 6  * 測試mongodb
 7  */
 8 public class Customer {
 9     /**
10      * cid:該字段用於mongodb的"_id"索引
11      * 1、需要@Id注解
12      * 2、取名無所謂,反正在mongodb中最后都會轉化為"_id"
13      * 3、定義為String類型,如果定義為Integer可能索引只會是0,會出現key重復導致數據庫插不進去的情況;
14      * 4、該類型也是MongoRepository泛型中主鍵的ID
15      */
16     @Id
17     private String cid;
18     private String firstname;
19     private String secondname;
20 
21     public String getCid() {
22         return cid;
23     }
24 
25     public void setCid(String cid) {
26         this.cid = cid;
27     }
28 
29     public String getFirstname() {
30         return firstname;
31     }
32 
33     public void setFirstname(String firstname) {
34         this.firstname = firstname;
35     }
36 
37     public String getSecondname() {
38         return secondname;
39     }
40 
41     public void setSecondname(String secondname) {
42         this.secondname = secondname;
43     }
44 
45 }
View Code

說明:生成的colletion(類似於MySQL中的表)就是domain類的簡單類名,eg.customer。

注意:

  • cid:該字段用於mongodb的"_id"索引
  • 需要@Id注解
  • 取名無所謂,反正在mongodb中最后都會轉化為"_id"
  • 定義為String類型,如果定義為Integer可能索引只會是0,會出現key重復導致數據庫插不進去的情況
  • 該類型也是MongoRepository泛型中主鍵的ID 

2.2、com.xxx.firstboot.mongo.CustomerRepository

 1 package com.xxx.firstboot.mongo;
 2 
 3 import java.util.List;
 4 
 5 import org.springframework.data.mongodb.repository.MongoRepository;
 6 
 7 import com.xxx.firstboot.domain.Customer;
 8 
 9 /**
10  * MongoRepository<Customer, Integer>
11  * 第一個參數:T 操作的vo
12  * 第二個參數:ID T的主鍵類型
13  * 作用:該接口實現了CRUD方法
14  * 
15  * 注意:
16  * 1、由於boot使用了spring-data-mongodb,所以我們不需要寫該接口的實現,
17  *   當我們運行程序的時候,spring-data-mongodb會動態創建
18  * 2、findBySecondname命名是有講究的,Secondname(是Customer的屬性)若改為lastname就會報找不到屬性lastname的錯誤
19  */
20 public interface CustomerRepository extends MongoRepository<Customer, String> {
21     public Customer findByFirstname(String firstname);
22     public List<Customer> findBySecondname(String secondname);
23 }
View Code

說明:該接口就是我們的業務接口。

注意:

  • 繼承MongoRepository<T, ID>接口
    • T:操作的domain,例如com.xxx.firstboot.domain.Customer
    • ID:T的主鍵類型(@ID修飾的屬性),通常就是String
    • 該接口的實現類也實現了CRUD操作
  • 我們的接口只需要定義方法的定義,不需要做實現,spring-data-mongodb會在程序運行的時候動態創建
    • 方法的命名是有講究的,與domain的屬性有關(可以再測測) 

2.3、com.xxx.firstboot.web.CustomerController

 1 package com.xxx.firstboot.web;
 2 
 3 import java.util.List;
 4 
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.web.bind.annotation.RequestMapping;
 7 import org.springframework.web.bind.annotation.RequestMethod;
 8 import org.springframework.web.bind.annotation.RequestParam;
 9 import org.springframework.web.bind.annotation.RestController;
10 
11 import com.xxx.firstboot.domain.Customer;
12 import com.xxx.firstboot.mongo.CustomerRepository;
13 
14 import io.swagger.annotations.Api;
15 import io.swagger.annotations.ApiOperation;
16 
17 @RestController
18 @RequestMapping("/customer")
19 @Api("customer相關的API,用於測試mongodb")
20 public class CustomerController {
21 
22     @Autowired
23     private CustomerRepository customerRepository;
24 
25     @ApiOperation("增加一個Customer")
26     @RequestMapping(value = "/addCustomer", method = RequestMethod.GET)
27     public Customer addCustomer(@RequestParam("firstname") String firstname,
28                                 @RequestParam("secondname") String secondname) {
29         Customer customer = new Customer();
30         customer.setFirstname(firstname);
31         customer.setSecondname(secondname);
32         return customerRepository.save(customer);
33     }
34 
35     @ApiOperation("獲取所有的Customer")
36     @RequestMapping(value = "/getAllCustomer", method = RequestMethod.GET)
37     public List<Customer> getAllCustomer() {
38         return customerRepository.findAll();
39     }
40 
41     @ApiOperation("根據firstname獲取Customer")
42     @RequestMapping(value = "/getCustomerByFirstname", method = RequestMethod.GET)
43     public Customer getCustomerByFirstname(@RequestParam("firstname") String firstname) {
44         return customerRepository.findByFirstname(firstname);
45     }
46 
47     @ApiOperation("根據secondname獲取多個Customer")
48     @RequestMapping(value = "/getCustomerBySecondname", method = RequestMethod.GET)
49     public List<Customer> getCustomerBySecondname(@RequestParam("secondname") String secondname) {
50         return customerRepository.findBySecondname(secondname);
51     }
52 
53     @ApiOperation("根據id刪除Customer")
54     @RequestMapping(value = "/deleteCustomerById", method = RequestMethod.GET)
55     public boolean deleteCustomerById(@RequestParam("cid") String cid) {
56         customerRepository.delete(cid);
57         return true;
58     }
59 }
View Code

說明:直接注入我們自己的業務接口,然后進行相應的操作即可。

此時,就可以進行測試了。只是此時使用的都是mongodb的默認信息。

  • host:localhost
  • port:27017
  • 數據庫:test
  • collection:customer(domain類的簡單類名)

2.4、application.properties

1 #mongodb note:mongo3.x will not use host and port,only use uri
2 spring.data.mongodb.host=192.168.21.54
3 spring.data.mongodb.port=27017
4 spring.data.mongodb.uri=mongodb://192.168.21.54:27017/myfirstMongodb
View Code

說明:如果需要指定host、port、數據庫,需要在application.properties文件中配置以上信息。

注意:

  • 配置必須以"spring.data.mongodb"為前綴
  • 如果是mongo3.x的話,host和port沒用,需要uri。(未測過)
  • uri = mongodb://host:port/數據庫
  • mongo2.x支持以上兩種配置方式
  • mongo3.x僅支持uri方式

 

3、測試

啟動應用,啟動mongo服務進程,打開swagger,使用robomongo或者mongobooster客戶端觀察mongodb存儲情況。

沒有在application.properties中設置屬性。

設置屬性后,

 

參考:

https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-data-mongodb

https://spring.io/guides/gs/accessing-data-mongodb/ 其中的例子就是對sample代碼的解釋

http://www.jianshu.com/p/e59cd2dc5274 關於mongodb主鍵

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-nosql.html 關於mongo2.x與3.x對host、port、uri配置的支持。

http://blog.didispace.com/springbootmongodb/ 主鍵為Long uri用戶名、密碼配置


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM