本文介绍了MyBatis-guice 3.3 +多个数据源+属性+ scriptrunner的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MyBatis-guice 3.3使用java Properties对象和ScriptRunner连接到第一个数据库以运行几个脚本:

I'm using MyBatis-guice 3.3 to connect to a first database using a java Properties object and a ScriptRunner to run few scripts:

Environment environment = injector.getInstance(SqlSessionFactory.class).getConfiguration().getEnvironment();
DataSource source = environment.getDataSource();
ScriptRunner runner = new ScriptRunner(source.getConnection());
runner.setLogWriter(null);
runner.setStopOnError(true);
runner.runScript(Resources.getResourceAsReader(properties.getProperty("script.dbA.create.schema")));

现在我想使用相同的方法添加第二个数据源(dbB)。按照MyBatis-guice参考指南,我必须使用2个PrivateModule。这部分工作正常。

Now I would like to add a second datasource (dbB) using the same approach. Following the MyBatis-guice reference guide I have to use 2 PrivateModule. This part works fine.

但是我应该如何调用我的ScriptRunner为dbA运行一些脚本,为dbB运行一些其他脚本?

But then how should I call my ScriptRunner to run some scripts for dbA and some others for dbB?

推荐答案

如果有人在GWT平台环境中寻找解决方案,这里是来自工作环境的配置。希望这很有用。

If anyone is looking for a solution in GWT Platform environment, here is the configuration from an working environment. Hope this is useful.

// mybatis-dbA-config.xml
<configuration>

  <settings>
    <setting name="cacheEnabled" value="false" />
    <setting name="useGeneratedKeys" value="true" />
    <setting name="defaultExecutorType" value="REUSE" />
  </settings>

  <typeAliases>
    <package name="com.example.domain" />
  </typeAliases>

  <environments default="test">
    <environment id="test">
      <transactionManager type="JDBC" />
      <dataSource type="POOLED">
        <property name="driver" value="oracle.jdbc.driver.OracleDriver" />
        <property name="url" value="jdbc:oracle:thin:@//localhost:1521/SchemaName" />
        <property name="username" value="user" />
        <property name="password" value="pwd" />
      </dataSource>
    </environment>
  </environments>

  <mappers>
    <mapper resource="com/example/mapper/DomainAMapper.xml" />
  </mappers>

</configuration>

...

//dbA Dao implementation
public class DbADaoImpl implements DbADao {
    @Inject
    @Named("DbA")
    private SqlSession sqlSession;

    public List<DomainA> listDbADomains() {
        return this.sqlSession.selectList("com.example.mapper.DomainAMapper.listDomainAs");
    }
...

//dbB Dao implementation
public class DbBDaoImpl implements DbBDao {
    @Inject
    @Named("DbB")
    private SqlSession sqlSession;

    public List<DomainB> listDbBDomains() {
        return this.sqlSession.selectList("com.example.mapper.DomainBMapper.listDomainBs");
    }
...

// Service implementation
public class MyServiceImpl implements MyService {
  @Inject
  private DbADao aDao;
  @Inject
  private DbBDao bDao;

  @Transactional(isolation = Isolation.SERIALIZABLE)
  @Override
  public Boolean doBusinessStuff() {
      // use aDao.
      // use bDao.
  }
...

// DbA Module
public class DbAModule extends PrivateModule {
    @Override
    protected void configure() {
        install(new XMLMyBatisModule() {
            @Override
            protected void initialize() {
                setEnvironmentId("test");
                setClassPathResource("mybatis-dbA-config.xml");

                useJdbcDriverClassLoaderoracle.jdbc.OracleDriver.class.getClassLoader());
                addProperties(createTestProperties());
            }
        });

        bind(SqlSession.class).annotatedWith(Names.named("DbA")).toProvider(SqlSessionManagerProvider.class).in(Scopes.SINGLETON);
        expose(SqlSession.class).annotatedWith(Names.named("DbA"));
    }

    protected static Properties createTestProperties() {
        final Properties myBatisProperties = new Properties();
        myBatisProperties.setProperty("mybatis.environment.id", "test");
        myBatisProperties.setProperty("JDBC.autoCommit", "false");
        return myBatisProperties;
    }
}

...
// DbB Module
// identical to the above replacing DbA with DbB

...

//
// GWTP Configurations
//

//
public class DaoServiceModule extends AbstractModule {
  @Override
  protected void configure() {
    bind(Service.class).to(ServiceImpl.class);
    bind(DbADao.class).to(DbADaoImpl.class);
    bind(DbBDao.class).to(DbBDaoImpl.class);
  }

public class GuiceServletConfig extends GuiceServletContextListener {
  @Override
  protected Injector getInjector() {
    return Guice.createInjector(new ServerModule(),
        new DispatchServletModule(),
        new DbAModule(), 
        new DbAModule(),
        new DaoServiceModule());
  }

这篇关于MyBatis-guice 3.3 +多个数据源+属性+ scriptrunner的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 16:32