Spring boot项目入门
首先创建maven项目:
点击next,接下来设置想要创建的项目信息:
name:项目名称
loaction:存储路径
Groupld:包名
ArtifactId:项目名称
version:版本
创建好之后可以看到:
接下来在pom.xml文件中,添加父工程依赖:
(设置好父工程依赖后,后续在添加依赖时省略版本号可省略)
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
</parent>
添加java版本依赖:
<properties>
<java.version>1.8</java.version>
</properties>
接下来配置启动依赖:
<dependencys>
//web依赖 默认端口为8080
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
</dependencys>
pom文件中依赖配置完成之后,在src/main/java中添加java启动类:
package com.dsr;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
public class Application {
public static void main(String[] args){
SpringApplication.run(Application.class,args);
}
}
简单写一个controller类:
package com.dsr.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.sql.DataSource;
@RestController
public class HellorController {
@GetMapping("hello")
public String hello(){
return "hello spring boot";
}
}
@RestController注解,相当于@Controller+@ResponseBody两个注解的结合,返回json数据不需要在方法前面加@ResponseBody注解了,但使用@RestController这个注解,就不能返回jsp,html页面,视图解析器无法解析jsp,html页面
启动application类可以在浏览器中显示:
访问http://localhost:8080/hello