티스토리 뷰
스프링에서 mybatis를 설정해서 사용해보기는 했으나, 막상 스프링 없이 사용하려니 버벅여서 정리합니다.
mybatis 공식홈페이지에 가면 어떻게 사용해야 하는지 친절하게 설명되어있습니다.
http://www.mybatis.org/core/getting-started.html
항상 궁금했던 것인데, ibatis 이름은 왜 mybatis로 바뀌었는가? 그래서 검색해봤습니다.
뭔가 정치적인 재미난 얘기가 써있지 않아서 그냥 넘어가겠습니다.
1. 커넥션 생성하기
JDBC를 사용한다고 하니 일단 java.sql.Connection을 생성하고 싶습니다.
연결이 되는지 확인해야 하니까요.
그래서 간단히 유닛테스트를 작성합니다.
openSession() 메소드까지 호출해도 실제 연결을 생성하지 않기 때문에 getConnection() 메소드를 호출해봅니다.
@Test public void test() { try { String resource = "net/daum/search/shopping/analysis/sql/mybatis/mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession= sqlSessionFactory.openSession(); Connection conn = sqlSession.getConnection(); assertEquals("shop", conn.getCatalog()); } catch (Exception e) { e.printStackTrace(); fail("Exception occurs"); } }
잘 됩니다.
2. 테이블 생성하기
SqlSession을 얻어온 다음에는 mapper클래스를 사용해서 sql문을 사용합니다.
Mapper클래스를 간단히 작성하고, 설정파일에 추가합니다.
import org.apache.ibatis.annotations.Update; public interface Table { @Update("DROP TABLE IF EXISTS temp_table") int dropTempTable(); @Update("CREATE TABLE temp_table (id integer)") int createTempTable(); }
@Test public void createTable() { SqlSession sqlSession = null; try { sqlSession = openSession(); } catch (Exception e) { e.printStackTrace(); fail("Exception occurs"); } Table tableMapper= sqlSession.getMapper(Table.class); tableMapper.dropTempTable(); tableMapper.createTempTable(); sqlSession.commit(true); }
이제 테이블을 만들고 삭제할 수 있습니다.
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Numbering
- pl/java
- output driver
- 다음팟인코더
- SimpleDateFormat
- PostgreSQL
- gl2
- yum update
- GMT
- OO3
- userguide
- Arrays
- 모토로이
- openoffice
- OpenJDK6
- 파란 화면을 보았니
- Fedora 9
- IcedTea6
- 병합정렬
- 출력드라이버
- Java
- subervsion
- Linux
- smplayer
- Eclipse
- xml2
- Fedora 8
- 리눅스
- tsclient
- JavaMail
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
글 보관함