-
Notifications
You must be signed in to change notification settings - Fork 24
Integration
You can use Mirage-SQL with other frameworks such as Spring Framework, Google Guice or Seasar2.
Mirage-SQL can work with the 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="com.miragesql.miragesql.integration.spring.SpringConnectionProvider">
<property name="transactionManager" ref="transactionManager" />
</bean>
<bean id="dialect" class="com.miragesql.miragesql.dialect.HyperSQLDialect"/>
<bean id="sqlManager" class="com.miragesql.miragesql.SqlManagerImpl">
<property name="connectionProvider" ref="connectionProvider" />
<property name="dialect" ref="dialect" />
</bean>
You can use SqlManager
as follows in your code:
private SqlManager sqlManager;
public void setSqlManager(SqlManager sqlManager){
this.sqlManager = sqlManager;
}
@Transactional
public void execute(){
// Database access using SqlManager
...
}
With the Spring Framework, JDBC connection information is defined in Spring bean definition file, and transactions are controlled by the AOP. So one does not need to make a jdbc.properties
or to use Session
and SessionFactory
.
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 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="com.miragesql.miragesql.integration.seasar.SeasarConnectionProvider" />
<component class="com.miragesql.miragesql.SqlManagerImpl" />
You can use SqlManager
as follow in your code:
@Resource
protected SqlManager sqlManager;
public void execute(){
// Database access using SqlManager
...
}