1.在idea中,首先创建好project,然后创建Module,选择maven,创建好对应的module后。
2.引入依赖pom.xml中
<!--所以的springboot应用都以该工程为父工程-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
</parent>
<dependencies>
<!--启动器:每一个启动器背后都是一堆的依赖,web启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
练手(http://localhost:8080/hello/show):
@RestController
@RequestMapping("hello")
@EnableAutoConfiguration//启动自动配置
public class HelloController {
@RequestMapping("show")
public String test(){
return "hello springboot 1";
}
public static void main(String[] args) {
SpringApplication.run(HelloController.class,args);
}
}
其中,应该要有一个公用的SpringApplication引导类,即在所有其他包之前:
@EnableAutoConfiguration//启动自动配置
@ComponentScan//类似于<context:component-scan base-package="">组件扫描
public class testApplication {
public static void main(String[] args) {
SpringApplication.run(testApplication.class, args);
}
}
二.JAVA配置
以连接数据库为例: