본문으로 바로가기

[MyBatis와 Spreing 연동하기]

category 수업내용 정리/정리안된 메모 2019. 6. 16. 21:01

[MyBatis와 Spreing 연동하기] : 스프링에 모든 작업 던져버리기 (*DAO클래스 만들지 않기)

이미지0) 준비하기 : porm.xml 설정

Spring : core,context,jdbc

Oracle : oracle

MyBatis : Mybatis, mybatis-spring​

이미지1-1) Mybaits가 만든 DAO가 Spring 위에서 동작하게 만들기 : applicationContext.xml 작성

1. sqlSessionFactory 객체 만들기 ​

2. StudentDaoImp에 의존성 주입

Mybaits가 만든 DAO가 Spring 위에서 동작해야 하기 때문에, 기존 Spring JDBC에서 사용했던 설정을 해주어야 한다.

이미지1-2) DAO interFace bean으로 등록하기 

DAO 작성시 구현클래스를 직접만들지 않고, 스프링에서 제공하는 MapperFactoryBean 클래스를 이용하는 것을 볼 수 있다.

(1) MapperFactoryBean을 Class로 지정해준다.

(2) value ="작성한 인터페이스 경로" 작성했던 인터페이스를 등록시켜 준다. (name = "mapperInterface") 

(3) 의존성 주입을 위해서 이미지1-1 에서 만든 sqlSessionFactory를 활용한다 

----------------------------------------------------------------------------

이미지2) MemberDao 클래스 코드 (인터페이스)

이미지3) 테스트해보기 bean으로 등록한 MemberDao interface 활용 

MapperFactoryBean 클래스를 이용해서 DAO를 만들어 냈다고 하더라도, 해당 DAO의 근본(?)은 MemberDao 이기에 getBean을 파라미터 부분에 적는 class를 MemberDao로 적어준다.​​

결과적으로 DAO 인터페이스와, DaoMapper만 작성해서 이전에 하던 DAO 작업을 모두 할 수 있음.

[applicationContext.xml 작성시 각각의 Bean에 들어가는 property]

dataSource org.springframework.jdbc.datasource.DriverManagerDataSource

driverClassName : oracle.jdbc.driver.OracleDriver

url : jdbc:oracle:thin:@localhost:1521:XE

​username, password

sqlSession org.mybatis.spring.SqlSessionFactoryBean

dataSource

mapperLocations : classpath:dao/mapper/**/*.xml

Dao org.mybatis.spring.mapper.MapperFactoryBean

sqlSessionFactory

mapperInterface

​​

'수업내용 정리 > 정리안된 메모' 카테고리의 다른 글

19/01/03​  (0) 2019.06.16
19/01/02  (0) 2019.06.16
18/12/31​  (0) 2019.06.16
18/12/28  (0) 2019.06.16
003  (0) 2019.06.16