====== MyBatis 적용 가이드 ====== ===== 개요 ===== 전자정부 표준프레임워크 기반 MyBatis 적용 가이드이다. {{:egovframework:rte2:psl:dataaccess:ex-dataaccess-mybatis.zip|}} ===== Step 1. pom.xml 변경 ====== 표준프레임워크 dataaccess artifact version을 다음과 같이 2.7.0으로 변경한다. egovframework.rte egovframework.rte.psl.dataaccess 2.7.0 ===== Step 2. 환경 설정 ====== ==== Step 2.1 XML 설정 ==== Spring XML 설정 파일 상(ex: context-mapper.xml)에 다음과 같은 sqlSession bean 추가한다. Ex) context-mapper.xml ==== Step 2.2 mapper xml 작성 ==== MyBatis 가이드에 따라 query xml 작성한다. (2.1의 예제 상 지정된 mapperLocations 위치) ===== Step 3. DAO 작성 ====== DAO의 경우는 다음과 같이 3가지 방식이 가능하다. ^ 방식 ^ 설명 ^ 비고 ^ | 기존 DAO 클래스 방식 | @Repository 지정 및 EgovAbstractMapper extends 활용 | 기존 iBatis와 같은 방식 | | Mapper interface 방식 | Mapper 인터페이스 작성 및 @Mapper annotation 지정 | @Mapper는 marker annotation(표준프레임워크 제공) | | Annotation 방식 | query xml 없이 mapper 인터페이스 상 @Select, @Insert 등을 활용 | Dynamic SQL 등의 사용에 제약이 있음 | ==== 3.1 기존 DAO 형태로 사용하는 경우 ==== @Repository 지정된 class가 EgovAbstractMapper를 extends 하여 insert, update, delete, selectByPk, list 메소드를 활용한다. @Repository("deptMapper") public class DeptMapper extends EgovAbstractMapper { public void insertDept(String queryId, DeptVO vo) { insert(queryId, vo); } public int updateDept(String queryId, DeptVO vo) { return update(queryId, vo); } public int deleteDept(String queryId, DeptVO vo) { return delete(queryId, vo); } public DeptVO selectDept(String queryId, DeptVO vo) { return (DeptVO)selectByPk(queryId, vo); } @SuppressWarnings("unchecked") public List selectDeptList(String queryId, DeptVO searchVO) { return list(queryId, searchVO); } } ==== 3.2 Mapper interface 사용 방식 ==== Mapper 인터페이스 작성 시 다음과 같이 @Mapper annotation 사용한다. (패키지를 포함하는 클래스명 부분이 mapper xml 상의 namespace가 선택되고 인터페이스 메소드가 query id로 호출되는 방식) @Mapper("employerMapper") public interface EmployerMapper { public List selectEmployerList(EmpVO vo); public EmpVO selectEmployer(BigDecimal empNo); public void insertEmployer(EmpVO vo); public int updateEmployer(EmpVO vo); public int deleteEmployer(BigDecimal empNo); } 이 경우는 xml 설정 상에 다음과 같은 MapperConfigurer 설정이 필요하다. Ex: context-mapper.xml basePackage에 지정된 패키지 안에서 @Mapper annotation을 스캔하는 설정이다. => @Mapper로 지정된 인터페이스를 @Service에서 injection 하여 사용함 public class EmployerMapperTest { @Resource(name = "employerMapper") EmployerMapper employerMapper; @Test public void testInsert() throws Exception { EmpVO vo = makeVO(); // insert employerMapper.insertEmployer(vo); // select EmpVO resultVO = employerMapper.selectEmployer(vo.getEmpNo()); // check checkResult(vo, resultVO); } ==== 3.3 Annotation 사용 방식 ==== mapper xml 작성 없이 Mapper 인터페이스 상에 @Select, @Insert, @Update, @Delete 등의 annotation을 통해 query가 지정되어 사용된다. @Mapper("departmentMapper") public interface DepartmentMapper { @Select("select DEPT_NO as deptNo, DEPT_NAME as deptName, LOC as loc from DEPT where DEPT_NO = #{deptNo}") public DeptVO selectDepartment(BigDecimal deptNo); @Insert("insert into DEPT(DEPT_NO, DEPT_NAME, LOC) values (#{deptNo}, #{deptName}, #{loc})") public void insertDepartment(DeptVO vo); @Update("update DEPT set DEPT_NAME = #{deptName}, LOC = #{loc} WHERE DEPT_NO = #{deptNo}") public int updateDepartment(DeptVO vo); @Delete("delete from DEPT WHERE DEPT_NO = #{deptNo}") public int deleteDepartment(BigDecimal deptNo); } => 이 경우는 별도의 mapper xml을 만들 필요는 없지만, dynamic query를 사용하지 못하는 등의 제약사항이 따름