1
1

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 3 years have passed since last update.

SpringBoot GradleのDB接続メモ

Posted at

##DB接続設定
①「src/main/resources/application.properties」にDB接続情報を設定

application.properties
spring.datasource.url=jdbc:oracle:thin:@(server):1521:(sid)        //(1)
spring.datasource.username=(username)                   //(2)
spring.datasource.password=(password)                               //(3)
spring.datasource.driverClassName=oracle.jdbc.driver.OracleDriver    //(4)

(1)(server)(sid)にJDBCでの接続URLを設定
(2)(username)にDBへの接続ユーザを設定
(3)(password)にDBへの接続パスワードを設定
(4)DBのドライバを設定

②「build.gradle」にOracle接続用定義を設定
Oracle接続用jarとJPAライブラリ(ojdbcXX)の定義を追加

build.gradle
dependencies {
  implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
  compile files('lib/ojdbc6.jar')
}

③Repositoryクラス
JpaRepositoriクラスを継承することでselect等を行うメソッドを自動生成してくれる
カッコのエンティティへのアクセス用のリポジトリ

DemoRepository.java
@Repository
public interface DemoRepository extends JpaRepository <demoEntity, Long> {
}

④Entityクラス
@Entityでエンティティクラス
@Tableで指定したテーブルへのアクセス用エンティティクラス
@DataでGetter,Setterメソッドを使用できる

demoEntity.java
@Entity 
@Table(name="テーブル名")
@Data
public class demoEntity {
  @Id
  private Integer id;

  @Column(name="カラム名")
  private String name;
}

⑤Controllerクラス
コントローラクラスからリポジトリを呼び出してデータを取得

DemoController.java
@RestController
public class DemoController {
  @Autowired
  private DemoRepository demoRepository;

  public List<demoEntity> demoDataList(){
    return demoRepository.findAll();
  }
}
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?