【Spring Framework】Spring Framework ~ JPA / Oracle DB 編 ~

■ はじめに

https://blogs.yahoo.co.jp/dk521123/37755544.html
の続き。

今度は、JPAを使ってDBアクセスするサンプルを作成する。

■ 環境設定

 * サンプルを実行する上での環境設定

Spring用のプロジェクト作成

[1] Eclipse で [File]-[New]-[Other]-[Spring Boot]-[Spring Starter Project]を選択し「Next」ボタン押下
[2] 以下を入力し、「Next」ボタン押下
 + Name : 任意文字列(今回は「sample」)
 + Type : Gradle
[3] Spring Boot Versionを選択(今回は「2.1.0 M4」)し、以下にチェックを付けて、「Finish」ボタン押下
 + Core - DevTools
 + SQL - JPA / JDBC
 + Template Engine - Thymeleaf
 + Web - Web

JDBCドライブ

本当は、 Gradle からやりたかったけど...
 * JDBCドライブ(今回は「ojdbc7.jar」)を以下のサイトから、
   ダウンロードし、インポートする
https://www.oracle.com/technetwork/database/features/jdbc/jdbc-drivers-12c-download-1958347.html

テストデータ

 * 以下の関連記事を参照。
https://blogs.yahoo.co.jp/dk521123/37756250.html

■ サンプル

スターター

PersonApplication.java
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class PersonApplication {
  public static void main(String[] args) {
    SpringApplication.run(PersonApplication.class, args);
  }
}

コントローラ側

PersonController.java
package com.example.demo.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.example.demo.models.PersonModel;
import com.example.demo.repositories.PersonRepository;

@Controller
@EnableAutoConfiguration
public class PersonController {
  @Autowired
  PersonRepository repository;

  @RequestMapping(value = "/", method = { RequestMethod.GET, RequestMethod.POST })
  public String home(Model model) {
    List<PersonModel> people = this.repository.getAll();
    model.addAttribute("people", people);
    return "home";
  }
}

モデル側

Person.java
package com.example.demo.models;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Person implements Serializable {
  private static final long serialVersionUID = 1L;
  
  @Id
  private String id;
  @Column
  private String name;
  
  public Person(String id, String name) {
    this.id = id;
    this.name = name;
  }
  
  public String getId() {
    return this.id;
  }
  public void setId(String id) {
    this.id = id;
  }
  public String getName() {
    return this.name;
  }
  public void setName(String name) {
    this.name = name;
  }
}

データアクセス側

PersonRepository.java
package com.example.demo.repositories;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.example.demo.models.Person;

@Repository
public class PersonRepository {
  private final JdbcTemplate jdbcTemplate;

  @Autowired
  public PersonRepository(JdbcTemplate jdbcTemplate) {
    this.jdbcTemplate = jdbcTemplate;
  }

  public List<Person> getAll() {
    return jdbcTemplate.query("SELECT * FROM person",
        (resultSet, rowNum) -> new Person(resultSet.getString("id"), resultSet.getString("name")));
  }
}

ビュー側

home.html (src/main/resources/templates/home.html)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1">
<tr>
  <th>ID</th>
  <th>Name</th>
</tr>
<tr th:each="person:${people}">
  <td th:text="${person.getId()}"></td>
  <td th:text="${person.getName()}"></td>
</tr>
</table> 
</body>
</html>

設定ファイル

application.properties (src/main/resources/application.properties)
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:xe
spring.datasource.username=TEST_USER
spring.datasource.password=password
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver

■ 実行する

[1] プロジェクトを右クリックし、[Run As]-[3 Spring Boot App]を選択
[2] ブラウザで以下のURLにアクセスする
[[[http://localhost:8080/]]]