1.點擊Creat New Project
或
2.左邊選擇Maven
然後next
3.填寫GroupId和ArtifactId(隨便填寫)
然後點擊next
然後確定項目名稱;和項目所在目錄。
點擊finish
4.目錄如下
5.直接刪除SRC目錄,在pom文件加上packing
項目目錄如下
<packaging>pom</packaging>
6.創建第一個springboot模塊
右鍵new->module
右側選擇箭頭所示,然後next
填寫Artifact,然後next
選擇要加的模塊,根據自己的要求添加,我暫時只添加web模塊。點擊next
基本都是默認,然後finish
7.完成後的項目目錄
8.重複第六步創建第二個module,並且在chapter_one的pom.xml文件中加入module2的依賴
<dependency>
<groupId>com.example</groupId>
<artifactId>chapter_two</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
9.在chapter_two中建一個model目錄,目錄下生成一個Account.java文件
Account.java內容
package com.example.chapter_two.model;
import java.sql.Timestamp;
public class Account {
private Long id;
private Timestamp timestamp;
private String accountName;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Timestamp getTimestamp() {
return timestamp;
}
public void setTimestamp(Timestamp timestamp) {
this.timestamp = timestamp;
}
public String getAccountName() {
return accountName;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
@Override
public String toString() {
return "Account{" + "id=" + id + ", timestamp=" + timestamp + ", accountName='" + accountName + '\'' + '}';
}
}
10.在chapter_one中,生成一個controller目錄,生成一個controller;
package com.example.chapter_one.controller;
import com.example.chapter_two.model.Account;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.sql.Timestamp;
import java.sql.Date;
@RestController
public class WebTestController {
@GetMapping("/chapter.one")
public String get(){
Account account=new Account();
account.setAccountName("chapter_two");
account.setId(1L);
account.setTimestamp(new Timestamp(new Date(323222222L).getTime()));
return account.toString();
}
}
11.啓動ChapterOne,瀏覽器中輸入 http://localhost:8080/chapter.one
測試成功,說明我麼的項目搭建完成。
可能碰到的問題:
1.新建SpringBoot模塊,可能版本過高,導致pom.xml很多jar包導如錯誤,建議使用2.1.6版本,