후위표기법 postfix
후위표기법이란 연산자를 피연산자 두개의 뒤쪽에 표기해 수식을 표현하는 방식이다.
숫자를 앞에 기호를 뒤에 표기한다.
연산자를 만났을 때 직전 두개의 숫자를 가지고 계산하면 된다.
1. 숫자 만나면 스택에 push.
2. 연산자 만나면 pop 두번한 결과 가지고 계산. (이 때 먼저 pop된 숫자가 오른쪽 피연산자)
3. 계산 결과를 다시 스택에 push.
ex) 5 + 3 * 1 일 경우
--> 5 3 1 * +
--> 5 (3 1 *) +
--> 5 3 +
=> 8
public void solution() throws IOException {
// 입력 처리
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String input = reader.readLine();
Stack<Integer> digitStack = new Stack<>();
for (int i = 0; i < input.length(); i++) {
char token = input.charAt(i);
// 1. 숫자라면, 스택에 push한다.
// Charater.isDigit(token) // token이 숫자가 표현된 글자인지 판단하는 메소드
// token을 int로 변환 => token - "0"
if (Character.isDigit(token)) {
digitStack.push(token - '0');
}
// 2. 숫자가 아니라면, (연산자) 스택에서 두번 pop하고 계산한다.
else if (!Character.isDigit(token)) {
int numRight = digitStack.pop();
int numLeft = digitStack.pop();
if (token == '+')
digitStack.push(numLeft + numRight);
else if (token == '-')
digitStack.push(numLeft - numRight);
else if (token == '*')
digitStack.push(numLeft * numRight);
else if (token == '/')
digitStack.push(numLeft / numRight);
}
else throw new IllegalArgumentException("invalid operator");
}
// System.out.println(digitStack.pop());
int answer = digitStack.pop();
if (digitStack.isEmpty())
System.out.println(answer);
else System.out.println("error");
}
깊이 우선 탐색 DFS
그래프에서 한쪽 갈림길을 택하며 갈 수 있는 최대한 깊이 들어가며 탐색하는 방법이다.
- 더 이상 갈 곳이 없을 때까지 길을 따라간다.
- 막다른 길이 나오면 마지막으로 갈림길이 나왔던 지점으로 돌아간다.
- 마지막 지점 -> 후입선출 -> 스택
정점 Node, Vertex
데이터가 존재하는 점
간선 Edge
정점을 연결해주는 선
MyBatis
MyBatis는 java 프로젝트에서 jdbc를 쓰지않고 더 편하게 접근할 수 있게 해주는 프레임워크로
jdbc를 사용하는 방법을 높은 수준에서 추상화한 것이다.
jdbc를 통해 db에 연결할 경우 statement를 만들고, setString 등으로 SQL 내부를 채워줘야 했지만
MyBatis를 사용하면 위와 같은 과정이 필요하지 않다.
MyBatis는 interface 메소드에 SQL을 연결하여 메소드 호출 시 SQL이 실행되며
인자와 결과를 Java 객체로서 활용이 가능하다.
MyBatis 설정
MyBatis의 db 관련 설정을 application.properties 또는 application.yaml에 추가해주는데
둘의 차이점은
properties : key-value 형태
yaml : 계층구조로 이뤄져 가독성 good
된다는 점이 다르다.
application.properties
spring.datasource.url=jdbc:sqlite:db.sqlite
spring.datasource.driver-class-name=org.sqlite.JDBC
application.yaml
spring:
datasource:
url: jdbc:sqlite:db.sqlite
driver-class-name: org.sqlite.JDBC
username: sa
password: password
mybatis:
mapper-locations: "classpath:mybatis/mappers/*.xml"
type-aliases-package: "com.example.mybatis.model"
configuration:
map-underscore-to-camel-case: true
요즘은 yaml이 구조와 계층이 잘보이기 때문에 더 많이 사용한다고 한다.
spring.datasource.url : 데이터베이스에 접속하기 위한 URL.
spring.datasource.driver-class-name : 데이터베이스를 사용할 때 사용할 JDBC 드라이버.
spring.datasource.username / password : 데이터베이스의 접속 정보를 작성하는 곳
mybatis.mapper-locations : SQL이 정의된 mapper.xml 파일의 위치
mybatis.type-aliases-package : 위에서 언급한 xml 파일에서 사용할 java 클래스의 패키지
mybatis.configuration.map-underscore-to-camel-case : snake_case와 camelCase 간 자동 변환
'멋쟁이 사자처럼 > TIL' 카테고리의 다른 글
230615 9주 4일차 TIL. JPA 복습, create, readAll (0) | 2023.06.18 |
---|---|
230614 TIL. Spring Streotypes (Beans) (0) | 2023.06.14 |
230612 TIL. Stack, Call Stack, 괄호 검사, lombok (0) | 2023.06.12 |
230609 8주 4일차 TIL. @PathVariable, Thymeleaf th:, @{} (0) | 2023.06.11 |
230607 8주 2일차 TIL. 빌드 자동화 도구, Maven, Gradle (0) | 2023.06.07 |