3. It reminds Apache iBATIS!
Indeed, MyBatis is the Apache iBATIS fork!
● Fork happened on 2010;
● The whole original team moved the project on
Google Code;
● Apache iBATIS moved to Attic and no more
maintained;
● New extensions after left ASF.
4.
5. The Guice integration early days...
● Announced as independent project on Google
Code on December 2009 with iBaGuice name;
● Only 2 folks behind it: Simone Tripodi & Marco
Speranza;
● Focused on providing a smart iBATIS bootstrap and
related component injection.
6. ... and the merge with MyBatis
● Donated to MyBatis after the iBATIS fork;
● Community supported;
● Same objectives, better APIs;
● More 3
rd
parties integration:
● Apache Commons DBCP;
● Bone CP;
● C3P0;
● Built-in MyBatis DataSource.
11. Issue: boring transactions!
public class FooDao {
@Inject
private SqlSessionManager sessionManager;
public void setSessionManager(SqlSessionManager sessionManager) {
this.sessionManager = sessionManager;
}
public void doFooBar() throws MyDaoException {
this.sessionManager.startManagedSession(ExecutorType.BATCH,
TransactionIsolationLevel.READ_UNCOMMITTED);
try {
FooMapper fooMapper = this.sessionManager.getMapper(FooMapper.class);
fooMapper.doFoo();
BarMapper barMapper = this.sessionManager.getMapper(BarMapper.class);
barMapper.doBar();
this.sessionManager.commit();
} catch (Throwable t) {
this.sessionManager.rollback();
throw new MyDaoException("Something went wrong", t);
} finally {
this.sessionManager.close();
}
}
12. Solution: @Transactional
public class FooDao {
@Inject
private FooMapper fooMapper;
@Inject
private BarMapper barMapper;
// let's assume setters here
@Transactional(executorType = ExecutorType.BATCH,
isolationLevel = TransactionIsolationLevel.READ_UNCOMMITTED,
rethrowExceptionsAs = MyDaoException.class,
exceptionMessage = "Something went wrong")
public void doFooBar() {
this.fooMapper.doFoo();
this.barMapper.doBar();
}
}
13. JDBC Helper
Small issue: remember JDBC URL patterns and driver
class names
● Create index/catalog? :(
● Create automatic index/catalog! :)
14. JDBCHelper in action
● Just a module that automatically injects JDBC.url and
JDBC.driver properties used by DataSource providers;
● JDBC.url is a template;
● Pattern contains variables in ${name|defaultValue} format;
● Patterns require other properties, i.e.:
jdbc:mysql://${JDBC.host|localhost}:${JDBC.port|3306}/${JDBC.schema}
● Sample?
createInjector(JdbcHelper.MySQL, ...);