springboot入门程序
(1)設置spring boot的parent
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
?
說明:Spring boot的項目必須要將parent設置為spring boot的parent,該parent包含了大量默認的配置,大大簡化了我們的開發。
(2)導入spring boot的web支持
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
(3)添加Spring boot的插件(暫時可不加)
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
(4)編寫Spring Boot應用主程序
import?org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
?* Spring Boot應用的程序入口
?* Created by Administrator on 2019/8/25 0025.
?*/
@SpringBootApplication
public class DeptApplication {
 ????public static void main(String[] args) {
 ????????SpringApplication.run(DeptApplication.class);
????}
}
?
代碼說明:
1、@SpringBootApplication:Spring Boot項目的核心注解,主要目的是開啟自動配置;
2、@Configuration:這是一個配置Spring的配置類;
3、@Controller:標明這是一個SpringMVC的Controller控制器;
4、main方法:在main方法中啟動一個應用,即:這個應用的入口;
(5)控制器
import?org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
?*控制器
?* Created by Administrator on 2019/8/25 0025.
?*/
@Controller
public class DeptController {
 ????@RequestMapping("/insert")
 ????@ResponseBody
 ????public String insert(){
????????System.out.println("控制器insert()......");
 ????????return "success";
????}
 ????/**由于springboot默認不支持jsp,所以有404*/
????@RequestMapping("/update")
 ????public String update(){
????????System.out.println("控制器update()......");
 ????????return "success";
????}
}
第一種:main方法
第二種:Springboot2.0.0環境下
<!-- maven插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- maven的jdk編譯版本插件 -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
?
第三種:以第二種環境為前提,在項目的pom.xml目錄下執行以下命令
mvn spring-boot:run
(7)測試
打開瀏覽器,輸入地址:
http://localhost:8080/insert
http://localhost:8080/update
總結
以上是生活随笔為你收集整理的springboot入门程序的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: springboot环境搭建及入门必知
 - 下一篇: springboot的核心