您好,登錄后才能下訂單哦!
繼續之前的dailyReport項目,今天的任務是選擇mongogdb作為持久化存儲。
關于nosql和rdbms的對比以及選擇,我參考了不少資料,關鍵一點在于:nosql可以輕易擴展表的列,對于業務快速變化的應用場景非常適合;rdbms則需要安裝關系型數據庫模式對業務進行建模,適合業務場景已經成熟的系統。我目前的這個項目——dailyReport,我暫時沒法確定的是,對于一個report,它的屬性應該有哪些:date、title、content、address、images等等,基于此我選擇mongodb作為該項目的持久化存儲。
修改Pom文件,增加mongodb支持
<dependency> ??<groupId>org.springframework.boot</groupId> ??<artifactId>spring-boot-starter-data-mongodb</artifactId></dependency>
重新設計Report實體類,id屬性是給mongodb用的,用@Id注解修飾;重載toString函數,使用String.format輸出該對象。
import?org.springframework.data.annotation.Id;/** ?*?@author?duqi ?*?@create?2015-11-17?19:31 ?*/public?class?Report?{????@Id ????private?String?id;????private?String?date;????private?String?content;????private?String?title;????public?Report()?{ ????}????public?Report(String?date,?String?title,?String?content)?{????????this.date?=?date;????????this.title?=?title;????????this.content?=?content; ????}????public?String?getId()?{????????return?id; ????}????public?void?setId(String?id)?{????????this.id?=?id; ????}????public?String?getTitle()?{????????return?title; ????}????public?void?setTitle(String?title)?{????????this.title?=?title; ????}????public?String?getDate()?{????????return?date; ????}????public?void?setDate(String?dateStr)?{????????this.date?=?dateStr; ????}????public?String?getContent()?{????????return?content; ????}????public?void?setContent(String?content)?{????????this.content?=?content; ????}????@Override ????public?String?toString()?{????????return?String.format("Report[id=%s,?date='%s',?content='%s',?title='%s']",?id,?date,?content,?title); ????} }
增加ReportRepository接口,它繼承自MongoRepository接口,MongoRepository接口包含了常用的CRUD操作,例如:save、insert、findall等等。我們可以定義自己的查找接口,例如根據report的title搜索,具體的ReportRepository接口代碼如下:
import?org.springframework.data.mongodb.repository.MongoRepository;import?java.util.List;/** ?*?Created?by?duqi?on?15/11/22. ?*/public?interface?ReportRepository?extends?MongoRepository<Report,?String>?{????Report?findByTitle(String?title);????List<Report>?findByDate(String?date); }
修改ReportService代碼,增加createReport函數,該函數根據Conroller傳來的Map參數初始化一個Report對象,并調用ReportRepository將數據save到mongodb中;對于getReportDetails函數,仍然開啟緩存,如果沒有緩存的時候則利用findByTitle接口查詢mongodb數據庫。
import?com.javadu.dailyReport.domain.Report;import?com.javadu.dailyReport.domain.ReportRepository;import?org.springframework.beans.factory.annotation.Autowired;import?org.springframework.cache.annotation.Cacheable;import?org.springframework.stereotype.Service;import?java.util.Map;/** ?*?@author?duqi ?*?@create?2015-11-17?20:05 ?*/@Servicepublic?class?ReportService?{????@Autowired ????private?ReportRepository?repository;????public?Report?createReport(Map<String,?Object>?reportMap)?{ ????????Report?report?=?new?Report(reportMap.get("date").toString(), ????????????????reportMap.get("title").toString(), ????????????????reportMap.get("content").toString()); ????????repository.save(report);????????return?report; ????}????@Cacheable(value?=?"reportcache",?keyGenerator?=?"wiselyKeyGenerator")????public?Report?getReportDetails(String?title)?{ ????????System.out.println("無緩存的時候調用這里---數據庫查詢,?title="?+?title);????????return?repository.findByTitle(title); ????} }
Controller只負責URL到具體Service的映射,而在Service層進行真正的業務邏輯處理,我們這里的業務邏輯異常簡單,因此顯得Service層可有可無,但是如果業務邏輯復雜起來(比方說要通過RPC調用一個異地服務),這些操作都需要再service層完成。總體來講,Controller層只負責:轉發請求 + 構造Response數據;在需要進行權限驗證的時候,也在Controller層利用aop完成。
一般將對于Report(某個實體)的所有操作放在一個Controller中,并用@RestController和@RequestMapping("/report")注解修飾,表示所有xxxx/report開頭的URL會由這個ReportController進行處理。
. POST
對于增加report操作,我們選擇POST方法,并使用@RequestBody修飾POST請求的請求體,也就是createReport函數的參數;
. GET
對于查詢report操作,我們選擇GET方法,URL的形式是:“xxx/report/${report's title}”,使用@PathVariable修飾url輸入的參數,即title。
import?com.javadu.dailyReport.domain.Report;import?com.javadu.dailyReport.service.ReportService;import?org.slf4j.Logger;import?org.slf4j.LoggerFactory;import?org.springframework.beans.factory.annotation.Autowired;import?org.springframework.web.bind.annotation.*;import?java.util.LinkedHashMap;import?java.util.Map;/** ?*?@author?duqi ?*?@create?2015-11-17?20:10 ?*/@RestController@RequestMapping("/report")public?class?ReportController?{????private?static?final?Logger?logger?=?LoggerFactory.getLogger(ReportController.class);????@Autowired ????ReportService?reportService;????@RequestMapping(method?=?RequestMethod.POST)????public?Map<String,?Object>?createReport(@RequestBody?Map<String,?Object>?reportMap)?{ ????????logger.info("createReport"); ????????Report?report?=?reportService.createReport(reportMap); ????????Map<String,?Object>?response?=?new?LinkedHashMap<String,?Object>(); ????????response.put("message",?"Report?created?successfully"); ????????response.put("report",?report);????????return?response; ????}????@RequestMapping(method?=?RequestMethod.GET,?value?=?"/{reportTitle}")????public?Report?getReportDetails(@PathVariable("reportTitle")?String?title)?{ ????????logger.info("getReportDetails");????????return?reportService.getReportDetails(title); ????} }
Update和delete操作我這里就不一一講述了
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。