Powerful persistence layer           withGoogle Guice & MyBatis     GuiceCon, March 19th, 2011               Simone Tripod...
MyBatis?!?
It reminds Apache iBATIS!Indeed, MyBatis is the Apache iBATIS fork!● Fork happened on 2010;● The whole original team moved...
The Guice integration early days...● Announced as independent project on GoogleCode on December 2009 with iBaGuice name;● ...
... and the merge with MyBatis● Donated to MyBatis after the iBATIS fork;● Community supported;● Same objectives, better A...
MyBatis Confguration & BootstrapcreateInjector(new MyBatisModule() {             @Override             protected void init...
MyBatis XML Confguration                        & BootstrapcreateInjector(new XMLMyBatisModule() {             @Override  ...
Require Mappers injectionpublic interface ContactMapper {    void brokenAdd(Contact contact);    void add(Contact contact)...
rd                        3 parties DataSourcecreateInjector(new XMLMyBatisModule() {             @Override             pr...
Issue: boring transactions!public class FooDao {    @Inject    private SqlSessionManager sessionManager;    public void se...
Solution: @Transactionalpublic class FooDao {    @Inject    private FooMapper fooMapper;    @Inject    private BarMapper b...
JDBC HelperSmall issue: remember JDBC URL patterns and driverclass names● Create index/catalog? :(● Create automatic index...
JDBCHelper in action● Just a module that automatically injects JDBC.url andJDBC.driver properties used by DataSource provi...
ReferencesMyBatis home page: http://mybatis.org/Google-Guice integration module:http://code.google.com/p/mybatis/downloads...
G R A Z I E :)
Powerful persistence layer with Google Guice & MyBatis
Upcoming SlideShare
Loading in …5
×

Powerful persistence layer with Google Guice & MyBatis

5,915 views

Published on

Published in: Technology

Powerful persistence layer with Google Guice & MyBatis

  1. 1. Powerful persistence layer withGoogle Guice & MyBatis GuiceCon, March 19th, 2011 Simone Tripodi simonetripodi@apache.org http://people.apache.org/~simonetripodi
  2. 2. MyBatis?!?
  3. 3. It reminds Apache iBATIS!Indeed, MyBatis is the Apache iBATIS fork!● Fork happened on 2010;● The whole original team moved the project onGoogle Code;● Apache iBATIS moved to Attic and no moremaintained;● New extensions after left ASF.
  4. 4. The Guice integration early days...● Announced as independent project on GoogleCode on December 2009 with iBaGuice name;● Only 2 folks behind it: Simone Tripodi & MarcoSperanza;● Focused on providing a smart iBATIS bootstrap andrelated component injection.
  5. 5. ... 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.
  6. 6. MyBatis Confguration & BootstrapcreateInjector(new MyBatisModule() { @Override protected void initialize() { environmentId("test"); bindDataSourceProviderType(PooledDataSourceProvider.class); bindTransactionFactoryType(JdbcTransactionFactory.class); addMapperClass(ContactMapper.class); handleType(CustomType.class).with(CustomLongTypeHandler.class); handleType(Address.class).with(AddressTypeHandler.class); addInterceptorClass(CountUpdateInterceptor.class); } });
  7. 7. MyBatis XML Confguration & BootstrapcreateInjector(new XMLMyBatisModule() { @Override protected void initialize() { setEnvironmentId("test"); setClassPathResource("org/acme/mybatis-config.xml"); } });
  8. 8. Require Mappers injectionpublic interface ContactMapper { void brokenAdd(Contact contact); void add(Contact contact); void update(Contact contact); void delete(Integer id); Contact getById(Integer id); List<Contact> selectAll();}@javax.inject.Singletonpublic class ContactMapperClient { @javax.inject.Inject private ContactMapper contactMapper; public void setContactMapper(ContactMapper contactMapper) { this.contactMapper = contactMapper; }}
  9. 9. rd 3 parties DataSourcecreateInjector(new XMLMyBatisModule() { @Override protected void initialize() { // MyBatis built-in bindDataSourceProviderType(UnpooledDataSourceProvider.class); bindDataSourceProviderType(PooledDataSourceProvider.class); bindDataSourceProviderType(JndiDataSourceProvider.class); // c3p0 bindDataSourceProviderType(C3p0DataSourceProvider.class); // apache commons-dbcp bindDataSourceProviderType(BasicDataSourceProvider.class); bindDataSourceProviderType(SharedPoolDataSourceProvider.class); bindDataSourceProviderType(PerUserPoolDataSourceProvider.class); // BoneCP bindDataSourceProviderType(BoneCPProvider.class); } });
  10. 10. 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(); }}
  11. 11. Solution: @Transactionalpublic class FooDao { @Inject private FooMapper fooMapper; @Inject private BarMapper barMapper; // lets 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(); }}
  12. 12. JDBC HelperSmall issue: remember JDBC URL patterns and driverclass names● Create index/catalog? :(● Create automatic index/catalog! :)
  13. 13. JDBCHelper in action● Just a module that automatically injects JDBC.url andJDBC.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, ...);
  14. 14. ReferencesMyBatis home page: http://mybatis.org/Google-Guice integration module:http://code.google.com/p/mybatis/downloads/list?can=3&q=Product%3DGuiceGoogle-Guice integration SVN:http://mybatis.googlecode.com/svn/sub-projects/mybatis-guice/trunk/
  15. 15. G R A Z I E :)

×