# Spring Boot 開發環境(使用 Intellij Community)
目標 : 建新Spring Boot 標準project
====== 基本架構 ===========
java JDK 17
spring boot / Jar
使用 lombok
使用 jpa
DB 使用 PostgreSQL
Spring Security
使用 Swagger2 (不在 Initializr 內,要另外加 ,參考https://www.baeldung.com/swagger-2-documentation-for-spring-rest-api
加 log (logback)
====== 環境開發 ===========
下載 Intellij Community https://www.jetbrains.com/idea/download/#section=mac
Intellij Community Plugin 安裝
進 Preferences / Plugins
a. Spring Boot Assistant
b. Lombok
c. JPA Buddy (產生 JPA Repository 套件)
d. Smart Tomcat (開發 war 才需要)
====== 建立專案 ===========
使用 spring initializr 建立專案,更改Artifact,Description, Package name改為全小寫簡化名稱(去掉Service)
[spring initializr](https://start.spring.io/#!type=maven-project&language=java&platformVersion=3.2.2&packaging=jar&jvmVersion=17&groupId=tw.sc&artifactId=OfficePieService&name=OfficePieService&description=Officepie%20Service&packageName=tw.sc.officepie&dependencies=lombok,web,data-jpa,postgresql,security)
====== 專案設定 ===========
開啟Intellij ,開啟下載回來的專案
檢查 Project Structure 設定
a. 選 File / Project Structure
b. 選 Modules
src/main/java 設為Sources
src/test 設為 Test
src/main/resources 設為Resources
設定 Maven
a.專案按右鍵,選Add Frameworks Support
b.Maven 勾選起來,按確定
c.pom.xml 按右鍵選Maven / Reload project
====== 加入程式 ===========
加入 Controller package
a. 在原本的package下 new/package ,取名 "controller" (自動增加 folder)
add rest Controller (web API)
在controller 下,新增一個 class 加入
(1)@RestController
(2)@RequestMapping("/hello") (web api對應url)
example :
@RestController
public class DemoController {
@RequestMapping("/hello")
public String hello() throws Exception{
return "HelloWorld ,Spring Boot!";
}
}
add MVC Controller (直接輸出html )
在controller 下,新增一個 class 加入
(1)@Controller
(2)@RequestMapping("/") (web 對應url)
example :
@Controller
public class DemoMVCController {
@RequestMapping("/helloworld")
public String helloWorld (Model model) throws Exception{
model.addAttribute("title", " helloworld ");
//View 的位置在 resources/templates
return "demo/helloworld";
}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title th:text="${title}"></title>
</head>
<body class="container">
<h1 th:text="${title}"></h1>
<p> text in html </p>
</body>
</html>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
加入 JPA (DB) 待補
注意事項=======
建立專案選 Maven 與 Maven POM 差別 : Maven 會建 src 目錄, Maven POM 不會 (不要選Maven POM ,選 Maven就好 )
tags: Spring boot Spring Intellij