본문 바로가기

Programming/Mybatis

[Mybatis]insert 구문 실행 후 selectKey 값이 1이 나오는 현상

 

기존 repository 소스코드 

@Repository public class AttachFileRepository4Mybatis {

@Autowired private SqlSessionTemplate sessionTemplate;

public List<Long> insertList(List<AttachFileRequestDTO> attachFiles) throws DataAccessException {

List<Long> attachFileIds = new ArrayList<>();

for (AttachFileRequestDTO attachFileRequestDTO : attachFiles) {

Long result = Long.valueOf(sessionTemplate.insert("attachFile.insert", attachFileRequestDTO)); attachFileIds.add(result);

}

return attachFileIds;

}

}

수정된 소스코드 

@Repository
public class AttachFileRepository4Mybatis {
    @Autowired
    private SqlSessionTemplate sessionTemplate;

    public List<Long> insertList(List<AttachFileRequestDTO> attachFiles) throws DataAccessException {
        List<Long> attachFileIds = new ArrayList<>();
        for (AttachFileRequestDTO attachFileRequestDTO : attachFiles) {
            sessionTemplate.insert("attachFile.insert", attachFileRequestDTO);
            Long attachFileId = attachFileRequestDTO.getAttachFileId();
            attachFileIds.add(attachFileId);
        }
        return attachFileIds;
    }
}

 

 

참고 

insert mapper

<mapper namespace="attachFile">

    <insert id="insert">
        <selectKey resultType="long" keyProperty="attachFileId" order="BEFORE">
            SELECT SEQ_ATTACH_FILE_ID.NEXTVAL AS attachFileId FROM DUAL
        </selectKey>
        INSERT INTO ATTACHFILE
        (ATTACHFILEID
        , ATTACHFILETYPE
        , CREATEDATE
        , FILESIZE
        , FILEUPLOADPATH
        , ORIGINFILENAME
        , S3BUCKETPATH
        , TEMPFILENAME
        ) VALUES (
        #{attachFileId}
        , #{attachFileType, jdbcType=VARCHAR}
        , SYSDATE
        , #{fileSize, jdbcType=NUMERIC}
        , #{fileUploadPath, jdbcType=VARCHAR}
        , #{originFileName, jdbcType=VARCHAR}
        , #{s3BucketPath}
        , #{tempFileName, jdbcType=VARCHAR}
        )
    </insert>

</mapper>

 

 

dto 객체 

 

@Data
@Getter
@Setter
public class AttachFileRequestDTO {

    private Long attachFileId;

    private String attachFileType;

    private String fileUploadPath;

    private String originFileName;

    private String tempFileName;

    private Long fileSize;

    private String s3BucketPath;

}
728x90