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

D98762c822b683773188e826a00d243e?s=47 yachiy
November 14, 2019
41

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

D98762c822b683773188e826a00d243e?s=128

yachiy

November 14, 2019
Tweet

Transcript

  1. 10.
  2. 11.
  3. 14.
  4. 18.
  5. 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
  6. 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ͷ݁ՌΛड͚औΔϝιουΛ ௥Ճ
  7. 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
  8. 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
  9. 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ͷϑΟʔϧυ໊ͱҰகͤ͞Δ
  10. 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 ϝϞΛӬଓԽ࠶ߏ੒͢ΔͨΊͷΠϯλϑΣʔε
  11. 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ͷڍಈΛ࣮ݱ͢Δ࣮૷Ϋϥε %*ίϯςφʹొ࿥
  12. 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 ύε͔Β஋Λऔಘ
  13. 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
  14. 44.
  15. 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 ͪ͜ΒΛ%*ίϯςφʹొ࿥
  16. 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 มߋҰ੾ͳ͠
  17. 54.
  18. 63.
  19. 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
  20. 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 ίϝϯτΞ΢τͷۀ຿ ϩδοΫΛݺग़
  21. 70.