spring boot后臺接口與MongoDB實現(xiàn)插入和獲取leetcode試題

很少寫后臺接口,近來spring boot比較火,而吸引我的一大特性是spring boot的部署非常方便:通過application.properties進(jìn)行配置,使用mvn package生成嵌套了tomcat容器的jar包,再放到任意一臺安裝了java環(huán)境的服務(wù)器,以java -jar xxx.jar啟動。非常小巧、靈活,與近幾年業(yè)界常提到微服務(wù)(包括docker)結(jié)合的很好。
故此,以快速學(xué)習(xí)的目的,搭建了spring boot后臺簡單接口,并通過MongoDB存取數(shù)據(jù)。
該Demo基于MongoDB 3.0.6,IDEA2016.2版本(基于Marven構(gòu)建spring boot項目),thymeleaf(基于html,spring boot提供的模板之一)構(gòu)建,實現(xiàn)功能很簡單,

下面理清主要點(diǎn)

一、spring boot配置

網(wǎng)上很多都沒有給出application.properties的位置和創(chuàng)建方式,右擊Resources目錄new->file即可,文件名就是application.properties.這么重要的文件有必要說明一下,因為大多數(shù)配置都是在這里面進(jìn)行。


application.properties位置所在

我因為只配置了端口、mongodb和thymeleaf,故配置如下。
<pre><code>

  • server.port=8000
  • debug=true
  • spring.http.encoding.charset=UTF-8
  • #mongodb配置

  • spring.data.mongodb.port=27017
  • spring.data.mongodb.host=XX.XX.XX.XX
  • spring.data.mongodb.repositories.enabled=true
  • spring.data.mongodb.database=LeetCode

########################################################

THYMELEAF (ThymeleafAutoConfiguration)

########################################################

  • spring.thymeleaf.prefix=classpath:/templates/
  • spring.thymeleaf.suffix=.html
  • spring.thymeleaf.content-type=text/html
  • spring.thymeleaf.cache=false

</code></pre>
還可配置mysql、redis等眾多服務(wù)。

除了application.properties要配置,pom的依賴也要配置。
下面的配置包含了spring boot、mongodb、thymeleaf這三種配置,引入后選擇auto-import系統(tǒng)自動導(dǎo)入相應(yīng)的依賴文件。
<pre><code>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongo-java-driver</artifactId>
        <version>3.0.4</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-mongodb</artifactId>
        <version>1.7.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-commons</artifactId>
        <version>1.10.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure</artifactId>
        <version>1.4.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
</dependencies>

</code></pre>

二、mongodb操作

使用MongoRepository,存入對象。

}```
另外需要題目model類,定義變量和get/set方法,不再贅述。

在主Controller中,初始化操作mongodb的對象。
@Autowired
private DemoInfoRepository demoInfoRepository;
一開始給頁面展現(xiàn)什么數(shù)據(jù)?返回leetcode.html。
@RequestMapping(value="/leetcode", method= RequestMethod.GET)
public String sayHelloForm(Model model) {
    model.addAttribute("helloMessage", new Demo());
    return "leetcode";
}
運(yùn)行類中執(zhí)行以下操作存入對象數(shù)據(jù),將頁面提交數(shù)據(jù)字段一一綁定,存入數(shù)據(jù)庫。
    ```@RequestMapping(value="/leetcode",method=RequestMethod.POST)
    public String submit(
            @RequestParam("name") String inputtext,
            @RequestParam("id") long result,
            @RequestParam("tag") String tag,
            @RequestParam("acceptance") String acceptance,
            @RequestParam("solution") String solution,
            @RequestParam("content") String content,
            @RequestParam("difficulty") String difficulty){
        Demo demoInfo = new Demo();
        demoInfo.setName(inputtext);
        demoInfo.setId(result);
        demoInfo.setTag(tag);
        demoInfo.setAcceptance(acceptance);
        demoInfo.setSolution(solution);
        demoInfo.setDifficulty(difficulty);
        demoInfo.setContent(content);
        demoInfoRepository.save(demoInfo);
        return "message";}

//執(zhí)行完上述操作后,返回message.html.

查詢

@RequestMapping("/find")
public List<Demo> find(){
    return demoInfoRepository.findAll();//直接調(diào)用findAll()方法。
}

三、thymeleaf前臺頁面與后臺數(shù)據(jù)綁定

    <p>題目名稱: <input type="text" th:field="*{name}" />題目序號: <input type="text" th:field="*{id}"/></p>
    <p>題目通過率: <input type="text" th:field="*{acceptance}" />難度: <input type="text" th:field="*{difficulty}" /></p>
    <p>分類: <input type="text" th:field="*{tag}" /></p>
    <p>題目內(nèi)容: <input type="text" th:field="*{content}" th:width="200" th:height="300"/></p>
    <p>解決方案: <input type="text" th:field="*{solution}" /></p>
    <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>

參考文章
spring boot可以作為app后端接口嗎
Spring Boot實戰(zhàn)之Rest接口開發(fā)及數(shù)據(jù)庫基本操作
SpringBoot 之 簡單的接口
spring boot 連接mongodb
MongoProperties mongodb3.0以后安全配置
84. Spring Boot集成MongoDB【從零開始學(xué)Spring Boot】
spring-boot--使用thymeleaf模板
使用Spring Boot開發(fā)Web項目
深入學(xué)習(xí)spring-boot系列(三)--使用thymeleaf模板
https://github.com/Terry-Shi/blog/wiki/Spring-Boot-thymeleaf

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容