0. API 추가

# Maven

1. application.properties

연결 정보
# username
spring.datasource.hikari.username=user01
# password
spring.datasource.hikari.password=user01
# url
spring.datasource.url=jdbc:mariadb://localhost:3306/iu
# driver
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver

2. Mapper 파일 위치

1. DAO와 같은 패키지 내에 Mapper.xml 생성
	 - Mapper.xml의 위치를 알려 주지 않아도 됌
	 - DAO의 풀패키지명과 Mapper.xml의 namespace의 값과 같아야 함
	 - Mapper.xml의 id와 DAO의 메서드명과 일치 시켜야 함
	 - 단, Mapper파일의 이름과 DAO의 이름을 동일하게 생성
		 ex) NoticeMapper.xml , NoticeMapper.java
	- properties 파일에 Mapper의 위치를 작성하지 않아도 됨

2. src/main/resources 에 생성
	a. 폴더 생성
		src/main/resources > database 폴더 생성

	b. Mappers, config 폴더 생성 
		src/main/resources > database > mappers 폴더 생성 
		src/main/resources > database > config  폴더 생성

	c. properties 파일에 설정 
		## Mapper, Config 설정
		mybatis.mapper-locations=classpath:database/mappers/*Mapper.xml
		mybatis.config-location=classpath:database/config/config.xml

3. Type aliases 설정
   1) mybatis config 파일에 작성
   2) application.properties에 작성
      mybatis.type-aliases-package=base 패키지명
      Alias 를 대체
			단, 같은 class명이 있을 경우 error 발생

Mapper 작성

- SQL(Query)문을 작성하는 곳
- 각각의 DB의 Table 마다 mapper 하나씩 생성
- Table - Mapper - DAO

1. Mapper 작성방법
	1) xml 태그의 스키마가 있어야 함

		<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "<http://mybatis.org/dtd/mybatis-3-mapper.dtd>">

	2)  mapper 태그 작성과 namespace 속성
		- Mapper 파일이 여러개 생성, Mapper의 구분은 namespace의 값으로 구분 중복(x)
		- 값은 개발자 마음대로 작성
		 
		<mapper namespace="연결할 DAO의 FQDN(풀패키지명.클래스명)"></mapper>

	3) 쿼리 태그와 속성들
		- mapper 태그 내에 작성
		- 쿼리 태그 내에 쿼리문 작성(java 작성법과 동일)

			<select></select>
			<insert></insert>
			<update></updeate>
			<delete></delete>

		- 속성
		a. id     
			- mapper 내에서 다른 쿼리태그와 구분하는 용도, 중복(X), 개발자 마음대로 작성
			- 연결한 DAO의 메서드명과 동일하게 작성

		b. parameterType
			- ? 값을 넣어줄때 사용하는 Data의 DataType을 작성
			- FQDN(풀패키지명.클래스명) 

		c. resultType
			- SELECT 태그에만 있는 속성
			- SELECT의 결과물을 담을 데이터타입
			- SELECT의 결과물의 컬럼명과 DTO의 setter의 이름이 동일해야 함
			- SELECT의 결과물(row)이 0개든, 1개든 , N개든 resultType은 DTO
	
		4) parameter 처리
			 - #{}
			 - DTO type이면 #{getter의 이름} 
			 - Wrapper type(Primitive Type) 또는 String Type 하나라면 #{DAO에서 넘겨주는 변수명}
			 - Getter Method가 없다
			 - #{}는 Data만 가능, 테이블명이나, 컬럼명 같은 것들은 사용 X 

			 - parameter는 한개든 여러개든 DTO에 담아서 전송

			 - Map , List
					-- Map  : #{key} 
					-- List : #{index번호}

		

2. DAO 작성법

	1) 멤버변수로 SqlSession을 선언하고 주입 : database-context.xml에 생성한 객체
	2) 멤버변수로 String namespace 선언 대입 : 연결하려는 mapper의 namespace속성의 값과 동일한 값, 끝에 . 추가
	3) 멤버메서드 생성
		 sqlSession.쿼리메서드명(namespace+"id명")             : Mapper 보내야하는 parameter가 없을 때
	   sqlSession.쿼리메서드명(namespace+"id명", 파라미터값)  : Mapper 보내야하는 parameter가 있을 때
		 **- 파라미터값은 딱 1 개만 가능, 2개 이상 보낼 수 없음**
		
		 a. select
		 sqlSession.selectOne()     : select의 결과물이 1개 일때 사용
		 sqlSession.selectList()    : select의 결과물이 N개 일때 사용 - List<> 타입으로 반환

			b. insert
			sqlSession.insert()       : int 가 반환

			c. update
			sqlSession.update()       : int 가 반환  

			d. delete
			sqlSession.delete()       : int 가 반환  

3. 동적 SQL
	- 제어문 제공

	1) 단일 if
		<if test="조건식"> </if>

	2) 여러개중 하나 선택
		<choose>
				<when test="조건식"></when>
				<when test="조건식"></when>
				<when test="조건식"></when>
				<otherwise></otherwise>
		</choose>
		
	3) foreach
			a. 속성들
				- collection : List 나 Array 형태만 가능
				- item       : collection에서 하나 꺼낸것을 담을 변수(alias)
				- open       : 구문이 시작할때 넣어주는 문자열
				- close      : 구문이 끝날 때 넣어주는 문자열
				- separator  : 반복 되는 사이에 넣어주는 문자열
				- index      : 0부터 시작하는 인덱스 번호
				
				
			b. parameter 적용
				1) List나 배열을 Map에 담아서 전송 했을 경우
				
				<foreach collection="Map의 키">
				
				2) 배열을 직접 전송했을 경우
					int [] ar = {1,2,3}
					sqlSession.delete("", ar)
					collection을 꼭 array로 작성
				<foreach collection="array">
				
				3) List를 직접 전송했을 경우
					 List<Integer> ar =...
					 sqlSession.delete("", ar)
					 collection을 꼭 list로 작성
	 				 <foreach collection="list">
	
	
4. ResultMap
	- SELECT의 결과를 ResultType이 아니라 ResultMap에서 처리
	- SELECT의 결과의 컬럼명과 DTO의 변수명이 다를 때 사용
	- Join의 결과를 처리할 때 사용

	<select id="" parameterType="" resultMap="결과를 처리할 resultmap의 ID">
		join query문
	</select>

	<resultMap type="최종 return할 DTO" id="id명">
			<id column="select결과물의 컬럼명" property="retun할 DTO의 멤버변수명(setter 이름)"></id>   <!-- PK로 사용하는 column -->
			<result column="select결과물의 컬럼명" property="retun할 DTO의 멤버변수명(setter 이름)"></result>   <!-- PK를 제외한 나머지 -->
			
			<!-- 1:1 관계 association-->
			<association property="retun할 DTO의 멤버변수명(setter 이름)" javaType="멤버변수의 Data type">
				<id column="select결과물의 컬럼명" property="retun할 DTO의 멤버변수명(setter 이름)"></id>   <!-- PK로 사용하는 column -->
				<result column="select결과물의 컬럼명" property="retun할 DTO의 멤버변수명(setter 이름)"></result>   <!-- PK를 제외한 나머지 -->
	 		</association>

			<!-- 1:N 관계 collection -->
			<collection property="retun할 DTO의 멤버변수명(setter 이름)" javaType="멤버변수의 Data type" ofType="Generic Data type">
				<id column="select결과물의 컬럼명" property="retun할 DTO의 멤버변수명(setter 이름)"></id>   <!-- PK로 사용하는 column -->
				<result column="select결과물의 컬럼명" property="retun할 DTO의 멤버변수명(setter 이름)"></result>   <!-- PK를 제외한 나머지 -->
	 		</collection >

	</resultMap>

DB의 SnakeCase 를 Java의 Camel Case와 Mapping

1. DB query 작성시 별칭 사용

2. application.properties에 설정 
  mybatis.configuration.map-underscore-to-camel-case=true
  기본값은 false

3. DAO, VO, Mapper

DAO

@Mapper
public interface QnaMapper {
	
	public List<QnaVO> getList()throws Exception;

}