Guest User

Untitled

a guest
Dec 14th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.85 KB | None | 0 0
  1. public class MyMyBatisModule extends PrivateModule {
  2.  
  3. private final String datasourceLabel;
  4. private final Properies datasourceProperties;
  5. private List< Key<?> > exposedKeys = new ArrayList< Key<?> >();
  6.  
  7. public MyMyBatisModule( String datasourceLabel, Properties datasourceProperties ) {
  8.  
  9. this.datasourceLabel = datasourceLabel;
  10. this.datasourceProperties = datasourceProperties;
  11. }
  12.  
  13. @Override
  14. protected void configure() {
  15.  
  16. install( new InternalMyMyBatisModule( ) );
  17.  
  18. for( Key<?> key: keys ) {
  19. expose( key );
  20. }
  21. }
  22.  
  23. private class InternalMyMyBatisModule extends MyBatisModule {
  24.  
  25. @Override
  26. protected void initialize( ) {
  27.  
  28. environmentId( datasourceLabel );
  29. Names.bindProperties( binder(), properties );
  30.  
  31. install( JdbcHelper.MySQL ); // See JDBC Helper commentary below
  32.  
  33. bindDataSourceProviderType( C3p0DataSourceProvider.class ); // Choose whichever one you want
  34. bindTransactionFactoryType( JdbcTransactionFactory.class );
  35.  
  36. // Register your mapper classes here. These mapper classes will have their
  37. // keys exposed from the PrivateModule
  38. //
  39. // i.e.
  40. //
  41. // keys.add( registerMapper( FredMapper.class );
  42. // kets.add( registerMapper( GingerMapper.class );
  43. }
  44.  
  45. private <T> Key<T> registerMapper( Class<T> mapperClass ) {
  46. Key<T> key = Key.get( mapperClass, Names.named( datasourceLabel ) );
  47. bind( key ).to( mapperClass );
  48. addMapperClass( mapperClass );
  49. return key;
  50. }
  51. }
  52. }
  53.  
  54. MySQL("jdbc:mysql://${JDBC.host|localhost}:${JDBC.port|3306}/${JDBC.schema}", "com.mysql.jdbc.Driver"),
  55.  
  56. public class MyBatisModules extends AbstractModule {
  57.  
  58. private Map< String, Properties > connectionsProperties;
  59.  
  60. public MyBatisModules( Map< String, Properties > = new HashMap< String, Properties > connectionsProperties ) {
  61. this.connectionsProperties = connectionsProperties; // consider deep copy if appropriate
  62. }
  63.  
  64. @Override
  65. protected void configure( ) {
  66.  
  67. for( Entry< String, Properties > datasourceConnectionProperties : this.connectionsProperties.entrySet() ) {
  68. install( new MyMyBatisModule( datasourceConnectionProperties.getKey(), datasourceConnectionProperties.getValue() ) );
  69. }
  70.  
  71. bind( MapperRetriever.class ); // See MapperRetriever later
  72.  
  73. // bind your DAO classes here. By wrapping MyBatis Mapper use in DAO implementations, theoretically we
  74. // can fairly easily change from MyBatis to any other database library just by changing the DAO implementation.
  75. // The rest of our codebase would remain the same.
  76. //
  77. // i.e.
  78. //
  79. // bind( FredDao.class ).to( FredDaoMyBatis.class );
  80. // bind( GingerDao.class).to( GingerDaoMyBatis.class );
  81. }
  82. }
  83.  
  84. public class MapperRetriever {
  85.  
  86. private final Injector injector;
  87.  
  88. @Inject
  89. public MapperRetriver( Injector injector ) {
  90. this.injector = injector;
  91. }
  92.  
  93. // The follwing two methods use the example Mappers referenced in the MyMyBatisModule implementation above
  94.  
  95. public FredMapper getFredMapper( String datasourceLabel ) {
  96. return this.injector.getInstance( Key.get( FredMapper.class, Names.named( datasourceLabel ) ) );
  97. }
  98.  
  99. public GingerMapper getGingerMapper( String datasourceLabel ) {
  100. return this.injector.getInstance( Key.get( GingerMapper.class, Names.named( datasourceLabel ) ) );
  101. }
  102. }
  103.  
  104. public interface FredDao {
  105. Fred selectFred( String datasourceLable, String fredId );
  106. }
  107.  
  108. public class FredDaoMyBatis implements FredDao {
  109.  
  110. private MapperRetriever mapperRetriever;
  111.  
  112. @Inject
  113. public FredDaoMyBatis( MapperRetriever mapperRetriever ) {
  114. this.mapperRetriever = mapperRetriever;
  115. }
  116.  
  117. @Override
  118. public Fred selectFred( String datasourceLabel, String fredId ) {
  119. FredMapper fredMapper = this.mapperRetriever.getFredMapper( datasourceLabel );
  120. return fredMapper.getFred( fredId );
  121. }
  122. }
  123.  
  124. public class DelegatingSqlSessionFactory implements SqlSessionFactory {
  125. private final Map<String, SqlSessionFactory> factories = new HashMap<>();
  126.  
  127. public DelegatingSqlSessionFactory(Map<String, DataSource> dataSources) throws ClassNotFoundException {
  128. dataSources.forEach((key, ds) -> {
  129. factories.put(key, createSqlSessionFactory(ds));
  130. });
  131. }
  132.  
  133. private SqlSessionFactory delegate() {
  134. // Read from a ThreadLocal to determine correct SqlSessionFactory key
  135. String key = findKey();
  136. return factories.get(key);
  137. }
  138.  
  139. @Override
  140. public SqlSession openSession() {
  141. return delegate().openSession();
  142. }
  143.  
  144. @Override
  145. public SqlSession openSession(boolean autoCommit) {
  146. return delegate().openSession(autoCommit);
  147. }
  148.  
  149. @Override
  150. public SqlSession openSession(Connection connection) {
  151. return delegate().openSession(connection);
  152. }
  153.  
  154. @Override
  155. public SqlSession openSession(TransactionIsolationLevel level) {
  156. return delegate().openSession(level);
  157. }
  158.  
  159. @Override
  160. public SqlSession openSession(ExecutorType execType) {
  161. return delegate().openSession(execType);
  162. }
  163.  
  164. @Override
  165. public SqlSession openSession(ExecutorType execType, boolean autoCommit) {
  166. return delegate().openSession(execType, autoCommit);
  167. }
  168.  
  169. @Override
  170. public SqlSession openSession(ExecutorType execType, TransactionIsolationLevel level) {
  171. return delegate().openSession(execType, level);
  172. }
  173.  
  174. @Override
  175. public SqlSession openSession(ExecutorType execType, Connection connection) {
  176. return delegate().openSession(execType, connection);
  177. }
  178.  
  179. @Override
  180. public Configuration getConfiguration() {
  181. return delegate().getConfiguration();
  182. }
  183. }
Add Comment
Please, Sign In to add comment