4
0

More than 3 years have passed since last update.

build.gradleに依存関係を追加したのにインポートが追加されない

Last updated at Posted at 2021-09-13

概要

JdbcTemplateを使ってDAOクラスを実装するケースを例にして、build.gradleに依存関係を追加したのにインポートが追加されない場合の原因および解決策を記載する。

環境

Eclipse IDE:2021-06 (4.20.0)

事象

JdbcTemplateを使ってDAOクラスを実装する。

build.gradle
・・・
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    runtimeOnly 'com.h2database:h2'
    implementation 'org.springframework.boot:spring-boot-devtools'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
・・・

SampleDaoImpl.java
package com.example.sample.dao

public class SampleDaoImpl implements SampleDao {
    private final JdbcTemplate jdbcTemplate;
    ・・・
}

この状態だと、SampleDaoImpl.javaのソースビューに

JdbcTemplateを型解決できません

というエラーが表示される。
これは、build.gradleの依存関係にJdbcTemplateのライブラリが定義されていないためである。

そこで、build.gradleの依存関係にJdbcTemplateのライブラリ'org.springframework.boot:spring-boot-starter-jdbc'を追加する。

build.gradle
・・・
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    runtimeOnly 'com.h2database:h2'
    implementation 'org.springframework.boot:spring-boot-devtools'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    implementation 'org.springframework.boot:spring-boot-starter-jdbc'
}
・・・

ファイルを保存して、インポートの編成を実行する。
しかしながら、SampleDaoImpl.javaにJdbcTemplateのインポートが追加されない。

JdbcTemplateを型解決できません

というエラーが変わらず表示される。

原因

原因は、build.gradleを編集後、Eclipseに変更が反映されていないことだった。

解決策

解決策としては、build.gradleを編集後、gradleプロジェクトをリフレッシュする。

gradleプロジェクトをリフレッシュする方法は、パッケージエクスプローラーの右クリックメニューからGradle>Gradleプロジェクトのリフレッシュを実行する

image.png

F5キーでプロジェクトをリフレッシュしても解消しないので注意。

4
0
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
4
0