ある組織では,週例ミーティングで各自が1週間の活動についてToDo/Doneに基づく進捗報告を行っている.
しかしながら,現状では自然言語で記述されており,管理が人それぞれバラバラであるため全体の見渡しも悪い.
この現状を,Webアプリケーションを用いたシステム化で改善したい.
CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; CREATE USER todo IDENTIFIED BY 'todo+' GRANT ALL ON todo.* to todo
# MySQLデータベース接続設定 spring.datasource.url=jdbc:mysql://localhost:3306/todo?serverTimezone=JST spring.datasource.username=todo spring.datasource.password=todo+ # Spring-JPA: DBのテーブルを自動作成してくれる機能 # create: 新規作成, update: なければ新規作成, create-drop: 新規作成し終了時に削除 spring.jpa.hibernate.ddl-auto=update
<!DOCTYPE html> <html lang="ja" xmlns:th="http://www.w3.org/1999/xhtml"> <head> <title>ToDoアプリ ユーザ管理</title> <meta charset="utf-8"/> </head> <body> <a href="inputform.html">ユーザを追加する</a><br> <a href="/users/{uid}">ユーザ情報を表示する(自分でuidいれてください)</a><br> <a href="/users">ユーザ一覧を表示する</a> </body> </html>
<!DOCTYPE html> <html lang="ja" xmlns:th="http://www.w3.org/1999/xhtml"> <head> <title>Input Form</title> </head> <body> <form action="/users" method="post"> <input type="text" name="uid"> <input type="text" name="name"> <input type=submit value="submit" name=submit> </form> </body> </html>
<!DOCTYPE html> <html lang="ja" xmlns:th="http://www.w3.org/1999/xhtml"> <head> <meta charset="UTF-8"> <title>User</title> </head> <body> <p th:text="'ID: ' + ${uid} + ' Name: ' + ${name} + ' Created at: ' + ${createdAt}"></p> </body> </html>
<!DOCTYPE html> <html lang="ja" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>ユーザ情報</title> </head> <body> <th:block th:object="${user}"> <h1>ユーザ情報 (ID=[[*id]])</h1> <table> <tr> <td> [[*uid]] </td> <td> [[*name]] </td> <td> [[*createdAt]] </td> </tr> </table> </th:block> </body> </html>
<!DOCTYPE html> <html lang="ja" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>uid検索結果</title> </head> <body> <h1>uid検索結果</h1> <table> <tr th:each="p: ${ulist}"> <td> [[${p.uid}]] </td> <td> [[${p.name}]] </td> <td> [[${p.createdAt}]] </td> </tr> </table> </body> </html>
@Repository public interface UserRepository extends CrudRepository<User, Long> { // 以下,カスタムクエリ /** * uidで検索 * @param name 検索するuid・LIKEで検索 "%test%" * @return */ public Iterable<User> findUserByUidLike(String uid); }
package jp.kobespiral.todo.service; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import jp.kobespiral.todo.dto.UserDto; import jp.kobespiral.todo.entity.User; import jp.kobespiral.todo.form.UserForm; import jp.kobespiral.todo.repository.UserRepository; @Service public class UserService { @Autowired private UserRepository userRepository; // スレのレポジトリ public UserDto createUser(UserForm form) { // 作成日時 Date now = new Date(); // ユーザ作ってセーブ User user = userRepository.save(new User(null, form.getUid(), form.getName(), now)); return UserDto.build(user); } public List<UserDto> getUserByuid(String uid) { ArrayList<UserDto> list = new ArrayList<UserDto>(); for (User user : userRepository.findUserByUidLike( "%" + uid + "%")) { list.add(UserDto.build(user)); } return list; } public List<UserDto> getAllUsers(){ ArrayList<UserDto> list = new ArrayList<UserDto>(); for (User user : userRepository.findAll()) { list.add(UserDto.build(user)); } return list; } }
package jp.kobespiral.todo.controller; import java.util.List; 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.PostMapping; import org.springframework.web.bind.annotation.GetMapping; import jp.kobespiral.todo.dto.UserDto; import jp.kobespiral.todo.form.UserForm; import jp.kobespiral.todo.service.UserService; @Controller public class UserController { @Autowired private UserService us; @PostMapping("/users") public String addUser(@ModelAttribute("userForm") UserForm userform, Model model) { String name = userform.getName(); String uid = userform.getUid(); UserDto userdto = us.createUser(userform); model.addAttribute("uid", userdto.getUid()); model.addAttribute("name", userdto.getName()); model.addAttribute("createdAt", userdto.getCreatedAt()); return "success"; } @GetMapping("/users/{uid}") public String showUser(@PathVariable String uid, Model model){ List<UserDto> userList = us.getUserByuid(uid); model.addAttribute("ulist", userList); return "ulist"; } @GetMapping("/users") public String showAllUser(Model model){ List<UserDto> userList = us.getAllUsers(); model.addAttribute("allulist", userList); return "alluserlist"; } }
package jp.kobespiral.todo.form; import lombok.Data; @Data public class UserForm { private String uid; private String name; }
package jp.kobespiral.todo.entity; import java.util.Date; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Temporal; import javax.persistence.TemporalType; import com.fasterxml.jackson.annotation.JsonIdentityInfo; import com.fasterxml.jackson.annotation.ObjectIdGenerators; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor @Entity @JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class) public class User { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Long id; private String uid; private String name; @Temporal(TemporalType.TIMESTAMP) private Date createdAt; //作成日時 }
package jp.kobespiral.todo.dto; import java.util.Date; import jp.kobespiral.todo.entity.User; import lombok.Data; @Data public class UserDto { private Long id; // key private String uid; //uid private String name; //name private Date createdAt; //作成日時 public static UserDto build(User user) { UserDto dto = new UserDto(); dto.number = user.getId(); dto.uid = user.getUid(); dto.name = user.getName(); dto.createdAt = user.getCreatedAt(); return dto; } }