은근한

Spring boot Mybatis Mapper with XML 본문

카테고리 없음

Spring boot Mybatis Mapper with XML

EsJoo 2017. 8. 17. 15:10

resources 내에 mybatis 폴더를 만들고


mybatis 내의 mapper 폴더를 생성해서 xml 파일을 관리할 예정이다.




Mybatis Mapper Xml에 사용될 DTD (Document Type Definition)


<?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="kaonashi.ryoko.TestMapper">
<select id="getCurrentDateTime" parameterType="String" resultType="String">
         ex) select NOW()
</select>
</mapper>


namespace에는 Mapper Interface를 적어주게 되면 


Interface 내의 Method Name 과 Xml내의 id 값이 일치하면 자동으로 맵핑 시켜준다.


*namespace가 헷갈릴수도 있는데 xml이 아닌 interface위치를 맵핑시켜주면 된다.



application.properties

#Mybatis
mybatis.type-aliases-package=kaonashi.ryoko
mybatis.mapper-locations=mybatis/mapper/**/*.xml

패키지는 alias 묶어서 조금 더 보기 편하게 만드려고 했고


관리하는 mapper 폴더를 지정해줬다.



TestMapper.java

import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface TestMapper {
String getCurrentDateTime();
}



사용하고자 하는 곳에서 @Autowired

@Autowired TestMapper testMapper;