Spring3.x with Mybatis 3.x insert시에 generated key 받아오기

less than 1 minute read

스프링4.x 이 나왔지만 아직까지 제대로 검토를 못해봐서… 3.x까지만.. 확인.

롬복사용한 도메인포조DomainPOJO with lombok

@Data public class EventLog { private Long eventLogId; private String eventResultStatusCode; private Timestamp createdAt; private String description; private Long eventInfoId; //OCCURRENCE, CONFIRM, RESOVED private String eventStatus; }

매퍼인터페이스 Mapper interface

public interface LogMapper { int insertEventLog(EventLog eventLog); }

매퍼xml Mapper xml

<?xml version=”1.0” encoding=”UTF-8”?> <!DOCTYPE mapper PUBLIC “-//mybatis.org//DTD Mapper 3.0//EN” “http://mybatis.org/dtd/mybatis-3-mapper.dtd”>

INSERT INTO event\_log( event\_result\_status\_code, description, event\_info\_id )VALUES( #{eventResultStatusCode}, #{description}, #{eventInfoId} )

테스트코드 Junit TestCode

@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration( locations = { “classpath*:spring-context.xml” } ) public class LogInsertTest {

@Autowired
LogMapper logMapper;

@Test
public void test(){
	EventLog eventLog = new EventLog();
	eventLog.setEventResultStatusCode("200");
	eventLog.setDescription("desc");
	eventLog.setEventInfoId(54L);
	
	int result = logMapper.insertEventLog(eventLog);
	System.out.println(result);
	System.out.println(eventLog);
} }

리턴값은 적용된 쿼리의 로우수

call by reference로 값을 확인할 수 있다.

기존 코드에서 몇 라인정도만 수정해주면 된다.

Categories:

Updated: