可以参考官方文档:
Idea创建maven工程
引入依赖
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>com.shu</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
创建主程序,建包名com.nanjing.boot
package com.nanjing.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 主程序类
* @SpringBootApplication:这是一个SpringBoot应用
*/
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class,args);
}
}
编写业务
package com.nanjing.boot.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/*
@RestController相当于
@Controller和@ResponseBody
*/
@RestController
public class HelloController {
@RequestMapping("/hello")
public String handle01(){
return "Hello spring boot2";
}
}
直接运行main方法
浏览器访问:http://localhost:8080/hello
简化配置
以后所有的配置都可以在application.properties中写,例如:端口号修改为8888
springboot也简化了部署,不需要在目标服务器上安装tomcat,
只需要先引入下面这个插件:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
然后把项目打成jar包
直接在目标服务器执行即可
改进:
上面新建springboot项目有个缺点,就是看不到项目名称
可以参考畅购商城的方式,商城-微服务工程结构搭建_Easy Coding-CSDN博客
在idea-workspace中新建一个changgou文件夹,用idea打开changgou,
选中changgou——>New——>Module
或者在idea-workspace中新建一个helloworld文件夹,new项目的时候选中