java application project에서 테스트를 위한 작업순서(in Maven 프로젝트)
1. pom.xml 추가
1.1 mybatis 기본 dependency 추가
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
1.2 config.xml 설정파일 관련 dependency 추가
<!-- https://mvnrepository.com/artifact/commons-configuration/commons-configuration -->
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-collections/commons-collections -->
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
1.3 Config.java, ConnectionFactory.java 생성
-- Config.java --
public class Config {
private static Configuration configuration;
static {
try {
URL url = Config.class.getClassLoader().getResource("config/database.xml");
if (configuration == null) {
configuration = new XMLConfiguration(url);
} else {
System.out.println("return configuration from Memory...");
}
} catch (ConfigurationException e) {
e.printStackTrace();
}
}
public static Configuration getConfiguration() {
return configuration;
}
}
-- ConnectionFactory.java -----
public class MyBatisConnectionFactory {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
final String resource = "config/mybatis-config.xml";
Reader reader = Resources.getResourceAsReader(resource);
if (sqlSessionFactory == null) {
Configuration conf = MyConfig.getConfiguration();
Properties props = new Properties();
props.setProperty("driver", conf.getString("jdbc.driver","oracle.jdbc.driver.OracleDriver"));
props.setProperty("url", conf.getString("jdbc.url","jdbc:oracle:thin:@127.0.0.1:1521:orcl"));
props.setProperty("username", conf.getString("jdbc.username","test2018"));
props.setProperty("password", conf.getString("jdbc.password","test2018"));
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader, props);
}
} catch (FileNotFoundException fileNotFoundException) {
fileNotFoundException.printStackTrace();
} catch (IOException iOException) {
iOException.printStackTrace();
}
}
public static SqlSessionFactory getSqlSessionFactory() {
return sqlSessionFactory;
}
}
2. TestMain.java 테스트 (Transaction 처리 포함 테스트)
...중략...
public static void main(String[] args) {
SqlSession sqlSession = null;
try {
sqlSession = MyBatisConnectionFactory.getSqlSessionFactory().openSession(false);
//Mapper 사용하는 방법
TestMapper testMapper = sqlSession.getMapper(TestMapper.class);
List<Map<String, Object>> progrmList = testMapper.getProgrmList();
//Sqlsession을 사용하는 방법
List<Map<String, Object>> progrmList = (List<Map<String,Object>>)sqlSession.selectList("getProgrmList");
for (Iterator iterator = progrmList.iterator(); iterator.hasNext();) {
Map<String, Object> map = (Map<String, Object>) iterator.next();
System.out.println(map);
}
Map<String, Object> param = new HashMap<String, Object>();
param.put("id", "test8");
param.put("name", "test8 name");
param.put("age", 88);
testMapper.insertTest(param);
Map<String, Object> param2 = new HashMap<String, Object>();
param2.put("id", "test5");
param2.put("name", "test5 update name");
param2.put("age", 28);
testMapper.updateTest(param2);
// 커밋
sqlSession.commit();
} catch (Exception e) {
// 롤백
sqlSession.rollback();
e.printStackTrace();
} finally {
sqlSession.close();
}
}