Getting started

MyBatis-Guice helps you integrate your MyBatis code seamlessly with Google Guice. Using the classes in this library, Google Guice will load the necessary MyBatis classes for you. This library also provides an easy way to inject MyBatis data mappers and SqlSessions into your application beans. Finally, MyBatis-Guice will let you demarcate transactions declaratively so you won't need to commit/rollback/close them by hand.

Installation

Installing the mybatis-guice module it is very easy, just put the mybatis-guice-3.11-SNAPSHOT.jar and dependencies in the classpath!

Apache Maven users instead can easily adding the following dependency in their POMs :

<dependencies>
...
  <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-guice</artifactId>
    <version>3.11-SNAPSHOT</version>
  </dependency>
...
</dependencies>

Quick Setup

To use MyBatis with Guice you need to set up a MyBatisModule, with a DataSource at least one data mapper class and a transactional bean to be injected with the mapper.

To setup a DataSource you can simply use the JdbcHelper (please see the related appendix for more informations) module to build the URL needed for your database and provide the connection properties:

Properties myBatisProperties = new Properties();
myBatisProperties.setProperty("mybatis.environment.id", "test");
myBatisProperties.setProperty("JDBC.schema", "mybatis-guice_TEST");
myBatisProperties.setProperty("derby.create", "true");
myBatisProperties.setProperty("JDBC.username", "sa");
myBatisProperties.setProperty("JDBC.password", "");
myBatisProperties.setProperty("JDBC.autoCommit", "false");

Injector injector = Guice.createInjector(
    JdbcHelper.HSQLDB_Embedded,
    new Module() {
        public void configure(Binder binder) {
            Names.bindProperties(binder, myBatisProperties);
        }
    }
);

Assume you have a data mapper class defined like the following:

public interface UserMapper {

    @Select("SELECT * FROM user WHERE id = #{userId}")
    User getUser(@Param("userId") String userId);

}
Note that the mapper class specified must be an interface, not an actual implementation class. In this example, annotations are used to specify the SQL, but a MyBatis mapper XML file could also be used.

Assume you also have a transactional service bean that uses your mapper:

public class FooServiceMapperImpl implements FooService {

    @Inject
    private UserMapper userMapper;

    @Transactional
    public User doSomeBusinessStuff(String userId) {
        return this.userMapper.getUser(userId);
    }

}

Setup a MyBatisModule, add your mapper to it and bind also your transactional service interface FooService to its implementation.

public interface UserMapper {
...
Injector injector = Guice.createInjector(
    new MyBatisModule() {

        @Override
        protected void initialize() {
            install(JdbcHelper.HSQLDB_Embedded);

            bindDataSourceProviderType(PooledDataSourceProvider.class);
            bindTransactionFactoryType(JdbcTransactionFactory.class);
            addMapperClass(UserMapper.class);

            Names.bindProperties(binder(), createTestProperties());
            bind(FooService.class).to(FooServiceMapperImpl.class);
        }

    }
);

This is all you need, you can now get an instance of your service. It will be automatically injected with the MyBatis mapper. With the mapper calling MyBatis data methods is only one line of code. Besides, all operations will be transactional, that means that you will not need to commit or rollback any connection.

FooService fooService = this.injector.getInstance(FooService.class);
fooService.doSomeBusinessStuff("data");