(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";
}
}
- 启动应用
- springboot项目的运行三种方式
第一种: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/update