(書き途中)
マルチユーザのメモ帳。
UserとMemoを@ManyToManyでリレーション
EntityとRepository
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package net.teachingprogramming.mybootapp.entity; import lombok.Getter; import lombok.Setter; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.ManyToMany; import javax.persistence.OneToMany; import javax.persistence.Table; import java.util.List; @Entity @Table(name = "USERS") public class User { @Id @Setter @Getter private String username; @Setter @Getter private String password; @Getter @Setter private Boolean enabled; @Getter @Setter @OneToMany(mappedBy = "user") private List<Comment> commentList; @Getter @Setter @ManyToMany(mappedBy = "userList") private List<Memo> memoList; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
package net.teachingprogramming.mybootapp.entity; import lombok.Getter; import lombok.Setter; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.ManyToMany; import java.util.ArrayList; import java.util.List; @Entity public class Memo { @Id @GeneratedValue @Getter @Setter private Long id; @Getter @Setter @ManyToMany List<User> userList; @Getter @Setter String text; public Memo() { this.userList = new ArrayList<>(); } } |
1 2 3 4 5 6 7 |
package net.teachingprogramming.mybootapp.repository; import net.teachingprogramming.mybootapp.entity.Memo; import org.springframework.data.jpa.repository.JpaRepository; public interface MemoRepository extends JpaRepository<Memo, String>{ } |
View
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8" /> <title>BBS</title> </head> <body> <h1>MEMO</h1> <h2>一覧</h2> <ol> <li th:each="memo:${user.memoList}"> <div> <span th:each="memouser:${memo.userList}" th:text="${memouser.username}">名前</span> </div> <div th:text="${memo.text}"> </div> </li> </ol> <h2>新規作成</h2> <form th:action="@{/memo/}" method="post"> <span th:each="alluser:${allUserList}" th:remove="tag"> <input name="memouser" type="checkbox" th:value="${alluser.username}" th:id="${alluser.username}" th:checked="${alluser.equals(user)}"/> <label th:for="${alluser.username}" th:text="${alluser.username}">ユーザ名</label> </span> <div> <textarea name="text" rows="5" cols="100"></textarea> </div> <input type="submit" /> </form> </body> </html> |
Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
package net.teachingprogramming.mybootapp.controller; import net.teachingprogramming.mybootapp.entity.Memo; import net.teachingprogramming.mybootapp.repository.MemoRepository; import net.teachingprogramming.mybootapp.repository.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import javax.servlet.http.HttpServletRequest; @Controller @RequestMapping("/memo") public class MemoController { @Autowired private UserRepository userRepository; @Autowired private MemoRepository memoRepository; @RequestMapping(value = "/", method = RequestMethod.GET) public String indexGet(ModelMap modelMap,HttpServletRequest httpServletRequest) { modelMap.addAttribute("user", userRepository.getOne(httpServletRequest.getRemoteUser())); modelMap.addAttribute("allUserList", userRepository.findAll()); return "memo/index"; } @RequestMapping(value = "/", method = RequestMethod.POST) public String indexPost(@RequestParam("memouser") String[] memouser, @RequestParam("text") String text, ModelMap modelMap,HttpServletRequest httpServletRequest) { Memo memo = new Memo(); for (String username : memouser) { memo.getUserList().add(userRepository.getOne(username)); } memo.setText(text); memoRepository.save(memo); return indexGet(modelMap, httpServletRequest); } } |