Skip to content
aadrian edited this page Apr 26, 2017 · 6 revisions

You can use Mirage-SQL with other frameworks such as Spring Framework, Google Guice or Seasar2.

Spring Framework

Mirage-SQL can work with Spring Framework using SpringConnectionProvider instead of DefaultConnectionProvider.

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  <property name="driverClassName" value="org.h2.Driver" />
  <property name="url" value="jdbc:h2:tcp://localhost:9092/test" />
  <property name="username" value="sa" />
  <property name="password" value="" />
</bean>

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource"/>
</bean>

<bean id="connectionProvider" class="jp.sf.amateras.mirage.integration.spring.SpringConnectionProvider">
  <property name="transactionManager" ref="transactionManager" />
</bean>

<bean id="dialect" class="jp.sf.amateras.mirage.dialect.HyperSQLDialect"/>

<bean id="sqlManager" class="jp.sf.amateras.mirage.SqlManagerImpl">
  <property name="connectionProvider" ref="connectionProvider" />
  <property name="dialect" ref="dialect" />
</bean>

You can use SqlManager as follow in your code:

private SqlManager sqlManager;

public void setSqlManager(SqlManager sqlManager){
  this.sqlManager = sqlManager;
}

@Transactional
public void execute(){
  // Database access using SqlManager
  ...
}

In Spring Framework, JDBC connection information are defined in Spring bean definition file, and transaction is controlled by AOP. So you don't need to make jdbc.properties and don't need to use Session and SessionFactory.

Google Guice

You can use Mirage-SQL with Google Guice using MirageModule.

Injector injector = Guice.createInjector(new MirageModule());

Session session = injector.getInstance(Session.class);
SqlManager sqlManager = injector.getInstance(SqlManager.class);

session.begin();

try {
  // database access using SqlManager
  ...
  session.commit();

} catch(Exception ex){
  session.rollback();
  throw ex;
} finally {
  session.release();
}

MirageModule supports @Transactional for automatic transaction control. It applies TransactionInterceptor to methods of managed components. TransactionInterceptor begins and commits / rollbacks a transaction automatically around the applied method. Of course, you can use OpenSessionInViewFilter instead of @Transactional in the web application.

public class EmployeeDao {

  @Inject
  private SqlManager sqlManager;

  @Transactional
  public void insertEmployee(Employee employee){
    sqlManager.insertEntity(employee);
  }

}

Injector injector = Guice.createInjector(new MirageModule());
EmployeeDao dao = injector.getInstance(EmployeeDao.class);

Employee employee = new Employee();
employee.empId = 1;
employee.name = "Naoki Takezoe";

dao.insertEmployee(employee);

Seasar2

Seasar2 is a DI container which is used in Japan. Seasar2 already has a O/R mapper named S2JDBC. S2JDBC is similar to Mirage-SQL because we referred S2JDBC in various respects to design Mirage-SQL. However you can use Mirage-SQL instead of S2JDBC.

Register SqlManager and SeasarConnectionProvider into your dicon file as follows:

<component class="jp.sf.amateras.mirage.integration.seasar.SeasarConnectionProvider" />
<component class="jp.sf.amateras.mirage.SqlManagerImpl" />

You can use SqlManager as follow in your code:

@Resource
protected SqlManager sqlManager;

public void execute(){
  // Database access using SqlManager
  ...
}
Clone this wiki locally