//////
Search
📓

11/8 회고록

생성일
2022/11/08 08:28
태그

이가현

게시판CRUD

JPA 연결
@GeneratedValue : 기본 키를 자동 생성해주는 어노테이션
주 키 생성 전략 네 가지 - 디폴트는 auto 1. AUTO : (persistence provider가) 특정 DB에 맞게 자동 선택
2.
IDENTITY: DB의 identity 컬럼을 이용
3.
SEQUENCE: DB의 시퀀스 컬럼을 이용
4.
TABLE: 유일성이 보장된 데이터베이스 테이블을 이용
연결 - redirect
서버(웹 컨테이너)가 클라이언트(웹 브라우저)에게 알려준 주소로 서버에 재요청하는 것
서버가 클라이언트에게 재요청하라고 응답하는 것이므로 response로 보냄
웹 컨테이너 외부의 주소로도 이동 가능
URL이 바뀐다.
request와 response 객체가 새롭게 생성된다.
시스템에 변화가 생기는 경우(생성, 수정, 삭제) 재요청을 방지하기 위해 사용한다.
redirect시 값을 전달하려면 GET 방식으로 보내야 한다. 요청 객체가 새로 생성되기 때문에 값을 보내려면 URL과 같이 보내야 하기 때문이다.
Spring에서는 RedirectAttribute를 사용하여 값을 보낼 수도 있다. 이 방법은 내부적으로 세션을 사용한다.
@GetMapping("") public String index(){ return "redirect:/articles/list"; }
Java
복사

임학준

알고리즘(이진탐색 BinarySearch)

가운데 있는 항목을 키값과 비교하여 이진 탐색을 반복적으로 수행한다
시간복잡도가 O(log n)으로 데이터 증가율에 비해 연산횟수의 증가율이 낮다
탐색알고리즘(O(n))이 순차적으로 탐색하는데에 비해 속도가 빠르다
배열의 중간값에서부터 찾는 값이 큰지 작은지 비교하여 찾는다
그러므로 정렬이 되있어야 한다
public class BinarySearch { public static void main(String[] args) { int[] nums = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; int find = 5; //찾는 값 int left = 0; //시작점 int right = nums.length - 1; //끝점 , 배열은 0부터 시작하므로 -1을 한다 int middle; //중간값 while (right >= left) {//값을 찾을때까지 반복문 middle = (right + left) / 2; //중간값을 찾는다 if (find == nums[middle]) { System.out.println(middle); //4를 출력 == find는 4번째 index에 있다 break; } if (find < nums[middle]) right = middle - 1; //끝점 옮기기 else left = middle + 1; //시작점 옮기기 } } }
Java
복사

Spring mvc

지난 수업 주요내용
Form에서 전송한 Data를 Post Controller에서 받기
jpa적용
bootstrap적용
오류상황

조국현

최아영

알고리즘 실습

[CodeUp] 3001 : 데이터 탐색
문제 설명
n개의 데이터가 있을 때, 특정 데이터가 어떤 위치에 있는지 찾는 것이 문제이다. 이 특정 데이터를 찾는 방법의 가장 기본적인 방법으로 선형탐색이 있다. 이는 데이터 셋에서 처음부터 차례대로 찾는 방법이다.
문제 풀이
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } int k = sc.nextInt(); int answer = -1; for (int i = 0; i < n; i++) { if (arr[i] == k) { answer = i+1; } } System.out.println(answer); } }
Java
복사
[CodeUp] 2083 : 이분 탐색
문제 설명
n개로 이루어진 정수 집합에서 원하는 수의 위치를 찾으시오. 단, 입력되는 집합은 오름차순으로 정렬되어 있으며, 같은 수는 없다.
문제 풀이
import java.util.Scanner; public class BinarySearch { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int s = sc.nextInt(); int[] arr = new int[n]; for (int i=0; i<n; i++) { arr[i] = sc.nextInt(); } int start = 0; int end = arr.length; int answer = -1; while (start <= end) { int mid = (start + end) / 2 ; if (s == arr[mid]) { answer = mid +1; break; } else if (s < arr[mid]) { end = mid - 1; } else { start = mid + 1; } } System.out.println(answer); } }
Java
복사

Spring MVC

조회 기능
id 조회
Controller에 /articles/{id} 연결한다.
id를 받아 select해서 Aritlce로 받아온다. → Model
받아온 데이터를 화면에 출력한다.
<ArticleController>
@GetMapping("/{id}") public String selectArticle(@PathVariable Long id, Model model) { Optional<Article> optArticle = articleRepository.findById(id); if (!optArticle.isEmpty()) { model.addAttribute("article", optArticle.get()); return "articles/show"; } else { return "articles/error"; } }
Java
복사
<show.mustache>
{{>layouts/header}} <table class="table"> <thead> <tr> <th scope="col">Id</th> <th scope="col">Title</th> <th scope="col">Content</th> </tr> </thead> <tbody class="table-group-divider"> {{#article}} <tr> <th>{{id}}</th> <td>{{title}}</td> <td>{{content}}</td> </tr> {{/article}} </tbody> </table> <a href="/articles">list article</a> {{>layouts/footer}}
HTML
복사
전체 조회
모든 데이터를 가져온다.
받아온 데이터를 화면에 출력한다.
<ArticleController>
@GetMapping("") public String index() { return "redirect:/articles/list"; } @GetMapping("/list") public String listArticle(Model model) { List<Article> articles = articleRepository.findAll(); model.addAttribute("articles", articles); return "articles/list"; }
Java
복사
<list.mustache>
{{>layouts/header}} <table class="table"> <thead> <tr> <th scope="col">Id</th> <th scope="col">Title</th> <th scope="col">Content</th> </tr> </thead> <tbody class="table-group-divider"> {{#articles}} <tr> <th>{{id}}</th> <td><a href="/articles/{{id}}">{{title}}</a></td> <td>{{content}}</td> </tr> {{/articles}} </tbody> </table> <a href="/articles/new">new article</a> {{>layouts/footer}}
HTML
복사
<ArticleController>
@GetMapping("") public String index() { return "redirect:/articles/list"; } @GetMapping("/list") public String listArticle(Model model) { List<Article> articles = articleRepository.findAll(); model.addAttribute("articles", articles); return "articles/list"; }
Java
복사
<list.mustache>
{{>layouts/header}} <table class="table"> <thead> <tr> <th scope="col">Id</th> <th scope="col">Title</th> <th scope="col">Content</th> </tr> </thead> <tbody class="table-group-divider"> {{#articles}} <tr> <th>{{id}}</th> <td><a href="/articles/{{id}}">{{title}}</a></td> <td>{{content}}</td> </tr> {{/articles}} </tbody> </table> <a href="/articles/new">new article</a> {{>layouts/footer}}
HTML
복사