0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Spring スタータ・プロジェクト (Spring Initializer) メモ(eclipse2022)

Posted at

Eclipse2022 使って Spring スタータ・プロジェクト (Spring Initializer) よりプロジェクト作るときの簡易なメモ
Thmeleaf, DB系(Spring Data JDBC, Spring Data JPA)の依存追加した時になんもせずに実行するとエラーになるのでその対処方法です

環境

  • windows11
  • Eclipse2022 (pleiades)
  • Spring Boot 3.1.5 (Maven)
  • Java17
  • MySQL 8.0.28

Spring スタータ・プロジェクト(Spring Initializer)依存追加

  • Spring Boot DevTools
  • Spring Web
  • Lombok
  • Spring Data JDBC
  • Thmeleaf
  • MySQL Driver
  • Spring JPA

やったこと(手順てきなやつ)

  1. Eclipse で Spring スタータ・プロジェクト(Spring Initializer) の新規作成
    [ファイル] - [新規] - [Spring スタータ・プロジェクト(Spring Initializer)] をクリック
    image.png

  2. 設定ダイアログで各設定を行い完了
    画像の赤枠部分が参考
    左の [タイプ]、[Javaバージョン]、右の依存関係は画像に合わせる
    左の [名前]、[グループ]、[パッケージ]は好きな値でOK!
    image.png

  3. DB接続情報などを application.properties に設定する
    DB接続情報ないとエラー発生します(他は雰囲気で設定してます)

    application.properties
    server.port=8080
    # datasource 周りは各自の環境に合わせてください
    spring.datasource.url=jdbc:mysql://localhost:3306/sampledb 
    spring.datasource.username=username
    spring.datasource.password=password
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.jpa.database=MYSQL
    spring.jpa.show-sql=true
    spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
    
  4. Thmeleafのテンプレ用の GreetingController.java を作成する
    src/main/java/sample の直下に controller パッケージを作成してその直下につくる

    GreetingController.java
    package sample.contller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    
    @Controller
    public class GreetingController {
    
        @GetMapping("/greeting")
        public String greeting(@RequestParam(name = "name", required = false, defaultValue = "World") String name,
                Model model) {
            model.addAttribute("name", name);
            return "greeting";
        }
    
    }    
    
  5. Thmeleafのテンプレ用の greeting.html を作成する
    src/main/reresources/templates 直下に作成

    greeting.html
    <!DOCTYPE html>
    <html>
    <head>
        <title>Getting Started: Serving Web Content</title> 
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>
    <body>
        <p th:text="|Hello, ${name}!|" />
    </body>
    </html>
    
  6. index.html を作成する
    src/main/reresources/static 直下に作成

    index.html
    <!DOCTYPE html>
    <html>
    <head>
        <title>Getting Started: Serving Web Content</title> 
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>
    <body>
        <p>Get your greeting <a href="/greeting">here</a></p>
    </body>
    </html>
    
  7. Maven Install を実行する
    プロジェクト右クリック - [実行] - [5 Maven install] をクリック
    image.png

  8. Spring Boot を実行する
    プロジェクト右クリック - [実行] - [Spring Boot アプリケーション] をクリック
    image.png

  9. ブラウザで http://localhost:8080/ にアクセスして以下みたいになればOK
    image.png

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?