닭발개발

[MyBatis] useGeneratedKeys, keyProperty 본문

Spring

[MyBatis] useGeneratedKeys, keyProperty

D269 2023. 3. 3. 13:58
728x90

useGeratedKeys 와 keyProperty

항상 이 두개는 함께 사용한다.

방금 insert한 auto increment가 걸린 pk값을 바로 사용하고 싶을 떄 사용한다.

  • useGeneratedKeys : insert나 update됨가 동시에 자동생성된 키를 가져올 수 있는 속성으로 true로 설정 (default: false)
  • keyProperty : 리턴 될 key property 설정. 즉 values나 set 다음에 오는 컬럼명과 동일하게 설정하면 됨. 여러개를 사용한다면 ,(콤마)를 구분자로 설정

 

public exampleVO {
  int userCode;
  String name;
  String email;
}

 

<insert id="insertExample" useGeneratedKeys="true" keyProperty="userCode" parameterType="exampleVO"> 
	INSERT INTO example 
		   (name, email ) 
	VALUES 
		   (#{name}, #{email} ) 
</insert>

 

userCode가 자동 증감되어 DB에 등록된다.
따로 set해주지 않더라도 exampleVO.getUserCode()를 하면 autoIncrement된 값을 얻을 수 있다!!
insert 나 update return받을 때 주로 사용할 수 있다.

 

 

 

 

 

 

 

출처 : https://sowon-dev.github.io/2021/07/26/210727MyBatis-keyProperty/

728x90
반응형