-
src/main/java/com/example/sample/domain/User.java
を作成する
package com.example.sample.domain;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue
private Long id;
private String name;
private Integer age;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
-
src/main/java/com/example/sample/domain/UserRepository.java
を作成する
package com.example.sample.domain;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
}
-
src/main/java/com/example/sample/service/UserService.java
を作成する
package com.example.sample.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.sample.domain.User;
import com.example.sample.domain.UserRepository;
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public List<User> findAll() {
return userRepository.findAll();
}
public User findOne(Long id) {
return userRepository.findOne(id);
}
public User save(User user) {
return userRepository.save(user);
}
public void delete(Long id) {
userRepository.delete(id);
}
}
-
src/main/java/com/example/sample/web/UserController.java
を作成する
package com.example.sample.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.example.sample.domain.User;
import com.example.sample.service.UserService;
@Controller
@RequestMapping("users")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(method = RequestMethod.GET)
public String index(Model model) {
model.addAttribute("users", userService.findAll());
return "users/index";
}
@RequestMapping(value = "new", method = RequestMethod.GET)
public String newUser(Model model) {
model.addAttribute("user", new User());
return "users/new";
}
@RequestMapping(value = "{id}/edit", method = RequestMethod.GET)
public String edit(@PathVariable Long id, Model model) {
User user = userService.findOne(id);
model.addAttribute("user", user);
return "users/edit";
}
@RequestMapping(value = "{id}", method = RequestMethod.GET)
public String show(@PathVariable Long id, Model model) {
User user = userService.findOne(id);
model.addAttribute("user", user);
return "users/show";
}
@RequestMapping(method = RequestMethod.POST)
public String create(@ModelAttribute User user) {
userService.save(user);
return "redirect:/users";
}
@RequestMapping(value = "{id}", method = RequestMethod.PUT)
public String update(@PathVariable Long id, @ModelAttribute User user) {
user.setId(id);
userService.save(user);
return "redirect:/users";
}
@RequestMapping(value = "{id}", method = RequestMethod.DELETE)
public String destroy(@PathVariable Long id) {
userService.delete(id);
return "redirect:/users";
}
}
-
src/main/resources/templates/users/index.html
を作成する
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>Listing Users</title>
<link rel="stylesheet" href="/css/bootstrap.min.css" />
<script src="/js/jquery.js"></script>
<script src="/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h1>Listing User</h1>
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr th:each="user : ${users}">
<td th:text="${user.id}"></td>
<td th:text="${user.name}"></td>
<td th:text="${user.age}"></td>
<td><a class="btn btn-default btn-xs" th:href="@{/users/{id}(id=${user.id})}">Show</a></td>
<td><a class="btn btn-default btn-xs" th:href="@{/users/{id}/edit(id=${user.id})}">Edit</a></td>
<td>
<form th:action="@{/users/{id}(id=${user.id})}" th:method="delete">
<input class="btn btn-default btn-xs" type="submit" value="destroy" />
</form>
</td>
</tr>
</tbody>
</table>
<a class="btn btn-default btn-xs" href="/users/new">New</a>
</div>
</body>
</html>
-
src/main/resources/templates/users/new.html
を作成する
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>New User</title>
<link rel="stylesheet" href="/css/bootstrap.min.css" />
<script src="/js/jquery.js"></script>
<script src="/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h1>New User</h1>
<form action="/users" th:method="post">
<div class="form-group">
<label class="control-laebl">Name</label>
<input class="form-control" type="text" name="name" />
</div>
<div class="form-group">
<label class="control-label">Age</label>
<input class="form-control" type="text" name="age" />
</div>
<button class="btn btn-default btn-xs" type="submit">Create</button>
</form>
<div class="pull-right">
<a class="btn btn-link btn-xs" href="/users">Back</a>
</div>
</div>
</body>
</html>
-
src/main/resources/templates/users/edit.html
を作成する
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>Editing User</title>
<link rel="stylesheet" href="/css/bootstrap.min.css" />
<script src="/js/jquery.js"></script>
<script src="/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h1>Editing User</h1>
<form th:action="@{/users/{id}(id=${user.id})}" th:method="put">
<div class="form-group">
<label class="control-laebl">Name</label>
<input class="form-control" type="text" name="name" th:value="${user.name}" />
</div>
<div class="form-group">
<label class="control-label">Age</label>
<input class="form-control" type="text" name="age" th:value="${user.age}" />
</div>
<button class="btn btn-default btn-xs" type="submit">Update</button>
</form>
<div class="pull-right">
<a class="btn btn-link btn-xs" href="/users">Back</a>
</div>
</div>
</body>
</html>
-
src/main/resources/templates/users/show.html
を作成する
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>Show User</title>
<link rel="stylesheet" href="/css/bootstrap.min.css" />
<script src="/js/jquery.js"></script>
<script src="/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h1>Show User</h1>
<div>
<label>ID</label>
<p th:text="${user.id}"></p>
</div>
<div>
<label>Name</label>
<p th:text="${user.name}"></p>
</div>
<div>
<label>Age</label>
<p th:text="${user.age}"></p>
</div>
<div>
<a class="btn btn-default btn-xs" th:href="@{/users/{id}/edit(id=${user.id})}">Edit</a>
</div>
<div class="pull-right">
<a class="btn btn-link btn-xs" href="/users">Back</a>
</div>
</div>
</body>
</html>
-
src/main/resources/application.properties
に以下の内容を追記
spring.thymeleaf.cache: false
spring.jpa.hibernate.ddl-auto: update
- 以下に配置(ファイル名が異なる場合はhtmlファイルを全て書き換えて下さい)
src/main/resources/static/js/jquery.js
- css/jsそれぞれ配置
src/main/resources/static/js/bootstrap.min.js
src/main/resources/static/css/bootstrap.min.css