스프링4.x 이 나왔지만 아직까지 제대로 검토를 못해봐서… 3.x까지만.. 확인.
롬복사용한 도메인포조DomainPOJO with lombok
1 2 3 4 5 6 7 8 9 10 |
@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
1 2 3 |
public interface LogMapper { int insertEventLog(EventLog eventLog); } |
매퍼xml Mapper xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?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"> <mapper namespace="mapper.LogMapper"> <cache /> <!-- <insert id="insertEventLog" parameterType="EventLog" > --> <insert id="insertEventLog" parameterType="EventLog" useGeneratedKeys="true" keyProperty="eventLogId"> INSERT INTO event_log( event_result_status_code, description, event_info_id )VALUES( #{eventResultStatusCode}, #{description}, #{eventInfoId} ) </insert> </mapper> |
테스트코드 Junit TestCode
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
@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로 값을 확인할 수 있다.
기존 코드에서 몇 라인정도만 수정해주면 된다.