脚手架项目(开局只有一个idea,从零到一肝脚手架)
项目仓库
day01
环境准备
- git
- maven
- apifox
建立git仓库(gitee)
- 创建一个仓库
git bash克隆
win + x
,选择终端管理员(A)
,打开命令行工具(注意文件的路径)
1
| git clone https://gitee.com/higanbana-0/ape-frame.git
|
克隆到本地,用idea打开
或者idea克隆
初始化项目
- 修改
pom.xml
文件
以下pom.xml
若无特别说明,均指ape-frame/pom.xml
1 2 3 4 5 6 7 8 9 10 11
| <?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>com.higanbana</groupId> <artifactId>ape-frame</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> </project>
|
注意maven的设置
添加为maven项目,右键pom.xml
-> Add as Maven Project
- 设置阿里镜像
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <repositories> <repository> <id>central</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <layout>default</layout> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories>
|
新建ape-user
模块
右键ape-frame
-> New
-> Module
pom.xml(ape-user)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <?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> <parent> <groupId>com.higanbana</groupId> <artifactId>ape-frame</artifactId> <version>1.0-SNAPSHOT</version> </parent>
<artifactId>ape-user</artifactId>
<properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties>
</project>
|
新建ape-common
模块
右键ape-frame
-> New
-> Module
引入springboot(pom.xml)
1 2 3 4 5
| <parent> <artifactId>spring-boot-starter-parent</artifactId> <groupId>org.springframework.boot</groupId> <version>2.4.2</version> </parent>
|
配置build —>plugins(pom.xml)
1 2 3 4 5 6 7 8 9
| <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.10.1</version> </plugin> </plugins> </build>
|
为了统一版本管理
1 2 3 4 5 6
| <properties> <java.version>17</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.plugin.version>3.10.1</maven.plugin.version> </properties>
|
有了统一版本管理,可以将maven-compiler-plugin
的版本号改为
1 2 3 4 5 6 7 8 9
| <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${maven.plugin.version}</version> </plugin> </plugins> </build>
|
同理,设置configuration
1 2 3 4 5 6 7 8 9 10 11 12
| <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${maven.plugin.version}</version> <configuration> <source>${maven.compiler.source}</source> <target>${maven.compiler.target}</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins>
|
1 2 3 4 5 6 7 8
| <properties> <java.version>17</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.plugin.version>3.10.1</maven.plugin.version> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> </properties>
|
打包目录
1 2 3 4 5 6 7 8 9 10 11
| <resources> <resource> <directory>src/main/resources</directory> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources>
|
创建启动类
在 ape-user/src/main/java
目录下创建包 com.higanbana.user
在包下创建启动类 UserApplication
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.higanbana.user;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import javax.swing.*;
@SpringBootApplication public class UserApplication { public static void main(String[] args) { SpringApplication.run(UserApplication.class); } }
|
对于@SpringBootApplication
注解,在pom.xml
文件中添加spring-boot-starter-web
依赖
1 2 3 4 5 6
| <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
|
在ape-user/src/main/resources
目录下创建文件 application.yml
测试
在ape-user/src/test/java
目录下创建包 com.higanbana.user.controller
在包下创建测试类 TestController
1 2 3 4 5 6 7 8 9 10 11 12
| package com.higanbana.user.controller;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;
@RestController public class TestController { @GetMapping("/test") public String test(){ return "Hello World!"; } }
|