一、 maven創建SpringBoot項目
注意: jdk必須1.8
Finish 完成創建
二、項目應用
1.springboot的項目結構
2.springboot的常規開發
由於springboot先天性集成了spring/springmvc,所以我們不需要編寫相關配置文件,直接開發即可。
三、springboot的核心配置文件
Springboot的核心配置文件主要是來修改一些基本配置(服務器端口號、項目訪問名字)以及集成配置(數據源、mybatis、redis等等)
Springboot的核心配置文件分為兩種形式:(注意:名字都必須為application)
1.application.properties
2.application.yml
3.自定義springboot配置
Springboot的application.properties/application.yml 文件還可以配置一些自定義屬性,用來給對象屬性賦值,方便代碼維護,以及解耦合
應用場景:springMvc文件上傳,定義全局文件夾路徑。
1、在application.yml中添加自定義屬性配置
方式一:通過@Value(“${upload.imagePath}”)
方式二:定義通用映射解析
2、在application.properties中添加自定義屬性配置
1.application.properties配置文件中定義自定義屬性
com.sam.name=sam
com.sam.age=11
com.sam.desc=magical sam
2、編寫Bean類,加載屬性
Sam類需要添加@Component注解,讓spring在啟動的時候掃描到該類,並添加到spring容器中。
第一種:使用spring支持的@Value()加載
package com.sam.demo.conf; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; /** * @author sam * @since 2017/7/15 */ @Component public class Sam { //獲取application.properties的屬性 @Value("${com.sam.name}") private String name; @Value("${com.sam.age}") private int age; @Value("${com.sam.desc}") private String desc; //getter & setter }
第二種:使用@ConfigurationProperties(prefix="") 設置前綴,屬性上不需要添加注解。
package com.sam.demo.conf; import org.springframework.stereotype.Component; /** * @author sam * @since 2017/7/15 */ @Component @ConfigurationProperties(prefix = "com.sam") public class Sam { private String name; private int age; private String desc; //getter & setter }
3、在controller中注入並使用Sam這個Bean。
package com.sam.demo.controller; import com.sam.demo.conf.Sam; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author sam * @since 2017/7/14 */ @RestController public class IndexController { @Autowired private Sam sam; @RequestMapping("/index") public String index() { System.out.println(sam.getName() + " " + sam.getAge() + " " + sam.getDesc()); return "index"; } }
四、springboot的環境配置
在實際開發過程中,不同的開發環境,我們使用的配置信息也是不一樣的,這關系到連接池配置、工廠配置等等
例如:
生產環境(線上環境)
開發環境(線下環境)
測試環境(單元測試)
不同的環境我們要指定不同的配置文件,相對springboot給我們提供了這樣的便利。
注意:也可以在application.properties總配置文件中引入application-dev.properties(開發)或application-prod.properties(生產)或application-test.properties(測試) 達到動態切換環境的目的
application.properties:
五、springboot整合Mybatis
1.導入相關依賴jar
<!--mybatis 依賴--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.10</version> </dependency> <!-- 分頁插件 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.3</version> </dependency> <!--連接池配置--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.31</version> </dependency> <!--log4j--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
2.在配置文件中加入連接池以及mybatis的相關配置
3.Demo測試
六、springboot整合Redis
1.導入依賴
2.在application.yml中添加redis配置
3.自定義redis的key/value 序列化配置,否則redis中存放的key/value是二進制格式,根據key刪除時,找不到指定key。注:這里使用的是RedisTemplate。
4.Redis的開發使用
七、springboot集成shiro
1.引入相關依賴
2.准備shiro的配置文件
1.手動封裝攔截策略
2.直接引入xml配置文件
在啟動時 在啟動類上通過@ImportResource(“classpath:shiro-config.xml”),初始化shiro配置
八、springboot整合JSP
1.引入相關依賴
2.手動創建webapp 靜態資源文件夾
3.在application.yml 中引入視圖解析配置
4.啟動測試
注意: 如果在訪問jsp的過程中 發生下載現象
- 排除jar的依賴問題
- 將jar中<scope> 設置為 compile
SpringBoot不推薦使用JSP作為View,而是推薦我們使用模板(如:thymeleaf、freemarker等模板引擎),原因如下:
1. JSP性能較差
2. 絕對的前后端分離思想,JSP並不利於頁面調試(運行依賴於web容器)
3. SpringBoot對內嵌web容器的支持默認也是用tomcat。但tomcat對web資源的處理上寫死了要使用文件目錄,對於打包成jar包的SpringBoot
應用來說,顯然不行,也有的人打包成war,然后還是部署到tomcat上,這樣違反了SpringBoot的初衷,這樣一來,等於否定了嵌入式容
器,而且程序員還要處理嵌入式環境和外部tomcat環境的不同帶來的問題。