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

Springで始める3層アーキテクチャ / start three layer archite...

yachiy
November 14, 2019
95

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

yachiy

November 14, 2019
Tweet

Transcript

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