AuthorMapper.xml
, AuthorMapper.java
AuthorService.java
, AuthorServiceImpl.java
AuthorVO.java
작성AuthorMapper.java
인터페이스AuthorMapper.xml
AuthorMapper.java
메서드 테스트AuthorService.java
인터페이스AuthorServiceImpl.java
alter table author add regDate timestamp default now();
alter table author add updateDate timestamp default now();
delete from author where authorId in ('1', '2', '3');
alter table author auto_increment = 1;
insert into author(authorName, nationId, authorIntro) values('유홍준', '01', '작가 소개입니다' );
insert into author(authorName, nationId, authorIntro) values('김난도', '01', '작가 소개입니다' );
insert into author(authorName, nationId, authorIntro) values('폴크루그먼', '02', '작가 소개입니다' );
com.store.model
패키지에 생성하여 작성한다.getter
, setter
, toString
작업을 한다. Lombok 사용자는 @Data 어노테이션만 붙이면 된다.package com.store.model;
import java.util.Date;
public class AuthorVO {
/* 작가 아이디 */
private int authorId;
/* 작가 이름 */
private String authorName;
/* 국가 id */
private String nationId;
/* 작가 국적 */
private String nationName;
/* 작가 소개 */
private String authorIntro;
/* 등록 날짜 */
private Date regDate;
/* 수정 날짜 */
private Date updateDate;
public int getAuthorId() {
return authorId;
}
public void setAuthorId(int authorId) {
this.authorId = authorId;
}
public String getAuthorName() {
return authorName;
}
public void setAuthorName(String authorName) {
this.authorName = authorName;
}
public String getNationId() {
return nationId;
}
public void setNationId(String nationId) {
this.nationId = nationId;
}
public String getNationName() {
return nationName;
}
public void setNationName(String nationName) {
this.nationName = nationName;
}
public String getAuthorIntro() {
return authorIntro;
}
public void setAuthorIntro(String authorIntro) {
this.authorIntro = authorIntro;
}
public Date getRegDate() {
return regDate;
}
public void setRegDate(Date regDate) {
this.regDate = regDate;
}
public Date getUpdateDate() {
return updateDate;
}
public void setUpdateDate(Date updateDate) {
this.updateDate = updateDate;
}
}
insert into author(authorName, nationId, authorIntro) values('작가이름', '나라코드', '작가소개' );
com.store.mapper
패키지에 AuthorMapper.java
인터페이스를 생성하였다.package com.store.mapper;
import com.store.model.AuthorVO;
public interface AuthorMapper {
/* 작가 등록 */
public void authorEnroll(AuthorVO author);
}
src/main/resources/com/store/mapper
경로에 AuthorMapper.java
인터페이스와 동일한 이름의 AuthorMapper.xml
파일을 생성한다.AuthorMapper.xml
다음과 같이 작성한다. namespace 속성 값에 AuthorMapper.java
의 경로를 포함한 동일한 파일 이름이 작성되어야 한다.<?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="com.store.mapper.AuthorMapper">
</mapper>
<inset>
태그를 추가해준다. id속성을 추가하고 속성 값은 AuthorMapper.java
에서 작성한 작가 등록 기능을 수행하는 메서드명과 동일한 이름을 작성한다.<inset>
태그 내에는 위의 insert문 테스트 에서 테스트했던 insert문을 작성한다.AuthorVO
에서 정의된 변수가 호출될 수 있도록 순서에 맞게 AuthorVO
의 변수 이름과 동일한 변수명을 #{}을 붙여서 작성한다.<!-- 작가 등록 -->
<insert id="authorEnroll">
insert into author(authorName, nationId, authorIntro) values(#{authorName}, #{nationId}, #{authorIntro});
</insert>
src/test/java
경로에 있는 com.store.mapper
패키지에 AuthorMapperTests.java
클래스를 생성한 후 앞서 우리가 만든 AuthorMapper.java
인터페이스의 authorEnroll
메서드가 정상적으로 작동하는지 테스트 한다.package com.store.mapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.store.model.AuthorVO;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/root-context.xml")
public class AuthorMapperTests {
@Autowired
private AuthorMapper mapper;
/* 작가 등록 테스트 */
@Test
public void authorEnroll() throws Exception {
AuthorVO author = new AuthorVO();
author.setNationId("01");
author.setAuthorName("테스트");
author.setAuthorIntro("테스트 소개");
mapper.authorEnroll(author);
}
}
com.store.service
패키지에 AuthorService.java
인터페이스를 생성하고 작가등록 메서드를 작성한다.package com.store.service;
import com.store.model.AuthorVO;
public interface AuthorService {
/* 작가 등록 */
public void authorEnroll(AuthorVO author) throws Exception;
}
com.store.service
패키지에 AuthorService.java
인터페이스를 구현하는 클래스인 AuthorServiceImpl.java
클래스를 생성 후 상속 키워드를 추가한다.package com.store.service;
public class AuthorServiceImpl implements AuthorService{
}
@Service
어노테이션을 추가한다.package com.store.service;
import org.springframework.stereotype.Service;
@Service
public class AuthorServiceImpl implements AuthorService{
}
AuthorMapper.java
인터페이스를 주입해주는 코드를 작성한다.package com.store.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.store.mapper.AuthorMapper;
@Service
public class AuthorServiceImpl implements AuthorService{
@Autowired
AuthorMapper authorMapper;
}
AuthorService.java
에서 정의한 작가등록 메서드를 오버라이딩 하여 AuthorMapper
의 작가 등록 메서드를 호출하는 코드를 작성한다.package com.store.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.store.mapper.AuthorMapper;
import com.store.model.AuthorVO;
@Service
public class AuthorServiceImpl implements AuthorService{
@Autowired
AuthorMapper authorMapper;
@Override
public void authorEnroll(AuthorVO author) throws Exception {
authorMapper.authorEnroll(author);
}
}