Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MyMyBatisModule extends PrivateModule {
- private final String datasourceLabel;
- private final Properies datasourceProperties;
- private List< Key<?> > exposedKeys = new ArrayList< Key<?> >();
- public MyMyBatisModule( String datasourceLabel, Properties datasourceProperties ) {
- this.datasourceLabel = datasourceLabel;
- this.datasourceProperties = datasourceProperties;
- }
- @Override
- protected void configure() {
- install( new InternalMyMyBatisModule( ) );
- for( Key<?> key: keys ) {
- expose( key );
- }
- }
- private class InternalMyMyBatisModule extends MyBatisModule {
- @Override
- protected void initialize( ) {
- environmentId( datasourceLabel );
- Names.bindProperties( binder(), properties );
- install( JdbcHelper.MySQL ); // See JDBC Helper commentary below
- bindDataSourceProviderType( C3p0DataSourceProvider.class ); // Choose whichever one you want
- bindTransactionFactoryType( JdbcTransactionFactory.class );
- // Register your mapper classes here. These mapper classes will have their
- // keys exposed from the PrivateModule
- //
- // i.e.
- //
- // keys.add( registerMapper( FredMapper.class );
- // kets.add( registerMapper( GingerMapper.class );
- }
- private <T> Key<T> registerMapper( Class<T> mapperClass ) {
- Key<T> key = Key.get( mapperClass, Names.named( datasourceLabel ) );
- bind( key ).to( mapperClass );
- addMapperClass( mapperClass );
- return key;
- }
- }
- }
- MySQL("jdbc:mysql://${JDBC.host|localhost}:${JDBC.port|3306}/${JDBC.schema}", "com.mysql.jdbc.Driver"),
- public class MyBatisModules extends AbstractModule {
- private Map< String, Properties > connectionsProperties;
- public MyBatisModules( Map< String, Properties > = new HashMap< String, Properties > connectionsProperties ) {
- this.connectionsProperties = connectionsProperties; // consider deep copy if appropriate
- }
- @Override
- protected void configure( ) {
- for( Entry< String, Properties > datasourceConnectionProperties : this.connectionsProperties.entrySet() ) {
- install( new MyMyBatisModule( datasourceConnectionProperties.getKey(), datasourceConnectionProperties.getValue() ) );
- }
- bind( MapperRetriever.class ); // See MapperRetriever later
- // bind your DAO classes here. By wrapping MyBatis Mapper use in DAO implementations, theoretically we
- // can fairly easily change from MyBatis to any other database library just by changing the DAO implementation.
- // The rest of our codebase would remain the same.
- //
- // i.e.
- //
- // bind( FredDao.class ).to( FredDaoMyBatis.class );
- // bind( GingerDao.class).to( GingerDaoMyBatis.class );
- }
- }
- public class MapperRetriever {
- private final Injector injector;
- @Inject
- public MapperRetriver( Injector injector ) {
- this.injector = injector;
- }
- // The follwing two methods use the example Mappers referenced in the MyMyBatisModule implementation above
- public FredMapper getFredMapper( String datasourceLabel ) {
- return this.injector.getInstance( Key.get( FredMapper.class, Names.named( datasourceLabel ) ) );
- }
- public GingerMapper getGingerMapper( String datasourceLabel ) {
- return this.injector.getInstance( Key.get( GingerMapper.class, Names.named( datasourceLabel ) ) );
- }
- }
- public interface FredDao {
- Fred selectFred( String datasourceLable, String fredId );
- }
- public class FredDaoMyBatis implements FredDao {
- private MapperRetriever mapperRetriever;
- @Inject
- public FredDaoMyBatis( MapperRetriever mapperRetriever ) {
- this.mapperRetriever = mapperRetriever;
- }
- @Override
- public Fred selectFred( String datasourceLabel, String fredId ) {
- FredMapper fredMapper = this.mapperRetriever.getFredMapper( datasourceLabel );
- return fredMapper.getFred( fredId );
- }
- }
- public class DelegatingSqlSessionFactory implements SqlSessionFactory {
- private final Map<String, SqlSessionFactory> factories = new HashMap<>();
- public DelegatingSqlSessionFactory(Map<String, DataSource> dataSources) throws ClassNotFoundException {
- dataSources.forEach((key, ds) -> {
- factories.put(key, createSqlSessionFactory(ds));
- });
- }
- private SqlSessionFactory delegate() {
- // Read from a ThreadLocal to determine correct SqlSessionFactory key
- String key = findKey();
- return factories.get(key);
- }
- @Override
- public SqlSession openSession() {
- return delegate().openSession();
- }
- @Override
- public SqlSession openSession(boolean autoCommit) {
- return delegate().openSession(autoCommit);
- }
- @Override
- public SqlSession openSession(Connection connection) {
- return delegate().openSession(connection);
- }
- @Override
- public SqlSession openSession(TransactionIsolationLevel level) {
- return delegate().openSession(level);
- }
- @Override
- public SqlSession openSession(ExecutorType execType) {
- return delegate().openSession(execType);
- }
- @Override
- public SqlSession openSession(ExecutorType execType, boolean autoCommit) {
- return delegate().openSession(execType, autoCommit);
- }
- @Override
- public SqlSession openSession(ExecutorType execType, TransactionIsolationLevel level) {
- return delegate().openSession(execType, level);
- }
- @Override
- public SqlSession openSession(ExecutorType execType, Connection connection) {
- return delegate().openSession(execType, connection);
- }
- @Override
- public Configuration getConfiguration() {
- return delegate().getConfiguration();
- }
- }
Add Comment
Please, Sign In to add comment