1年以上前、mybatis config 設定 = 接続情報等のXMLを使わないケースで
以下を書いた。
【再興】mybatis XML設定を使わない場合 - Oboe吹きプログラマの黙示録
1つの Mapper に限らず、複数 Mapper を使用することもあるのだから、以下が良い。
public static SqlSession getSqlSession(Class<?>... mapperClasses){ UnpooledDataSource dataSource = GenericBuilder.of(UnpooledDataSource::new) .with(UnpooledDataSource::setDriver, "com.mysql.cj.jdbc.Driver") .with(UnpooledDataSource::setUrl, "jdbc:mysql://localhost:3306/testDB?serverTimezone=JST") .with(UnpooledDataSource::setUsername, "root") .with(UnpooledDataSource::setPassword, "pass") .build(); Environment environment = new Environment("deployment", new JdbcTransactionFactory(), dataSource); Configuration config = new Configuration(environment); // snake Case → camel Case config.setMapUnderscoreToCamelCase(true); for(Class<?> cls: mapperClasses){ config.addMapper(cls); } SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(config); return factory.openSession(); }
使い回しを考えて、、、getSqlSession() だけでなく、
void execution(Consumer<SqlSession>) も以下のようにあると良いかもしれない
import java.util.function.Consumer; import java.util.function.Function; import org.apache.ibatis.datasource.unpooled.UnpooledDataSource; import org.apache.ibatis.mapping.Environment; import org.apache.ibatis.session.Configuration; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.yipuran.util.GenericBuilder; /** * SqlWorker */ public class SqlWorker{ private SqlSessionFactory factory; public SqlWorker(Class<?>... mapperClasses) { UnpooledDataSource dataSource = GenericBuilder.of(UnpooledDataSource::new) .with(UnpooledDataSource::setDriver, "com.mysql.cj.jdbc.Driver") .with(UnpooledDataSource::setUrl, "jdbc:mysql://localhost:3306/testDB?serverTimezone=JST") .with(UnpooledDataSource::setUsername, "root") .with(UnpooledDataSource::setPassword, "pass") .build(); Environment environment = new Environment("deployment", new JdbcTransactionFactory(), dataSource); Configuration config = new Configuration(environment); // snake Case → camel Case config.setMapUnderscoreToCamelCase(true); for(Class<?> cls: mapperClasses){ config.addMapper(cls); } factory = new SqlSessionFactoryBuilder().build(config); } public SqlSession getSqlSession(){ return factory.openSession(); } public void execution(Consumer<SqlSession> consumer) { SqlSession session = factory.openSession(); try(session){ consumer.accept(session); session.commit(); }catch(Exception e){ Logger logger = LoggerFactory.getLogger(getClass()); logger.error(e.getMessage(), e); session.rollback(); throw new RuntimeException(e); } } public <R> R getObject(Function<SqlSession, R> function) { try(SqlSession session = factory.openSession()){ return function.apply(session); }catch(Exception e){ Logger logger = LoggerFactory.getLogger(getClass()); logger.error(e.getMessage(), e); throw new RuntimeException(e); } } }