SpringBoot学习-01

运行简单的Hello World SpringBoot程序

第一种方式:通过maven工程创建spring boot项目

使用IDEA选择maven工程,选择jdk版本和quick start模板,点击next

GroupId和ArtifactId根据自己喜欢创建,一路点击next即可创建成一个原始的maven工程

工程目录是这样


接着添加spring boot依赖,在pom.xml中加入如下

	<!-- Inherit defaults from Spring Boot -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.4.RELEASE</version>
	</parent>

	<!-- Add typical dependencies for a web application -->
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>

maven就会导入spring boot相关的依赖


然后创建一个HelloWorldMainApplication类

@SpringBootApplication
public class HelloWorldMainApplication {
    public static void main(String[] args) {
        SpringApplication.run(HelloWorldMainApplication.class,args);
    }
}

同时创建一个controller包,在包里创建HelloController类,写如下代码

@Controller
public class HelloController {
    @ResponseBody
    @RequestMapping("/hello")
    public String hello(){
        return "Hello World";
    }
}

最后运行HelloWorldMainApplication类,在浏览器中输入http://localhost:8080/hello就可以看见,输出了Hello World


将应用打包成jar包

在pom.xml中添加如下依赖

<!--maven插件,将应用打包成一个可执行的jar包-->
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>

接着如图操作,会得到一个jar包,在如下的位置

从命令行中运行jar包也可 运行程序



第二种方式:使用IDEA快速搭建spring boot项目

选择spring initializr,选择jdk版本号,点击下一步

根据自己的要求选择

选择自己需要的依赖,IDEA会自动在pom.xml中帮我们添加对应的依赖,最后一直点next即可创建出一个spring boot项目


resource文件夹中目录结构

  1. static:保存所有的静态资源:js,css,images;
  2. templates:保存所有的模板页面(SpringBoot默认不支持jsp )
  3. application.properties:spring boot应用的配置文件

猜你喜欢

转载自blog.csdn.net/qq_40866897/article/details/89311048