Skip to content

Instantly share code, notes, and snippets.

@syhily
Created March 18, 2016 02:47
Show Gist options
  • Select an option

  • Save syhily/9b0dde832aac5dbd24ef to your computer and use it in GitHub Desktop.

Select an option

Save syhily/9b0dde832aac5dbd24ef to your computer and use it in GitHub Desktop.
Simple MyBatis Test Case
package com.blueocn.user.server;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.sql.Connection;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.jdbc.ScriptRunner;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Assert;
import org.mybatis.spring.transaction.SpringManagedTransactionFactory;
public abstract class AbstractDAOTest {
protected SqlSessionFactory sqlSessionFactory;
PooledDataSource ds;
private DataSource createDataSource(String tmpDb) throws IOException {
Properties props = Resources.getResourceAsProperties("h2db.properties");
ds = new PooledDataSource();
ds.setDriver(props.getProperty("driver"));
ds.setUrl(String.format(props.getProperty("url"), tmpDb));
ds.setUsername(props.getProperty("username"));
ds.setPassword(props.getProperty("password"));
return ds;
}
private void createTable(DataSource ds, String resource) throws Exception {
try (Connection connection = ds.getConnection()) {
ScriptRunner runner = new ScriptRunner(connection);
runner.setAutoCommit(true);
runner.setStopOnError(true);
runner.setLogWriter(null);
runner.setSendFullScript(true);
runner.setErrorLogWriter(new PrintWriter(System.out));
Reader reader = Resources.getResourceAsReader(resource);
runner.runScript(reader);
} catch (Exception ex) {
throw ex;
}
}
protected void setUpBeforeClassInternal(String mapperPath, String tmpDb, String tableSql) throws Exception {
DataSource ds = createDataSource(tmpDb);
createTable(ds, tableSql);
Environment environment = new Environment("test", new SpringManagedTransactionFactory(), ds);
Configuration configuration = new Configuration();
configuration.setUseGeneratedKeys(true);
configuration.setMapUnderscoreToCamelCase(true);
configuration.setEnvironment(environment);
configuration.setLazyLoadingEnabled(true);
configuration.getTypeAliasRegistry().registerAliases("com.blueocn.user.server.entity");
configuration.getTypeAliasRegistry().registerAliases("com.blueocn.user.server.v2");
parserMapper(mapperPath, configuration);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
}
private void parserMapper(String mapperPaths, Configuration configuration) throws IOException {
String[] mappers = mapperPaths.split(",");
for (int i = 0; i < mappers.length; i++) {
String mapperPath = mappers[i].trim();
InputStream mapperLocation = Resources.getResourceAsStream(mapperPath);
Assert.assertNotNull(mapperLocation);
XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(mapperLocation, configuration, mapperPath, configuration.getSqlFragments());
xmlMapperBuilder.parse();
}
}
@After
public void tearDown() throws Exception {
sqlSessionFactory = null;
if (ds != null) {
ds.forceCloseAll();
ds = null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment