스프링,마이바티스연동예제(MapperScannerConfigurer, SqlSessionFactoryBean이용)
[개요]
SqlSessionTemplate을 주입받고 Mapper Interface 참조를 취득 후
MyBatis를 연동하기도 하지만, 이번에는 SqlSessionTemplate을 이용하지 않고
MapperScannerConfigurer를 이용하여 Mapper Interface를 스프링에서 자동스캔하게
하여 여러 매퍼를 이용할 수 있는 구성으로 만들어 보자. Mapper만 필요한 곳(DAO)에서
주입받아 사용하면 된다.
0. 실습을 위한 테이블 생성 및 데이터 입력
create table myemp (
Empno number,
Ename varchar2(10)
)
insert into myemp Values (1, 1길동’);
COMMIT;
1. [classpath:mybatis2/mybatis2.xml]
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<property name="driverClass" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@192.168.0.27:1521:onj" />
<property name="username" value="scott" />
<property name="password" value="tiger" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis2/config2.xml" />
<property name="mapperLocations" value="classpath:mybatis2/mappers/myemp2.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="onj.edu.mybatis2" />
</bean>
</beans>
2. [classpath:mybatis2/config2.xml]
<?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>
<typeAliases>
<typeAlias
type="onj.edu.mybatis2.MyEmp"
alias="myemp" />
</typeAliases>
</configuration>
3. 매퍼 XML
[classpath:mybatis2/mappers/myemp2.xml]
<?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="onj.edu.mybatis2.MyEmpDao">
<resultMap type="myemp" id="myemp">
<result property="empno" column="empno" />
<result property="ename" column="ename" />
</resultMap>
<select id="getMyEmp" parameterType="int" resultMap="myemp">
select empno, ename from myemp where empno=#{empno}
</select>
<insert id="setMyEmp" parameterType="onj.edu.mybatis.model.MyEmp">
insert into myemp (empno, ename) values ( #{empno}, #{ename} )
</insert>
<update id="updateMyEmp" parameterType="onj.edu.mybatis.model.MyEmp">
update myemp
set ename = #{ename}
where empno = #{empno}
</update>
</mapper>
4. MyEmp.java
package onj.edu.mybatis2;
//myemp 테이블의 도메인 클래스
public class MyEmp {
private int empno;
private String ename;
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
}
5. 매퍼인터페이스, MyEmpDao.java
package onj.edu.mybatis2;
import org.springframework.stereotype.Service;
public interface MyEmpDao {
public MyEmp getMyEmp(int empno); //empno로 사원데이터 selcet
public int setMyEmp(MyEmp myEmp); //insert
public int updateMyEmp(MyEmp myEmp);
}
6. MyEmpDaoImpl.java
package onj.edu.mybatis2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class MyEmpDaoImpl implements MyEmpDao{
//Mapper Interface
@Autowired
MyEmpDao myEmpDao ;
public MyEmp getMyEmp(int empno) {
return myEmpDao.getMyEmp(empno);
}
public int setMyEmp(MyEmp myEmp) {
return myEmpDao.setMyEmp(myEmp);
}
public int updateMyEmp(MyEmp myEmp) {
return myEmpDao.updateMyEmp(myEmp);
}
}
7. MyEmpTest.java
package onj.edu.mybatis2;
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;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:mybatis2/mybatis2.xml")
public class MyEmpTest {
@Autowired
private MyEmpDao myEmpDao;
@Test
public void test() {
MyEmp myEmp = myEmpDao.getMyEmp(1);
System.out.println(myEmp.getEmpno()); //1
System.out.println(myEmp.getEname()); //1길동
myEmp.setEmpno(myEmp.getEmpno()+1);
myEmp.setEname("3길동");
int ret = myEmpDao.setMyEmp(myEmp); //3번데이터 Insert
System.out.println(ret);
myEmp.setEmpno(1);
myEmp.setEname("변경된이름");
int ret2 = myEmpDao.updateMyEmp(myEmp); //1번데이터 이름변경
System.out.println(ret2);
}
}
댓글 없음:
댓글 쓰기