Spring 3.0 + iBatis 3.0 연동 질문입니다

1,089 views
Skip to first unread message

ycperfect

unread,
Oct 6, 2010, 11:50:49 AM10/6/10
to Korea Spring User Group
으헉

스프링 MVC에서

C를.... 컨트롤러 서비스 DAO까지 쪼갰는데...

DAO에서 iBatis 3.0을 쓸려니 정말 어떻게 하는건지 헷갈리네요

getSqlMapTemplate()

이거 아예 써서 가져오질 못하던데

어떻게 하시나여?

지금 일단 iBatis 3.0에 있는 메소드대로 하고 Spring과 연동없이는 됐는데

이걸 DI해서 하려니까 안 되네여;;


String resource = "main/config/sqlmapconfig.xml";
Reader reader = null;

try {
reader = Resources.getResourceAsReader(resource);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

SqlSessionFactory sqlMapper = new
SqlSessionFactoryBuilder().build(reader);
SqlSession session = sqlMapper.openSession();
ArrayList<DTObbs> ListBbs = (ArrayList<DTObbs>)
session.selectList("bbs.xoListBbs01", DTObbs.class);

session.close();
return ListBbs;

이걸 억지로 reader랑 SqlSessionFactory, SqlSession을 Bean으로 스프링 ap-con에 생성하고

그걸 초기화시킬려고 했는데

저렇게 메소드로 생성을 시키네요;

저럴때, 제가 하는 방식대로 해서

한다면...

reader, SqlSessionFactory, SqlSession을 생성할때

안에 property는 어떻게 넣어야 할까요? ㅠㅠ

연동 꼭 하고 싶네요 ㅠㅠ

Sanghyuk Jung

unread,
Oct 6, 2010, 7:26:22 PM10/6/10
to ks...@googlegroups.com
iBatis가 3.0에서 많은 것이 바뀌었네요. 이전버전과 다르게  나름대로의 FactoryBean을 만들어줘야 가능할 것입니다.

 단순하게 쿼리호출뿐만이 아니고 Spring에서 관리하는 Datasource와 연결, Transaction처리등이 되어야할 것인데, 아래 이슈에서 이전버전과 같이 Spring에서 편하게 쓸 수 있도록 작업이 진행 중인 것으로 보입니다.

https://jira.springframework.org/browse/SPR-5991

위의 페이지에  첨부된 파일들이 도움이 될 것 같네요.

iBatis 3.0에 추가된 특별한 기능이 필요하신 것이 아니라면 이전버전을 쓰는것이 더 나을지도 모르겠습니다..



2010년 10월 7일 오전 12:50, ycperfect <burn...@gmail.com>님의 말:

--
Google 그룹스 'Korea Spring User Group' 그룹에 가입했으므로 본 메일이 전송되었습니다.
이 그룹에 게시하려면 ks...@googlegroups.com(으)로 이메일을 보내세요.
그룹에서 탈퇴하려면 ksug+uns...@googlegroups.com로 이메일을 보내주세요.
더 많은 옵션을 보려면 http://groups.google.com/group/ksug?hl=ko에서 그룹을 방문하세요.


Harold Kim

unread,
Oct 8, 2010, 4:39:38 AM10/8/10
to Korea Spring User Group
아직 spring3에서는 mybatis(ibatis3)를 공식적으로 지원하지는 않는 것 같구요.
mybatis.org 에서 최근 릴리즈한 mybatis-spring 을 검토해 보시면 될 것 같네요.

mybatis3에서 새롭게 제공되는 Mapper Interface도 역시 DI가능합니다.~


> 다운로드 위치

http://code.google.com/p/mybatis/downloads/list?can=3

> application context 세팅은 아래와 같이 datasource 정의하셔서 sqlSessionFactory에 꽂아주시면 됩니다.

설정파일 예시 :

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://
www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jdbc="http://www.springframework.org/schema/jdbc">

<bean id="propertyConfigurer"

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="location">
<value>net/kimgisa/practice/data/mybatis-config.properties</value>
</property>
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.username}" />
<property name="password" value="${database.password}" />

<property name="testWhileIdle" value="true" />
<property name="timeBetweenEvictionRunsMillis" value="300000" />
<property name="numTestsPerEvictionRun" value="6" />
<property name="minEvictableIdleTimeMillis" value="1800000" />
<property name="initialSize" value="1" />
<property name="maxActive" value="5" />
<property name="maxIdle" value="5" />
<property name="maxWait" value="5000" />
<property name="poolPreparedStatements" value="true" />
<property name="maxOpenPreparedStatements" value="10" />
</bean>

<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation"
value="classpath:net/kimgisa/practice/data/mybatis-config.xml" />
<property name="environment" value="development" />
</bean>

<bean id="zipCodeMapper"
class="org.mybatis.spring.MapperFactoryBean">
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
<property name="mapperInterface"
value="net.kimgisa.practice.mapper.ZipCodeMapper" />
</bean>

<bean id="zipCodeService"
class="net.kimgisa.practice.service.impl.ZipCodeServiceImpl">
<property name="zipCodeMapper" ref="zipCodeMapper" />
</bean>
</beans>

Sanghyuk Jung

unread,
Oct 8, 2010, 4:44:05 AM10/8/10
to ks...@googlegroups.com
이것도 찾아보면 나오겠지만;

얼핏봐서는 myBatis도 iBatis 3.0과 클래스명등은 비슷해보이네요.
iBatis 원래 만든 사람이 myBatis 였었다는 정도만 들었는데,

iBatis 3.0과 MyBatis가 어떤차이가 있는지도 궁금하네요.

(주말에 찾아보고 자문자답할지도 모르겠습니다 ^^;)

2010년 10월 8일 오후 5:39, Harold Kim <magu...@gmail.com>님의 말:

burnbone

unread,
Oct 8, 2010, 4:50:38 AM10/8/10
to ks...@googlegroups.com
영문 레퍼런스 보니까

iBatis에서 그냥 찾아 바꾸기 눌러서 myBatis로 바뀐 정도?

아파치 재단에서 구글이었나... 이동하는 바람에 그냥 생긴 이름 바꾸기인듯;

영어가 잘 안 되서 이 이상은....

Harold Kim

unread,
Oct 8, 2010, 4:53:25 AM10/8/10
to Korea Spring User Group
네 아파치 재단에서 Google Code로 소스코드를 이전하면서 이름을 바꾸었습니다.
그런데 아직 패키지명은 여전히 ora.apache.ibatis.... 로 남아있네요 (3.0.2 release 버전 기준)

Sungchul Park

unread,
Oct 8, 2010, 5:07:07 AM10/8/10
to ks...@googlegroups.com
같은 프로젝트에요.
apache 프로젝트였다가 myBatis로 독립한...
클린턴 베긴 아저씨가 아파치와 뭔 갈등이 있었다고 하더라고요.
아파치의 의사 결정 과정이나 일 진행 방식이 프로젝트의 성장을 방해한다고 판단했다는 것 같은데...

참고하세요.
http://communityovercode.com/2010/06/mybatis-forks-apache-ibatis/
이것도 찾아보면 나오겠지만;

얼핏봐서는 myBatis도 iBatis 3.0과 클래스명등은 비슷해보이네요.
iBatis 원래 만든 사람이 myBatis 였었다는 정도만 들었는데,

iBatis 3.0과 MyBatis가 어떤차이가 있는지도 궁금하네요.

(주말에 찾아보고 자문자답할지도 모르겠습니다 ^^;)

2010년 10월 8일 오후 5:39, Harold Kim <magu...@gmail.com>님 의 말:
이 그룹에 게시하려면 ks...@googlegroups.com(으) 로 이메일을 보내세요.

그룹에서 탈퇴하려면 ksug+uns...@googlegroups.com로 이메일을 보내주세요.
더 많은 옵션을 보려면 http://groups.google.com/group/ksug?hl=ko에 서 그룹을 방문하세요.

ycperfect

unread,
Oct 10, 2010, 11:55:53 AM10/10/10
to Korea Spring User Group
글을 썼는데... 글이 포스팅이 안 되서 다시 올립니다..

Harold Kim님께서 올리신것처럼 DI를 하다보니 질문이 생겼는데요

참고로 전 Annotation 안 쓰고 mapper 파일이 xml로 config 파일에서 연결했고 이거에 문제는 생겼지만 버그
는 잡앗습니다

<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:properties/server/server.properties</value>
</property>
</bean>
-> 파일로부터 프로퍼티 읽어들이기

<bean id="dataSource"
class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<property name="driverClass" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</bean>
-> 데이터소스에 DI
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis/
mybatis_main.xml" />
<property name="environment" value="development" />
</bean>
-> sqlSessionFactoryBean에서 데이터소스, mybatis 설정값 DI

<bean id="sqlSessionTemplate"
class="org.mybatis.spring.SqlSessionTemplate">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
-> DI한 값을 가지고 만들어진 sqlSessionFactory를 sqlSessionTemplate에 주입

<bean id="DAObbsImpl" class="main.dao.impl.DAObbsImpl">
<property name="sqlSessionTemplate" ref="sqlSessionTemplate"/>
</bean>
-> DAObbsImpl이라는 클래스(SQL을 처리해서 입력시키는 클래스;)에 DI

그런데... 그 다음에 DAObbsImpl에서 어떤 식으로 써야할지 모르겠습니다

API를 찾아봐도 헷갈리고;
이건 스프링이 아니긴 한데.... 연결고리에서 애매하기도 합니다 ㅠㅠ 이건 mybatis에서도 안 나오는거라서여;


public class DAObbsImpl extends SqlSessionDaoSupport {
@Autowired
SqlSessionTemplate sqlSessionTemplate;

public List<DTObbs> test(){
List<DTObbs> jo =
getSqlSessionTemplate().selectList("bbs.xoListBbs01", DTObbs.class);
}
}

뭐 이렇게.. 짰는데... null 에러만 뜨고 제대로 못 가져오는 거 같습니다
어떤 부분이 문제가 생긴걸까요 -_-;;

그.. mapper di 말씀하신 걸 보니.. 영어판을 그냥 읽어서 이해가 전 잘 안 되는데;
그.. mapper 자체는 어노테이션 안 쓰는 이상 스프링 어플 컨텍스트에 넣을 필요는 없지 않나요?
config 파일에 그 정보가 들어가있는데...

거기서 막히네요 ㅠ

On 10월8일, 오후5시39분, Harold Kim <magud...@gmail.com> wrote:
> 아직 spring3에서는 mybatis(ibatis3)를 공식적으로 지원하지는 않는 것 같구요.
> mybatis.org 에서 최근 릴리즈한 mybatis-spring 을 검토해 보시면 될 것 같네요.
>
> mybatis3에서 새롭게 제공되는 Mapper Interface도 역시 DI가능합니다.~
>
> > 다운로드 위치
>
> http://code.google.com/p/mybatis/downloads/list?can=3
>
> > application context 세팅은 아래와 같이 datasource 정의하셔서 sqlSessionFactory에 꽂아주시면 됩니다.
>
> 설정파일 예시 :
>
> <?xml version="1.0" encoding="UTF-8"?>
> <beans xmlns="http://www.springframework.org/schema/beans"
> xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/jdbchttp://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd"

Harold Kim

unread,
Oct 13, 2010, 4:44:46 AM10/13/10
to Korea Spring User Group
Spring에서 SqlSessionTemplate을 생성해서 DAO 구현체에 주입시키는 방식이라면
설정은 그대로 두시고 아래처럼 구현하면 될 듯 싶네요. (eye-coding이라 그대로 쓰시면 안돌아 갈수도...)

1. DAO 구현 예

public class DAObbsImpl extends SqlSessionDaoSupport {

SqlSessionTemplate sqlSessionTemplate;

public void setSqlSessionTemplate(SqlSessionTemplate
sqlSessionTemplate) {
this.sqlSessionTemplate = sqlSessionTemplate;
}

public List<DTObbs> test() {
List<DTObbs> jo =

sqlSesssionTemplate.selectList("bbs.xoListBbs01",
DTObbs.class);
}
}

2. Mapper Interface를 서비스에 주입시키시면 별도 DAO구현없이 쿼리를 수행하실 수 있습니다.
SqlSession의 getMapper()를 호출하면 mybatis 프레임워크가 프록시 객체를 생성해서 리턴합니다.
간단한 쿼리는 어노테이션 매핑을 이용할 수도 있습니다.

On 10월11일, 오전12시55분, ycperfect <burnb...@gmail.com> wrote:
> 글을 썼는데... 글이 포스팅이 안 되서 다시 올립니다..
>
> Harold Kim님께서 올리신것처럼 DI를 하다보니 질문이 생겼는데요
>
> 참고로 전 Annotation 안 쓰고 mapper 파일이 xml로 config 파일에서 연결했고 이거에 문제는 생겼지만 버그
> 는 잡앗습니다
>
> <bean id="propertyConfigurer"

> class="org.springframework.beans.factory.config.PropertyPlaceholderConfigur er">
> <property name="location">

> > xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework..."

burnbone

unread,
Oct 13, 2010, 9:38:06 AM10/13/10
to ks...@googlegroups.com
첫번째대로 했는데 여전히 에러는 널포인터가 뜨네여;

sqlSessionTemplate의 게터와 세터 메소드가 파이널로 지정되서

확장되면 아예 오버라이드를 할 수도 없고

아예 널이 떠버리네요 -_-;;

2번대로 해야하는걸까요 -_ㅋ

Harold Kim

unread,
Oct 13, 2010, 10:49:04 AM10/13/10
to Korea Spring User Group
샘플로 구현한 소스 메일로 보내드렸습니다. 참고하세요.

Max

unread,
Oct 26, 2010, 6:17:47 AM10/26/10
to Korea Spring User Group
Transaction Bean설정 부분이 빠진것 같은데, 이부분은 어떻게 처리 하셨어요???
DataSourceTransactionManager를 쓰셨나요? 아님 myBatis-spring에서 제공하는걸 쓰셨나요?
myBatis 설정 찾기가 힘들군요... ㅡㅜ;;;


On 10월11일, 오전12시55분, ycperfect <burnb...@gmail.com> wrote:

> 글을 썼는데... 글이 포스팅이 안 되서 다시 올립니다..
>
> Harold Kim님께서 올리신것처럼 DI를 하다보니 질문이 생겼는데요
>
> 참고로 전 Annotation 안 쓰고 mapper 파일이 xml로 config 파일에서 연결했고 이거에 문제는 생겼지만 버그
> 는 잡앗습니다
>
> <bean id="propertyConfigurer"

> class="org.springframework.beans.factory.config.PropertyPlaceholderConfigur er">
> <property name="location">

> > xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework..."

burnbone

unread,
Oct 26, 2010, 9:35:07 AM10/26/10
to ks...@googlegroups.com
생각해보니 그냥 흘러갔는데요 ㅋㅋ

딱히 쓴건 없는듯 합니다 트랜잭션은; ㅋ

sunghan yun

unread,
Oct 26, 2010, 10:11:36 PM10/26/10
to ks...@googlegroups.com
myBatis3 User Guide 18페이지에 나와 있네요. 

2010년 10월 26일 오후 10:35, burnbone <burn...@gmail.com>님의 말:
생각해보니 그냥 흘러갔는데요 ㅋㅋ

딱히 쓴건 없는듯 합니다 트랜잭션은; ㅋ

--

sinius

unread,
Nov 30, 2010, 12:53:03 AM11/30/10
to Korea Spring User Group
혹시 샘플로 구현된 소스 저도 좀 메일로 받아볼수 있을까요?

> > 2번대로 해야하는걸까요 -_ㅋ- 원본 텍스트 숨기기 -
>
> - 원본 텍스트 보기 -

Reply all
Reply to author
Forward
0 new messages