springboot框架搭建
源码地址:https://gitee.com/lingyao1/springbootdemo.
一、pom.xml文件
首先创建pom.文件 并添加相关依赖。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--springboot的父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.lingyao</groupId>
<artifactId>springbootdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springbootdemo</name>
<description>spinDemo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<!--springboot核心依赖 -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--单元测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--web模块-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!--maven打包插件-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
二、创建controller包
在项目根目录下创建创建controller包,然后写一个controller类。(被@controller标记的类)
package com.lingyao.springbootdemo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author lingyao
* @description view层,前后端数据交互层
*/
@RestController
/**
* @Controller类中的方法可以直接通过返回String跳转到jsp、ftl、html等模版页面。也可以在方法上加@ResponseBody注解,返回实体对象。
* @RestController类中的所有方法只能返回String、Object、Json等实体对象,不能跳转到模版页面
*/
public class LoginController {
@RequestMapping(value = "/")
String index() {
return "this is Spring Boot Demo!";
}
}
三、运行启动类
一般启动类在用IDE生成时默认生成的名字是ProjectNameApplication(ProjectName指的是项目名)。此时项目能正常启动,访问 "localhost:8080"便出现"this is Spring Boot Demo!"说明框架搭建成功。
注意:ProjectNameApplication启动类必需要和项目根目录同级,这样包扫描就会扫描到根目录下的所有包以及子包中的内容了。
package com.lingyao.springbootdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 启动类必需要和项目根目录同级,这样包扫描就会扫描到根目录下的所有包以及子包中的内容了;
*/
@SpringBootApplication
public class SpringbootdemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootdemoApplication.class, args);
}
}
四、项目目录基本结构
src main java com.lignyao.springboot.demo controller resoureces test java com.lignyao.springboot.demo target