Spring

02-2. ORM : MyBatis 연동하기 (Mapper, Configration ) [Spring]

민돌이 2016. 11. 2. 09:52
반응형

Mapper 설정

src/main/java -> dao -> impl -> sql 에 indexDao.xml 이름으로 file 생성


<?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="indexDao">

//namaspace -> 파일명


<select id="getNowDateTime" resultType="string" >

SELECT /* [indexDao.xml] [getNowDateTime] */

SYSDATE

FROM         DUAL

</select>


</mapper>


IndexDaoImpl에 있는

public String getNowDateTime() {

return getSqlSession().selectOne("indexDao.getNowDateTime");

} // indexDao은 namespace  getNowDateTime 은 select id이다



Configration 생성

src/main/resource -> mybatis.xml 이름으로 file 생성


<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">


<configuration>


<mappers>

<mapper resource="com/ktds/dao/impl/sql/indexDao.xml" />

</mappers>


</configuration>


Configration 설정


rootContext.xml 에서 <bean id ="sqlSessionFactory"  </bean> 안에

<property name="configLocation" value="classpath:/mybatis.xml" /> 추가하기

 


Controller -> Service -> biz -> dao

순으로 호출한다.



typeAliases -> 별칭을 주기


<typeAliases>

<typeAlias type="com.ktds.vo.EmployeesVO" alias="EmployeesVO" />

<!-- com.ktds.vo.EmployeesVO  EmployeesVO 로 별칭을 준다  -->

</typeAliases>




반응형