Introduction

Core components are contained in the org.mybatis.guice.*
package, providing a set of reusable Google Guice
javax.inject.Providers and
com.google.inject.Modules that
alleviate users the task to create MyBatis objects.

MyBatis Bootstrap

MyBatis offers an excellent APIs layer for the Bootstrap configuration that makes
it easy to write custom bootstrap - by default MyBatis comes with the XML loader -
and integrating 3rd part components.

The core component of the Guice approach is represented by the
org.mybatis.guice.MyBatisModule that's able to
create the core MyBatis org.apache.ibatis.session.SqlSessionFactory,
org.apache.ibatis.session.SqlSessionManager and
the user defined Mappers.

The best way to start is just adding the
org.mybatis.guice.MyBatisModule into the
com.google.inject.Injector
as shown in the example below and explain details step by step:

MyBatis properties

By design, we choose to reuse the default configuration
properties provided by Guice to let users feel free to read and
set them in any way you prefer; we suggest to put it in a
properties file, maybe filtered and set depending on which
environment users are building the application.

By default, if a configuration property is not specified, it will be ignored and
MyBatis will take care about proper default initialization. Users can initialize
properties using the proper setters OR by following method (please don't bind properties twice!):

The DataSource Provider

The javax.sql.DataSource Provider is one of
the two required providers that takes care about building and
injecting the used javax.sql.DataSource.

The mybatis-guice framework comes with some providers that
support the MyBatis-native Data Sources and other well known
Data Sources, C3P0,
Apache Commons DBCP and BoneCP but users are free to
implement their own javax.sql.DataSource
Provider and reference it in the org.mybatis.guice.MyBatisModule.

Please read the following chapter Data Source Providers
to obtain more informations about natively supported providers.

The Transaction Factory

Users are free to plug their preferred org.apache.ibatis.transaction.TransactionFactory:

and let Google Guice create the handlers instances and bind them to be injected to MyBatis components.
Constructor injection will not work, so use field or setter injection for type handler dependencies.

Configuring Interceptor Plugins

Users can easily add their preferred org.apache.ibatis.plugin.Interceptor
by invoking:

Multiple Datasources

It often happens that users need to interact with multiple schemas
in the same application, that means to have separate MyBatis
configurations.

Fortunately, the Google Guice com.google.inject.PrivateModule
comes to help us in a very simple and smart way, that will be shown in
the following example.

Let's take in consideration, to simplify the example, we have only
two datasources (but the same concept can be extended for an
arbitrary data sources number) one for the contacts
schema and another one for the companies schema.
So, all it has to do is installing the org.mybatis.guice.MyBatisModule
modules into the Google Guice com.google.inject.PrivateModule
as shown below:

The example shows how to use the org.mybatis.guice.MyBatisModule
to create two different MyBatis configurations in the same context.
Feel free to implement the getConnectionProperties()
method in the way you prefer! It could be, for example:

MyBatis XML Bootstrap

Users that want configure the MyBatis via the XML configuration, without loosing any single feature
of the org.mybatis.guice.MyBatisModule, can create their Injector using the
org.mybatis.guice.XMLMyBatisModule.

XMLMyBatisModule clients have just to instantiate it specifying

the MyBatis XML configuration file, located in the classpath, by default
the module will look for mybatis-config.xml in the root in the classpath;

the optional MyBatis environmentId, defaults to the default attribute of <environments> element;

the optional java.util.Properties to fill placeholders in the MyBatis XML configuration,
empty by default.