Java/mybatis

Mybatis insert후 키값 반환하기

바리새인 2024. 12. 27. 16:51

쿼리

<insert id="create" parameterType="map">
    <selectKey resultType="String" keyProperty="id" order="BEFORE">
        SELECT to_char(current_timestamp,'yyyymmdd')||lpad(nextval('seq_test')::TEXT, 3, '0')
    </selectKey>
    INSERT INTO test(
        id, name
    )
    VALUES (
        #{id}, #{name}
    )
</insert>

서비스

public String create(Map<String, Object> attach) {
    testMapper.create(attach);
    return attach.get("id").toString(); // Retrieve pre-generated ID
}