プログラミング逆引き辞典

~ 多言語対応のプログラミングレシピ ~

Spring Boot × Gradle 依存関係 記述するコードまとめ

Springスターター・プロジェクトの各依存関係を後から追加する方法

【環境】

  • IDE環境:Eclipse
  • ビルドツール:Gradle
     


    最初から依存関係が決まっている場合はプロジェクト作成時にEclipseで設定すれば良いが、後から追加したい場合もある
    その場合は依存関係を「build.gradle」に追記すれば良い
     

    ■Springスターター・プロジェクトのデフォルト

    plugins {
        id 'org.springframework.boot' version '2.2.1.RELEASE'
        id 'io.spring.dependency-management' version '1.0.8.RELEASE'
        id 'java'
    }
     
    group = 'com.example'
    version = '0.0.1-SNAPSHOT'
    sourceCompatibility = '1.8'
     
    repositories {
        mavenCentral()
    }
     
    dependencies {
        implementation 'org.springframework.boot:spring-boot-starter'
        testImplementation('org.springframework.boot:spring-boot-starter-test') {
                exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
        }
    }
     
    test {
        useJUnitPlatform()
    }

     
     


    ■Spring Web

    記述場所:dependencies

    記述コード:

    implementation 'org.springframework.boot:spring-boot-starter-web'
    //下記は削除
    //implementation 'org.springframework.boot:spring-boot-starter'

     
     


    ■Spring Data JPA

    記述場所:dependencies

    記述コード:

    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'

     
     


    ■Oracle Driver

    記述場所:dependencies

    記述コード:

    runtimeOnly 'com.oracle.ojdbc:ojdbc8'

     
    別途、「application.properties」にも下記DB接続情報の記述が必要

    # DB接続情報
    spring.datasource.url=jdbc:oracle:thin:DBUSER/0000@//localhost:1521/orclpdb
    spring.datasource.username=DBUSER
    spring.datasource.password=0000
    spring.datasource.driverClassName=oracle.jdbc.driver.OracleDriver

     
     


    ■Spring Security

    記述場所:dependencies

    記述コード:

    implementation 'org.springframework.boot:spring-boot-starter-security'
    testImplementation 'org.springframework.security:spring-security-test'

     
     


    ■Spring Boot DevTools

    記述場所①:sourceCompatibility = ‘〇〇’の後など(どこでもよい)

    記述コード①:

    configurations {
        developmentOnly
        runtimeClasspath {
                extendsFrom developmentOnly
        }
    }

    記述場所②:dependencies

    記述コード②:

    developmentOnly 'org.springframework.boot:spring-boot-devtools'

     
     


    ■Tymeleaf

    記述場所:dependencies

    記述コード:

    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'

     
     


    ■Lombok

    記述場所①:sourceCompatibility = ‘〇〇’の後など(どこでもよい)

    記述コード①:

    configurations {
        compileOnly {
                extendsFrom annotationProcessor
        }
    }

     

    記述場所②:dependencies

    記述コード②:

    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'

     
     


    ■Flyway

    記述場所:dependencies

    記述コード:

    implementation 'org.flywaydb:flyway-core'

     

    設定方法詳細はこちら→Flywayマイグレーションの設定方法

     
     


    ■下記の依存関係全てを追加した場合

    ・Spring Web
    ・Spring Data JPA
    ・Oracle Driver
    ・Spring Security
    ・Spring Boot DevTools
    ・Tymeleaf
    ・Lombok
    ・Flyway

    【記述例】:

    plugins {
    id 'org.springframework.boot' version '2.2.1.RELEASE'
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
    id 'java'
    }
     
    group = 'com.example'
    version = '0.0.1-SNAPSHOT'
    sourceCompatibility = '1.8'
     
    configurations {
        developmentOnly
        runtimeClasspath {
                extendsFrom developmentOnly
        }
        compileOnly {
                extendsFrom annotationProcessor
        }
    }
      
    repositories {
        mavenCentral()
    }
     
    dependencies {
        implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
        implementation 'org.springframework.boot:spring-boot-starter-security'
        implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
        implementation 'org.springframework.boot:spring-boot-starter-web'
        implementation 'org.flywaydb:flyway-core'
        compileOnly 'org.projectlombok:lombok'
        developmentOnly 'org.springframework.boot:spring-boot-devtools'
        runtimeOnly 'com.oracle.ojdbc:ojdbc8'
        annotationProcessor 'org.projectlombok:lombok'
        testImplementation('org.springframework.boot:spring-boot-starter-test') {
                exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
        }
        testImplementation 'org.springframework.security:spring-security-test'
    }
     
    test {
        useJUnitPlatform()
    }

     
     


    ■WebJars

    BootStrapやJQuery等を利用する為に設定
     
     

    ※記述するコードは下記サイトを参照

    https://www.webjars.org/
     
     

    記述場所:dependencies

    記述コード:

    implementation 'org.webjars:bootstrap:4.3.1'
    implementation 'org.webjars:jquery:3.4.1'

     

    Thymeleafに下記を記述

    <link rel="stylesheet" th:href="@{/webjars/bootstrap/4.3.1/css/bootstrap.min.css}">
    <script th:src="@{/webjars/jquery/3.4.1/jquery.min.js}"></script>
    <script th:src="@{/webjars/bootstrap/4.3.1/js/bootstrap.min.js}"></script>

     

    【Thymeleaf記述例】

    <!DOCTYPE html>
    <html xmlns:th="http://www/thymeleaf.org">
    <head>
    <meta charset="UTF-8">
    <link rel="stylesheet" th:href="@{/webjars/bootstrap/4.3.1/css/bootstrap.min.css}">
    <title>社員一覧</title>
    </head>
    <body>
    <div class="container">
    <h1>社員一覧</h1>
        <hr>
        <table class="table table-striped table-bordered table-condensed">
            <tr>
                <th>社員番号</th>
                <th>社員名</th>
                <th>メールアドレス</th>
                <th>権限</th>
                <th colspan="2">編集</th>
            </tr>
            <tr th:each="employee : ${employees}">
                <td th:text="${employee.employeeId}"></td>
                <td th:text="${employee.employeeName}"></td>
                <td th:text="${employee.email}"></td>
                <td th:text="${employee.adminFlag == '0'}? '一般' : '管理者'"></td>
                <td>
                    <form th:action="@{/employees/edit}" method="get">
                        <input type="submit" class="btn btn-default" name="edit" value="編集">
                        <input type="hidden" name="id" th:value="${employee.id}">
                    </form>
                </td>
                <td>
                    <form th:action="@{/employees/delete}" method="post">
                        <input type="submit" class="btn btn-danger" name="delete" value="削除">
                        <input type="hidden" name="id" th:value="${employee.id}">
                    </form>
                </td>
            </tr>
        </table>
    </div>
    <script th:src="@{/webjars/jquery/3.4.1/jquery.min.js}"></script>
    <script th:src="@{/webjars/bootstrap/4.3.1/js/bootstrap.min.js}"></script>
    </body>
    </html>

     
     


    ■MyBatis

    O/Rマッパーの一種
    「MyBatis Generator」でMapper&Entityの自動生成が可能
     

    記述場所:dependencies

    記述コード:

    implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.1'

     

    ・generatorConfig.xml

    Mapper&Entityの自動生成する為の設定ファイル
     

    格納場所:src/main/resources

    下記のフォーマットを参考に作成する
    generatorConfig.xmlのフォーマット
     
     


    ■thymeleaf-extras-springsecurity

    ユーザーの権限によって画面の表示項目を非表示にできる
     

    記述場所:dependencies

    記述コード:

    implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity4:3.0.4.RELEASE'