Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Springで始める3層アーキテクチャ / start three layer architecture with spring

D98762c822b683773188e826a00d243e?s=47 yachiy
November 14, 2019
51

Springで始める3層アーキテクチャ / start three layer architecture with spring

D98762c822b683773188e826a00d243e?s=128

yachiy

November 14, 2019
Tweet

Transcript

  1. 4QSJOHͰ࢝ΊΔ૚ΞʔΩςΫνϟ

  2. ૚ͱ͸

  3. ϓϨθϯςʔγϣϯ૚ σʔλΞΫηε૚ ϏδωεϩδοΫ૚

  4. ϓϨθϯςʔγϣϯ૚

  5. ը໘ͩͬͨΓɺ"1*ͷΤϯυϙΠϯτͩͬͨΓ ར༻ऀͱͷڥքɺΠϯλϑΣʔεʹͳΔ෦෼

  6. σʔλΞΫηε૚

  7. σʔλͷӬଓԽɺ࠶ߏ੒Λߦ͏࢓૊Έʢ%# ͱ͔ϑΝΠϧͱ͔ʣͱͷ΍ΓऔΓΛߦ͏૚

  8. ϏδωεϩδοΫ૚

  9. ۀ຿ͷϩδοΫΛهड़͢Δ૚

  10. ʁʁʁ

  11. ۀ຿ͱ͸

  12. ۀ຿ ˠͦͷΞϓϦέʔγϣϯͰղܾ͍ͨ͠໰୊ ˠଟذʹΘͨΔʹෳࡶ

  13. IUUQTXXXTMJEFTIBSFOFUNBTVEBTT

  14.  ͜͜

  15. ݁ہҰ൪ूத͍ͨ͠ͷ͸ۀ຿ͷ಺༰͔ͩΒ ೖग़ྗ͔Β෼཭ִͯ͠཭͠·͠ΐ͏ͱ͍͏࿩

  16. 4QSJOHͰ͍͏ͱ

  17. !$POUSPMMFS !3FTU$POUSPMMFS !3FQPTJUPSZ !4FSWJDF

  18. ࣮ફ

  19. ίϝϯτΛॻ͖ࠐΊΔϝϞாΛߟ͑Δ ʢߦͷ్தʹίϝϯτೖΕͯ΋ແࢹ͢Δʣ ಺༰Λॻ͚Δ ೖྗ಺༰ΛอଘͰ͖Δ ίϝϯτ͕ফ͑ͨঢ়ଶͰ಺༰͕ݟΕΔ

  20. ίϝϯτΛॻ͖ࠐΊΔϝϞாΛߟ͑Δ ʢߦͷ్தʹίϝϯτೖΕͯ΋ແࢹ͢Δʣ ಺༰Λॻ͚Δ ೖྗ಺༰ΛอଘͰ͖Δ ίϝϯτ͕ফ͑ͨঢ়ଶͰ಺༰͕ݟΕΔ

  21. package com.yachiy.spring.introduction.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller public class MemoController

    { @GetMapping("/") public String memo() { return "memo"; } } .FNP$POUSPMMFSKBWB
  22. <!doctype html> <html> <head> <meta charset="UTF-8"/> <title>ίϝϯτΞ΢τ͕ޮ͘ϝϞாʢԾʣ</title> </head> <body> <h1>ίϝϯτΞ΢τ͕ޮ͘ϝϞாʢԾʣ</h1>

    <textarea id="content" cols="50" rows="20" placeholder="಺༰Λೖྗ͍ͯͩ͘͠͞"></textarea> <button>ૹ৴</button> </body> </html> NFNPIUNM
  23. ࣮ߦͯ͠MPDBMIPTUʹΞΫηε

  24. ը໘͸ग़Δʢೖྗͯ͠ૹ৴ͯ͠΋Կ΋ى͜Βͳ͍͚Ͳʣ

  25. ࣍ʹೖྗ஋ͷड͚औΓ

  26. package com.yachiy.spring.introduction.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping;

    @Controller public class MemoController { @GetMapping("/") public String memo() { return "memo"; } @PostMapping("/") public String receive(MemoForm memoForm, Model model) { model.addAttribute("content", memoForm.getContent()); return "received"; } } .FNP$POUSPMMFSKBWB 1045ͷ݁ՌΛड͚औΔϝιουΛ ௥Ճ
  27. package com.yachiy.spring.introduction.controller; /** * ϝϞ಺༰Λड͚౉ͨ͢ΊͷϞσϧ */ public class MemoForm {

    private String content; public String getContent() { return content; } public void setContent(String content) { this.content = content; } } .FNP'PSNKBWB
  28. <!doctype html> <html xmlns:th="http://www.w3.org/1999/xhtml"> <head> <meta charset="UTF-8"/> <title>݁Ռ</title> </head> <body>

    <h1>಺༰Λड͚औΓ·ͨ͠</h1> <div th:text="${content}"></div> </body> </html> SFDFJWFEIUNM
  29. <!doctype html> <html> <head> <meta charset="UTF-8"/> <title>ίϝϯτΞ΢τ͕ޮ͘ϝϞாʢԾʣ</title> </head> <body> <h1>ίϝϯτΞ΢τ͕ޮ͘ϝϞாʢԾʣ</h1>

    <form method="post" action="/"> <textarea id="content" name="content" cols="50" rows="20" placeholder="಺༰Λೖྗ͍ͯͩ͘͠͞"></textarea> <button>ૹ৴</button> </form> </body> </html> NFNPIUNM GPSNλά௥Ճ .FNP'PSNͷϑΟʔϧυ໊ͱҰகͤ͞Δ
  30. ࣮ߦͯ͠MPDBMIPTUʹΞΫηε

  31. ೖྗͯ͠ ԡԼ

  32. ೖྗ಺༰͕൓ө͞Ε͍ͯΔ

  33. ίϝϯτΛॻ͖ࠐΊΔϝϞாΛߟ͑Δ ʢߦͷ్தʹίϝϯτೖΕͯ΋ແࢹ͢Δʣ ಺༰Λॻ͚Δ ೖྗ಺༰ΛอଘͰ͖Δ ίϝϯτ͕ফ͑ͨঢ়ଶͰ಺༰͕ݟΕΔ

  34. ࣍อଘͱ֬ೝʢϝϞாͳͷͰʣ

  35. ͚ͩͲ%#Λ༻ҙ͢Δͷ͸݁ߏେมʜ ˠࠓճ͸ϝϞϦ্ʹอଘ͢ΔΑ͏࣮૷

  36. ͡Ό࣮͋ࡍ%#࢖͏Α͏ʹͳͬͨΒ ৭ʑͳॴஔ͖׵͑ͳ͖Ό͍͚ͳ͍ͷʁ ˠ%*ͷ࢓૊ΈΛ࢖ͬͯӬଓԽͷೖग़ྗͷϩδοΫ͚ͩ ஔ͖׵͑Ε͹ॅΉΑ͏ʹ࣮૷͢Δ

  37. package com.yachiy.spring.introduction.dao; public interface MemoDao { /** * ϝϞ಺༰ΛӬଓԽ͠ϝϞIDΛฦ٫͢Δ. *

    * @param content ಺༰ * @return ϝϞID */ int store(String content); /** * IDΛࢦఆͯ͠ϝϞ಺༰Λऔಘ͢Δ. * * @param id ϝϞID * @return ϝϞ಺༰ */ String findById(int id); } .FNP%BPKBWB ϝϞΛӬଓԽ࠶ߏ੒͢ΔͨΊͷΠϯλϑΣʔε
  38. package com.yachiy.spring.introduction.dao; import org.springframework.stereotype.Repository; import java.util.ArrayList; import java.util.List; @Repository public

    class InMemoryMemoDao implements MemoDao { private static final List<String> memoList = new ArrayList<>(); @Override public int store(String content) { memoList.add(content); return memoList.size(); } @Override public String findById(int id) { return memoList.get(id - 1); // 0࢝·ΓͳͷͰ-1 } } *O.FNPSZ.FNP%BPKBWB ϝϞϦ্Ͱ.FNP%BPͷڍಈΛ࣮ݱ͢Δ࣮૷Ϋϥε %*ίϯςφʹొ࿥
  39. package com.yachiy.spring.introduction.controller; import com.yachiy.spring.introduction.dao.MemoDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model;

    import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; @Controller public class MemoController { private MemoDao memoDao; @Autowired public MemoController(MemoDao memoDao) { this.memoDao = memoDao; } @GetMapping("/") public String memo() { return "memo"; } @PostMapping("/") public String receive(MemoForm memoForm, Model model) { int memoId = memoDao.store(memoForm.getContent()); model.addAttribute("memoId", memoId); return "received"; } @GetMapping("/{memoId}") public String view(@PathVariable int memoId, Model model) { String content = memoDao.findById(memoId); model.addAttribute("content", content); return "view"; } } .FNP$POUSPMMFSKBWB %FQFOEFODZ*OKFDUJPO ύε͔Β஋Λऔಘ
  40. <!doctype html> <html xmlns:th="http://www.w3.org/1999/xhtml"> <head> <meta charset="UTF-8"/> <title>݁Ռ</title> </head> <body>

    <h1>಺༰Λड͚औΓ·ͨ͠</h1> <div>ϝϞIDɿ</div> <div th:text="${memoId}"></div> </body> </html> SFDFJWFEIUNM <!doctype html> <html xmlns:th="http://www.w3.org/1999/xhtml"> <head> <meta charset="UTF-8"/> <title>಺༰</title> </head> <body> <div th:text="${content}"></div> </body> </html> WJFXIUNM
  41. !3FQPTJUPSZ ӬଓԽͷͨΊͷ࣮૷Λ%*ίϯςφʹొ࿥ ͢ΔͨΊͷΞϊςʔγϣϯ

  42. %*ίϯςφ %FQFOEFODZ*OKFDUJPO͍࣮ͨ͠૷Λొ࿥ ͓ͯ͘͠৔ॴ

  43. !"VUPXJSFE %*ίϯςφ͔Β౰ͯ͸·Δ࣮૷Λ஫ೖ͢Δ ͨΊͷΞϊςʔγϣϯ

  44. ʁʁʁ

  45. ԿΛ͍͔ͨ͠

  46. $POUSPMMFSͷཱ৔͔Β͢Δͱʜ  ˠड͚औͬͨϝϞͷ಺༰ͷӬଓԽΛґཔ͍ͨ͠ɻ ˠӬଓԽઌ͸%#Ͱ΋ϑΝΠϧͰ΋ϝϞϦ্Ͱ΋΄͍͠ͱ͖ʹऔΓग़ͤΕ͹ Ͳ͜Ͱ΋͍͍ɻ ˠ࣮૷ʹґଘͨ͘͠ͳ͍ ˠΠϯλϑΣʔε͚ͩ஌͍ͬͯͨ ˠద੾ͳ࣮૷Λউखʹࠩ͠ࠐΜͰ΄͍͠ ґଘੑٯసͷ๏ଇ

  47. ґଘੑΛٯసΠϯλϑΣʔεʹ౰ͯ͸·Δ࣮ ૷Λࠩ͠ࠐΉ ˠ%FQFOEFODZ*OKFDUJPO

  48. ͡Ό͋ԿΛࠩ͠ࠐΉ͔ ˠ%*ίϯςφʹొ࿥͞Ε͍ͯΔ΋ͷͷͳ͔͔Βͦ ͷΠϯλʔϑΣʔεΛ࣮૷͍ͯ͠Δ΋ͷΛࠩ͠ࠐΉ ͳͷͰಉ͡ΠϯλʔϑΣʔεΛ࣮૷͍ͯ͠Δ΋ ͷ͸͔̍ͭ͠%*ίϯςφʹొ࿥Ͱ͖ͳ͍

  49. ϝϞϦʹอଘ͢Δͷ͸΍Ίͯ .Z42-Λ࢖͍͍ͨ৔߹

  50. package com.yachiy.spring.introduction.dao; import org.springframework.stereotype.Repository; import java.util.ArrayList; import java.util.List; // @Repository

    public class InMemoryMemoDao implements MemoDao { private static final List<String> memoList = new ArrayList<>(); @Override public int store(String content) { memoList.add(content); return memoList.size(); } @Override public String findById(int id) { return memoList.get(id - 1); // 0࢝·Γͳͷ Ͱ-1 } } *O.FNPSZ.FNP%BPKBWB ίϝϯτΞ΢τ package com.yachiy.spring.introduction.dao; import org.springframework.stereotype.Repository; import java.util.ArrayList; import java.util.List; @Repository public class MySQLMemoDao implements MemoDao { @Override public int store(String content) { JdbcTemplate …. } @Override public String findById(int id) { JdbcTemplate …. } } .Z42-.FNP%BPKBWB ͪ͜ΒΛ%*ίϯςφʹొ࿥
  51. package com.yachiy.spring.introduction.controller; import com.yachiy.spring.introduction.dao.MemoDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model;

    import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; @Controller public class MemoController { private MemoDao memoDao; @Autowired public MemoController(MemoDao memoDao) { this.memoDao = memoDao; } @GetMapping("/") public String memo() { return "memo"; } @PostMapping("/") public String receive(MemoForm memoForm, Model model) { int memoId = memoDao.store(memoForm.getContent()); model.addAttribute("memoId", memoId); return "received"; } @GetMapping("/{memoId}") public String view(@PathVariable int memoId, Model model) { String content = memoDao.findById(memoId); model.addAttribute("content", content); return "view"; } } .FNP$POUSPMMFSKBWB มߋҰ੾ͳ͠
  52. ಈ͔ͯ͠ΈΔ

  53. CPPU3VOͯ͠MPDBMIPTUʹΞΫηε

  54. ԡԼ

  55. อଘ͞ΕͨϝϞͷ*%

  56. MPDBMIPTU\NFNP*E^ʹΞΫηε

  57. ϝϞͷ಺༰͕ग़ྗ͞Εͯ ͍Δ͜ͱΛ֬ೝ

  58. ೖग़ྗ͸0,

  59. ίϝϯτΛॻ͖ࠐΊΔϝϞாΛߟ͑Δ ʢߦͷ్தʹίϝϯτೖΕͯ΋ແࢹ͢Δʣ ಺༰Λॻ͚Δ ೖྗ಺༰ΛอଘͰ͖Δ ίϝϯτ͕ফ͑ͨঢ়ଶͰ಺༰͕ݟΕΔ

  60. ΍ͬͱۀ຿ϩδοΫ

  61. ࠓճͷۀ຿ ίϝϯτΛফ͢

  62. ίϝϯτΛফ͢λΠϛϯά͸ͲͪΒ͕ྑͦ͞͏͔ อଘ͢Δͱ͖ʹίϝϯτΛফ͔ͯ͠Βอଘ ͢Δ ੜσʔλΛอଘͯ͠ϝϞΛ֬ೝ͢Δͱ͖ʹ ίϝϯτΛফ͢

  63. ݕ౼

  64. อଘ࣌ʹίϝϯτΛফ͢ύλʔϯ ౎౓ίϝϯτΛফ͢ॲཧ͕ͳͯ͘ࡁΉʢελΠϧ ౰ͯΔͷ͸อଘ࣌ͷΈʣ ੜσʔλͷ֬ೝ͸ෆՄ औಘ࣌ʹίϝϯτΛফ͢ύλʔϯ ੜσʔλΛ֬ೝͰ͖Δ ֬ೝͷͨͼʹίϝϯτΛফ͢ඞཁ͕͋Δ

  65. ࣮ࡍʹۀ຿ͷࠓޙΛߟ͑Δͱʜ ฤूػೳཉ͘͠ͳΓͦ͏ ˠͦͷͱ͖ʹ͸ੜσʔλͰฤू͍ͨ͠ ˠίϝϯτΛফͯ͠อଘ͢Δͱͦͷ৘ใ͸ࣦΘ Εͯ͠·͏ ˠࠓճͷۀ຿Ͱ͸ফͨ͠ঢ়ଶͰอଘͯ͠͸͍͚ ͳ͍

  66. ͲͬͪͰ΋͍͍৔߹͸޷Έ ʢˍνʔϜͰͷܾΊ໰୊ʣ

  67. package com.yachiy.spring.introduction.service; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class

    CommentOutService { public String execute(String content) { List<String> lines = Arrays.asList(content.split("\r\n")); List<String> markUpList = lines.stream() .map(this::commentOut) .collect(Collectors.toList()); return String.join("\r\n", markUpList); } private String commentOut(String content) { if (content.matches("//.+")) { return ""; } return content; } } $PNNFOU0VU4FSWJDFKBWB
  68. package com.yachiy.spring.introduction.controller; import com.yachiy.spring.introduction.dao.MemoDao; import com.yachiy.spring.introduction.service.CommentOutService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller;

    import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; @Controller public class MemoController { private MemoDao memoDao; @Autowired public MemoController(MemoDao memoDao) { this.memoDao = memoDao; } @GetMapping("/") public String memo() { return "memo"; } @PostMapping("/") public String receive(MemoForm memoForm, Model model) { int memoId = memoDao.store(memoForm.getContent()); model.addAttribute("memoId", memoId); return "received"; } @GetMapping("/{memoId}") public String view(@PathVariable int memoId, Model model) { String content = memoDao.findById(memoId); model.addAttribute("content", new CommentOutService().execute(content)); return "view"; } } .FNP$POUSPMMFSKBWB ίϝϯτΞ΢τͷۀ຿ ϩδοΫΛݺग़
  69. CPPU3VOͯ͠MPDBMIPTUʹΞΫηε

  70. ԡԼ

  71. MPDBMIPTU\NFNP*E^ʹΞΫηε

  72. ίϝϯτ͕ফ͍͑ͯΔ͜ͱ Λ֬ೝ

  73. ૚ΞʔΩςΫνϟΛ࣮ݱͰ͖ͨ

  74. େࣄͳͷ͸֤૚ͷ໾ׂ͕ଞͷ૚ʹછΈग़͞ ͳ͍͜ͱ ಛʹۀ຿ϩδοΫ͸ଞͷ૚ʹછΈग़͕ͪ͠ ͳͷͰҙࣝͯ͠ઃܭ͢Δ