1.创建第一个springboot项目
环境:jdk1.8,idea2018
1.1 创建工程的时候选择Spring Initializr
1.2 填写项目的基本信息
1.3 选择项目中需要的模块(web、sql、aop等),在创建工程的时候向导会联网自动将这些模块依赖的jar包引入,一定要联网
1.4 完成创建Finsh
第一次创建可能会慢,maven建议改成阿里镜像,修改maven安装位置下的config的setting.xml文件
<mirrors>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
生成的主程序:
@SpringBootApplication
public class Springboot01HelloworldApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot01HelloworldApplication.class, args);
}
}
编写controller类:
@Controller
public class HelloController {
@RequestMapping("hello")
@ResponseBody
public String hello(){
return "hello spring";
}
}
1.5 执行主程序
若出现tomcat登录页面,账号为user,密码为控制台输出的一段字符串
自动生成的resources目录下有两个文件夹和一个配置文件
-
static:保存所有的静态资源(JS、CSS、图片等)
-
templates:保存所有的模板页面,SpringBoot默认采用JAR包并使用嵌入式的tomcat,默认是不支持JSP页面的,需要使用模板引擎(freemarker、thymeleaf)
-
application.properties:SpringBoot应用的配置文件,可在该配置文件中修改默认配置(比如应用的端口)
成功创建!