使用idea建立springboot的maven專案
阿新 • • 發佈:2021-07-28
一、建立父工程
idea建立springboot的工程方式有很多,這是其中一種,這個主要是父工程統一管理子工程。
準備工作:配置好JDK,MAVEN,需要知道或者瞭解啟動器,瞭解@RESTController和@Controller的區別。
1、建立sprigboot工程project-parent(需要聯網)在idea開發工具中,使用Spring Initializr快速初始化一個Spring Boot 模組。
2、生成的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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.3</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.test.demo</groupId> <artifactId>project-parent</artifactId> <version>0.0.1-SNAPSHOT</version> <name>project-parent</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <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> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
3、修改上邊生成的pom檔案
3.1可以修改版本(<version>2.5.3</version>)
3.2<artifactId> 節點後面新增 pom型別
3.3配置<dependencyManagement>鎖定依賴的版本
修改後的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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.test.demo</groupId> <artifactId>project-parent</artifactId> <version>0.0.1-SNAPSHOT</version> <name>project-parent</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <json.version>20170516</json.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>${json.version}</version> </dependency> </dependencies> </dependencyManagement> </project>
4、刪除src資料夾(父工程不寫controller,service,dao,交給子模組處理)
5、建立子模組,父工程右鍵->new->Module->maven
5、子模組新建後的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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>project-parent</artifactId> <groupId>com.test.demo</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>service</artifactId> </project>
6、修改pom檔案
6.1新增pom型別
<artifactId>節點後面新增 pom型別
<artifactId>service</artifactId> <packaging>pom</packaging>
6.2新增依賴
6.3修改後的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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>project-parent</artifactId> <groupId>com.test.demo</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>service</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
7、建立controller和啟動類,注意啟動類(TestApplication)的位置
controller檔案
package com.test.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/login") public class TestController { @RequestMapping("login") public String login(){ String test = "test"; return "login"; } }
啟動類
package com.test; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class TestApplication { public static void main(String[] args) { SpringApplication.run(TestApplication.class,args); } }
7、啟動啟動類,遊覽器輸入地址,頁面出現login表示成功。(因為這裡類註解用的@RESTController而不是@Controller,所以這裡返回的是字串login)