一、SpringBoot的主要優點:
1.快速整合第三方框架,無需配置文件。幾乎可以是零配置的開箱即用(out-of-the-box)。開發者能夠更加專注於業務邏輯
2.內置http容器(Tomcat、Jetty),最終以java應用程序進行執行
二、實現原理:
1.Maven依賴傳遞

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
2.SpringBoot的Web組件,默認集成的是SpringMVC框架。SpringMVC是控制層。
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController // @EnableAutoConfiguration 作用 開啟自動裝備(默認是true) public class IndexController { // 1.在微服務情況,基本上都在類上加上@RestController 目的?返回json格式。
//2.@RestController注解相當於@ResponseBody + @Controller合在一起的作用。如果只是使用@RestController注解Controller,則Controller中的方法無法返回jsp頁面,或者html,配置的視圖解析器 InternalResourceViewResolver不起作用,返回的內容就是Return 里的內容。使用傳統方式返回json //@RestController 修飾的類下的所有方法,全部都是返回josn格式,這樣的話不用在方法上加上@ResponseBody @RequestMapping("/index1") public String index() throws InterruptedException { return "v6.0"; } // // 思考:如何啟動? 使用main啟動 // public static void main(String[] args) { // // 告訴SpringBoot 程序入口 默認端口號是8080 // SpringApplication.run(IndexController.class, args); // } }
3.內置Tomcat tomcat = new Tomcat() 對象

@RestController @EnableAutoConfiguration public class HelloController { @RequestMapping("/hello") public String index() { return "Hello World"; } public static void main(String[] args) { SpringApplication.run(HelloController.class, args); } }
三、StringMVC
@Controller:表示這個類是SpringMVC 里的Controller,DispatchServlet會自動掃面這個類。效果:用這個注解,通過return的String類型的數據,視圖解析器可以解析jsp,html頁面,並且跳轉到相應頁面。目的就是為了能訪問,所以是必須的。
@ResponseBody :可作用於類或者方法,表示該方法的返回結果直接寫入 HTTP response body 中,適用於返回JSON,XML。無法返回jsp頁面,或者html。一般通過Ajax程序來獲取數據。一般只寫在方法上面,因為這個注解是為了,區別同一個類,不同的方法到底是返回頁面還是返回數據!
@RestController: 表示 Controller+ ResponseBody(類上只用ResponseBody是不能被訪問的,必須要有Controller),同樣不能返回jsp和html。如果是只用於返回json的類,一般用這種。
@RequestMapping(“/url”):作用在 Controller的方法,表示映射地址
四、實踐
資源訪問
Springboot 的工作目錄resources下面有兩個文件夾,static 和 templates。
1.Static 文件夾下面放靜態資源
可以直接訪問(默認頁面index.html)可以用瀏覽器直接訪問,localhost:81默認頁面 為 static/index.html。
http://localhost:81/images/aa.jpg http://localhost:81/aa.html
2.templates 文件夾下面放動態頁面(使用Freemarker模板引擎渲染web視圖)

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
加入thymeleaf依賴,就可以通過java代碼轉發到目錄下面的頁面。
Java代碼如下:
@Controller //表示轉發到頁面 public class TTController { @RequestMapping("/test") public String test() { return "test";//訪問的是templates下面的test.html } }
下面是動態頁面演示:
java代碼
@Controller public class TextController { @Autowired private TextServiceImpl textServiceImpl; @RequestMapping("/text") public String text(Model model,HttpServletRequest request){ List<Text> textList = textServiceImpl.findAll(); model.addAttribute("textList",textList); return "text"; } @RequestMapping("/insert") public String insert(String content,Model model,HttpServletRequest request){ Text text = new Text(); text.setContent(content); Integer i = textServiceImpl.insert(text); if(i==1){ System.out.println("插入成功"); } List<Text> textList = textServiceImpl.findAll(); model.addAttribute("textList",textList); return "text"; } }
@Service //自動注冊到Spring容器,不需要再在applicationContext.xml配置 public class TextServiceImpl implements TextService{ @Autowired private TextMapper textMapper; public List<Text> findAll(){ return textMapper.findAll(); } @Transactional public Integer insert(Text text) { int insertResult = textMapper.insert(text.getContent()); System.out.println("insertResult="+insertResult); return insertResult; } }
public interface TextService { public Integer insert(Text text); public List<Text> findAll(); }
@Repository //用於標注數據訪問組件 public interface TextMapper { @Select("SELECT * FROM TEXT") List<Text> findAll(); /* @Select("SELECT * FROM USER WHERE NAME = #{name}") User findByName(@Param("name") String name); */ @Insert("INSERT INTO TEXT (content) VALUE(#{content})") int insert(@Param("content") String content); }
src\main\resources\templates\text.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>文本顯示</title> <script type="text/javascript"></script> <body > <form action="/insert"> 文字:<input type="text" name="content"/> <input type="submit" value="提交"> </form> <thead> <tr th:each="text:${textList}"> <td th:text="${text.content}"></td> </tr> </thead> </body> </html>
main方法
//@EnableAutoConfiguration 作用在於讓 Spring Boot 根據應用所聲明的依賴來對 Spring 框架進行自動配置 //@ComponentScan(basePackages = "wull.tool.*") 控制器掃包范圍 @SpringBootApplication //這個表示上面兩行 //@ComponentScan("/") @MapperScan("wull.*") public class ToolApplication { public static void main(String[] args) { SpringApplication.run(ToolApplication.class, args); System.out.println("啟動成功" ); } }