spring boot 實踐

2、實踐

一些說明:html

項目IDE採用Intellij(主要緣由在於Intellij顏值完爆Eclipse,誰叫這是一個看臉的時代)java

工程依賴管理採用我的比較熟悉的Maven(事實上SpringBoot與Groovy纔是天生一對)mysql

1.預覽:

(1)github地址git

 

https://github.com/djmpink/springboot-mybatisgithub

 

git :  https://github.com/djmpink/springboot-mybatis.gitweb

 

(2)完整項目結構spring

 

 

 

 

(3)數據庫sql

數據庫名:test mongodb

【user.sql】shell

2.Maven配置

完整的【pom.xml】配置以下:

 

 

3.主函數

 

【Application.java】包含main函數,像普通java程序啓動便可。

此外,該類中還包含和數據庫相關的DataSource,SqlSeesion配置內容。

注:@MapperScan(「cn.no7player.mapper」) 表示Mybatis的映射路徑(package路徑)

 

4.Controller

 

請求入口Controller部分提供三種接口樣例:視圖模板,Json,restful風格

(1)視圖模板

返回結果爲視圖文件路徑。視圖相關文件默認放置在路徑 resource/templates下:

(2)Json

返回Json格式數據,多用於Ajax請求。

(3)restful

REST 指的是一組架構約束條件和原則。知足這些約束條件和原則的應用程序或設計就是 RESTful。

此外,有一款RESTFUL接口的文檔在線自動生成+功能測試功能軟件——Swagger UI,具體配置過程可移步《Spring Boot 利用 Swagger 實現restful測試》

 

5.Mybatis

 

配置相關代碼在Application.java中體現。

(1)【application.properties】

注意,在Application.java代碼中,配置DataSource時的註解

@ConfigurationProperties(prefix=「spring.datasource」) 

表示將根據前綴「spring.datasource」從application.properties中匹配相關屬性值。

(2)【UserMapper.xml】

Mybatis的sql映射文件。Mybatis一樣支持註解方式,在此不予舉例了。

(3)接口UserMapper

 

3、總結

 

(1)運行 Application.java

(2)控制檯輸出:

 

 

…..(略過無數內容)

 

(3)訪問:

針對三種控制器的訪問分別爲:

視圖:

http://localhost:8080/hello?name=7player

 

Json:

http://localhost:8080/getUserInfo

 

 

Restful(使用了swagger):

http://localhost:8080/swagger/index.html

 

 

 

4、參閱

《Spring Boot – Quick Start》

http://projects.spring.io/spring-boot/#quick-start

《mybatis》

http://mybatis.github.io/mybatis-3/

《使用 Spring Boot 快速構建 Spring 框架應用》

http://www.ibm.com/developerworks/cn/java/j-lo-spring-boot/

《Using @ConfigurationProperties in Spring Boot》

http://www.javacodegeeks.com/2014/09/using-configurationproperties-in-spring-boot.html?utm_source=tuicool

《Springboot-Mybatis-Mysample》

https://github.com/mizukyf/springboot-mybatis-mysample

《Serving Web Content with Spring MVC》

http://spring.io/guides/gs/serving-web-content/

《理解RESTful架構》

http://www.ruanyifeng.com/blog/2011/09/restful

 

附錄:

Spring Boot 推薦的基礎 POM 文件

名稱

說明

spring-boot-starter

核心 POM,包含自動配置支持、日誌庫和對 YAML 配置文件的支持。

spring-boot-starter-amqp

經過 spring-rabbit 支持 AMQP。

spring-boot-starter-aop

包含 spring-aop 和 AspectJ 來支持面向切面編程(AOP)。

spring-boot-starter-batch

支持 Spring Batch,包含 HSQLDB。

spring-boot-starter-data-jpa

包含 spring-data-jpa、spring-orm 和 Hibernate 來支持 JPA。

spring-boot-starter-data-mongodb 

包含 spring-data-mongodb 來支持 MongoDB。

spring-boot-starter-data-rest

經過 spring-data-rest-webmvc 支持以 REST 方式暴露 Spring Data 倉庫。

spring-boot-starter-jdbc

支持使用 JDBC 訪問數據庫。

spring-boot-starter-security

包含 spring-security。

spring-boot-starter-test

包含經常使用的測試所需的依賴,如 JUnit、Hamcrest、Mockito 和 spring-test 等。 

spring-boot-starter-velocity

支持使用 Velocity 做爲模板引擎。

spring-boot-starter-web

支持 Web 應用開發,包含 Tomcat 和 spring-mvc。

spring-boot-starter-websocket

支持使用 Tomcat 開發 WebSocket 應用。

spring-boot-starter-ws

支持 Spring Web Services。

spring-boot-starter-actuator

添加適用於生產環境的功能,如性能指標和監測等功能。

spring-boot-starter-remote-shell

添加遠程 SSH 支持。

spring-boot-starter-jetty

使用 Jetty 而不是默認的 Tomcat 做爲應用服務器。

spring-boot-starter-log4j

添加 Log4j 的支持。

spring-boot-starter-logging

使用 Spring Boot 默認的日誌框架 Logback。

spring-boot-starter-tomcat

使用 Spring Boot 默認的 Tomcat 做爲應用服務器。

 

 

 轉自:http://7player.cn/2015/08/30/%E3%80%90%E5%8E%9F%E5%88%9B%E3%80%91%E5%9F%BA%E4%BA%8Espringboot-mybatis%E5%AE%9E%E7%8E%B0springmvc-web%E9%A1%B9%E7%9B%AE/

 

MVCMyBatisspringSpringBoot

相關文章
相關標籤/搜索