HelloWorld的写法(SpringBoot版本)
技术架构:java、SpringBoot框架、Mysql、Maven依赖管理、IDEA开发工具
- 创建Maven工程
- 引入依赖
- 创建主程序
- 编写业务逻辑
- 测试程序
- 简化配置
- 部署打包
- 创建Maven工程时可以采用本地下载Maven的方式,加入环境变量并修改../conf文件目录下的setting.xml文件。具体如下图所示。
//Maven下载链接 https://maven.apache.org/
- 加入环境变量path路径(根据你自己存放的上述安装包的路径):D:\apache-maven-3.8.1\bin
- 在同一文件目录下心建仓库位置:D:\apache-maven-3.8.1\MavenRepository
//新建本地仓库 <localRepository>D:\apache-maven-3.8.1\MavenRepository</localRepository> //更改镜像 <mirrors> <!-- mirror | Specifies a repository mirror site to use instead of a given repository. The repository that | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used | for inheritance and direct lookup purposes, and must be unique across the set of mirrors. | <mirror> <id>mirrorId</id> <mirrorOf>repositoryId</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://my.repository.com/repo/path</url> </mirror> --> <mirror> <id>alimaven</id> <mirrorOf>central</mirrorOf> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> </mirror> </mirrors>
3.设置Maven工程的仓库目录:
- 引入依赖(pom.xml)
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.4.5</version>
</parent>
<dependencies>
<!--web场景-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!-- 打包工具-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- 不安装打包插件会报错:UTF-8错误-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
</plugin>
</plugins>
</build>
- src/main/java文件夹下心建com.test0目录并创建主程序MainApplication.java文件
package com.test0;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
//启动类
SpringApplication.run(MainApplication.class,args);
}
}
- 编写业务逻辑
import org.springframework.web.bind.annotation.RestController;
import javax.xml.ws.RequestWrapper;
//字符串形式返回
//@ResponseBody、@Controller
@RestController
public class HelloController {
//自己定义访问路径
@RequestMapping("/hello")
public String handle(){
return "Hello SpringBoot";
}
}
- 启动并测试
访问localhost:8080/hello即可:
- 部署打包
//以jar包形式打包,直接运行jar包即可实现程序的运行
<packaging>jar</packaging>
<!-- 打包工具-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- 不安装打包插件会报错-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
</plugin>
</plugins>
</build>
下载clean和package,最终在target文件目录下会有一个.jar包;
//命令行进入此目录下并执行运行操作即可
E:\com.demo\target>java -jar com.demo-1.0-SNAPSHOT.jar