본문 바로가기

DB

[SQL] MyBatis 에서 insert 시 자동 생성키 - MySQL

728x90

1. table 구조

create table user(
	userIdx int not null AUTO_INCREMENT,
	name varchar(10) not null,
	email varchar(50) not null,
	pw varchar(50) not null,
	pwCheck varchar(50) not null,
	tel int not null,
	address varchar(500) not null,
	checkAll boolean not null,
	PRIMARY KEY (userIdx)
);

 

 

 

2. 사용법

 

- repository 

@Insert("INSERT INTO dbelle.user (name, email, pw, pwCheck, tel, address, checkAll) VALUES (#{name}, #{email}, #{pw}, #{pwCheck}, #{tel}, #{address} , #{checkAll})")
@Options(useGeneratedKeys = true, keyProperty = "userIdx")
void addUserinfo(User user);

 

- xml

 <insert id="addUserinfo" parameterType="com.dbellart.web.domain.User" useGeneratedKeys="true"  keyProperty="userIdx">
 	<![CDATA[
		insert into dbelle.USER (userIdx, name, email, pw, pwCheck, tel, address, checkAll)
		values (#{name}, #{email}, #{pw}, #{pwCheck}, #{tel}, #{address} , #{checkAll})
		]]>
</insert>
728x90