IBatis에서 Insert 후에 자동 생성 된 PK 값을 리턴받아 처리하는 코드 샘플이다.
--- User Table ---
create table T_USER (
user_id INT NOT NULL AUTO_INCREMENT,
login_id varchar(20) NOT NULL,
login_pwd varchar(20) NOT NULL,
user_name varchar(20),
cell_phone varchar(20),
address varchar(100),
email varchar(100),
PRIMARY KEY (user_id),
UNIQUE login_id (login_id)
);
--- UserSqlMap.xml ---
<insert id="insert" parameterClass="amf1.model.User">
insert into T_USER (login_id, login_pwd, user_name, cell_phone, address, email)
values (#loginId:VARCHAR#, #loginPwd:VARCHAR#, #userName:VARCHAR#,
#cellPhone:VARCHAR#, #address:VARCHAR#, #email:VARCHAR#)
<selectKey keyProperty="userId" resultClass="int">
SELECT LAST_INSERT_ID()
</selectKey>
</insert>
---- UserDaoImpl.java ---
@Override
public int insertWithoutPK(User record) {
Integer userId = (Integer) getSqlMapClientTemplate().insert("T_USER.ibatorgenerated_insert",
record);
record.setUserId(userId);
return userId;
}
댓글 없음:
댓글 쓰기