■ サンプル
スターター
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