빠에야는 개발중
MySQL, MyBatis 연동 본문
Maven dependency 설정으로 mysql과 mybatis를 등록하고 테스트 케이스를 작성했다. 프로퍼티의 실제 값들은 따로 파일에 빼놓았다.
1 2 3 4 5 6 7 8 9 10 11 | <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- mybatis SqlSessionFactoryBean --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> </bean> | cs |
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 29 30 31 32 | package com.aiolos.sloot; import java.sql.Connection; import javax.inject.Inject; import javax.sql.DataSource; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:root-context.xml" }) public class MySQLConnectionTest { @Inject private DataSource ds; @Test public void testConnection() throws Exception { try (Connection con = ds.getConnection()) { System.out.println("\n >>>>>>>>>> Connection 출력 : " + con + "\n"); } catch (Exception e) { e.printStackTrace(); } } } | cs |
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 29 30 31 32 33 34 35 36 | package com.aiolos.sloot; import javax.inject.Inject; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:root-context.xml"}) public class MyBatisTest { @Inject private SqlSessionFactory sqlFactory; @Test public void testFactory(){ System.out.println("\n >>>>>>>>>> sqlFactory 출력 : "+sqlFactory); } @Test public void testSession() throws Exception{ try(SqlSession session = sqlFactory.openSession()){ System.out.println(" >>>>>>>>>> session 출력 : "+session+"\n"); } catch (Exception e) { e.printStackTrace(); } } } | cs |
테스트를 위해서 junit 버전을 4.7에서 4.1.2로 낮추어주었다. 안 낮추면 에러를 뿜는다.
hamcrest도 추가해주었다. 이게 없으면 SelfDescribing 클래스를 못찾는다며 에러를 뿜는다.
1 2 3 4 5 6 7 8 9 10 11 12 | <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-all</artifactId> <version>1.3</version> <scope>test</scope> </dependency> | cs |
참고 출처 : http://all-record.tistory.com/175
'개인 프로젝트 - Sloot(중단)' 카테고리의 다른 글
AWS RDS를 사용해보자 (0) | 2018.02.15 |
---|---|
아침부터 서버가 터졌다. (0) | 2018.02.14 |
기능 설계 및 API 명세 작성 (0) | 2018.02.14 |
개발 환경을 설정하자 (0) | 2018.02.12 |
개발 스펙 (0) | 2018.02.09 |
Comments