1.SpringBoot基础入门之HelloWorld

一:创建Maven工程

二:添加依赖

官网文档最为致命

We need to start by creating a Maven pom.xml file. The pom.xml is the recipe that is used to build your project. Open your favorite text editor and add the following:

 ①要用到SpringBoot的功能,只需要引入父项目boot-starter-parent

    
        org.springframework.boot
        spring-boot-starter-parent
        2.7.5
    

②想要开发web,只需要添加web的场景启动器

想以往我们Spring,SpringMVC要导入一大推的东西,现在我们只需要添加一个依赖,spring-boot-starter-web,我们称之为web的场景启动器,也就是说我们想要开发web场景了,把这个依赖导进来即可

    
        
            org.springframework.boot
            spring-boot-starter-web
        
    

三:编写你的代码即可

①编写业务代码之前,需要有一个主程序将它引导SpringBoot的启动

/**
 * 主程序类
 * @SpringBootApplication:这是一个SpringBoot应用
 */
@SpringBootApplication
public class MainApplication {
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class,args);
    }
}

②编写我们的业务代码

//@ResponseBody
//@Controller
@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String handle01() {
        return "Hello,Spring Boot 2!";
    }
}

③测试

直接运行main方法即可

成功在页面中显示

1.SpringBoot基础入门之HelloWorld_第1张图片

 ④简化配置

application.properties,所有的配置都可以写在这个里面

我们以前想要改tomcat端口号,还要改打开tomcat配置文件等等一大堆,现在SpringBoot是来整合其他所有东西的一个总框架,所以SpringBoot为了简化期间,将我们未来所有的配置都可以抽取在一个配置文件里面,application.properties。可以修改tomcat,SpringMVC的一些设置,什么都可以改噢

server.port=8888

 ⑤简化部署

我们再也不需要在目标服务器来安装tomcat等一大堆,只需要给我们工程里面引入

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

把项目打成jar包,直接在目标服务器执行即可。

注意点:

  • 取消掉cmd的快速编辑模式

你可能感兴趣的:(SpringBoot2框架,spring,boot,java,后端)