ITSTEIN
spring과 mybatis 연동 본문
Springframework와 mybatis의 연동은 많은 방법이 있다.
그중 SqlSessionTemplate, SqlSessionDaoSupport, MapperScanner 3가지 방식을 알아보겠다.
1. 기본 설정 (SqlSessionFactory)
1 2 3 4 5 6 7 8 | <!-- SqlsessionFactory setup for MyBatis Database Layer --> < bean id = "sessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean" > < property name = "dataSource" ref = "dataSource" ></ property > < property name = "mapperLocations" value = "classpath:/mapper/*.xml" ></ property > < property name = "typeAliasesPackage" value = "com.eggrok.model" ></ property > < property name = "configLocation" value = "classpath:/mybatis/mybatis-config.xml" ></ property > </ bean > |
2. SqlSessionTemplate 사용하기
1 2 3 | < bean id = "sqlSessionTemplate" class = "org.mybatis.spring.SqlSessionTemplate" > < constructor-arg index = "0" ref = "sqlSessionFactory" ></ constructor-arg > </ bean > |
1 2 3 4 5 6 7 8 9 | @Repository public class UserCommonDAO { @Autowired private SqlSessionTemplate sqlSession; public void insertUser(UserVO user) { sqlSession.insert( "UserDAO.insertUser" , user); } } |
3. SqlSessionDaoSupport 사용하기
- SqlDaoSupport는 SqlSession을 더욱 쉽게 사용하기 위한 객체이다.
- SqlSessionFactory, SqlSessionTemplate 설정을 추가하고, SqlSessionDaoSupport를 상속받는 DAO객체를 생성한다.
- SqlSessionDaoSupport의 setSqlSessionTemplate()메소드에는 @Autowired가 빠져있으므로, 상속받은 클래스에서 재정의해서 @autoWired를 추가해야한다.
1 2 3 4 5 6 7 8 9 10 11 12 13 | @Repository public class UserCommonDAO extends SqlSessionDaoSupport { @Override @Autowired public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) { super .setSqlSessionTemplate(sqlSessionTemplate); } public UserVO selectUser(String userId) { return getSqlSession().selectOne( "selectUser" , userId); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | < mapper namespace = "com.eggrok.dao.UserCommonDAO" > < select id = "selectUser" parametertype = "String" resulttype = "UserVO" > /* UserCommonDAO.selectUser */ select user_no as userNo , user_id as userId , user_name as userName from tb_user where user_id = #{userId} </ select > </ mapper > |
3. mapper interface사용.
- DAO영역을 interface로 만들어, data access 계층 추상화 처리.
- DAO영역을 구현객체로 만들지 않고, mapper.xml과 연결만 시켜주는 interface로 만든는 것이다.
- DAO에 비지니스로직이 들어갈 여지가 생기지 않고, 추상화가 가능하다.
2번 SqlDaoSupport를 mapperScanner를 사용한 interface연동으로 수정해보겠다.
1 2 3 4 5 | < bean class = "org.mybatis.spring.mapper.MapperScannerConfigurer" > < property name = "basePackage" value = "com.eggrok" > </ property ></ bean > |
- mapper.xml과 연동할 interface 생성
1 2 3 | public interface UserCommonDAO { public UserVO selectUser(String userId); } |
DAO영역이 추상화 되어서 , 더욱 객체지향적인 코드가 완성되었습니다.
## 출처
http://www.mybatis.org/spring/ko/sqlsession.html
http://www.mybatis.org/spring/ko/mappers.html
http://www.mybatis.org/spring/factorybean.html
'JAVA' 카테고리의 다른 글
java.net.SocketException : Connection reset 에러 해결기 (1) | 2018.03.23 |
---|---|
javax.net.ssl.SSLHandshakeException 해결기 (1) | 2018.03.23 |
java equals(), hashCode() 분석 (0) | 2018.03.14 |
JDBC, DBCP, JNDI는? DBCP vs JNDI (0) | 2017.07.11 |