SqlSessionFactoryBuilder

The SqlSessionFactoryBuilder has five build() methods, each which allows you to build a SqlSessionFactory from a different source.

  1. SqlSessionFactory build(InputStream inputStream)
  2. SqlSessionFactory build(InputStream inputStream, String environment)
  3. SqlSessionFactory build(InputStream inputStream, Properties properties)
  4. SqlSessionFactory build(InputStream inputStream, String env, Properties props)
  5. SqlSessionFactory build(Configuration config)

The first four methods are the most common, as they take an InputStream instance that refers to an XML document, or more specifically, the mybatis-config.xml file discussed above. The optional parameters are environment and properties. Environment determines which environment to load, including the datasource and transaction manager. For example:

  1. <environments default="development">
  2. <environment id="development">
  3. <transactionManager type="JDBC">
  4. ...
  5. <dataSource type="POOLED">
  6. ...
  7. </environment>
  8. <environment id="production">
  9. <transactionManager type="MANAGED">
  10. ...
  11. <dataSource type="JNDI">
  12. ...
  13. </environment>
  14. </environments>

If you call a build method that takes the environment parameter, then MyBatis will use the configuration for that environment. Of course, if you specify an invalid environment, you will receive an error. If you call one of the build methods that does not take the environment parameter, then the default environment is used (which is specified as default=”development” in the example above).

If you call a method that takes a properties instance, then MyBatis will load those properties and make them available to your configuration. Those properties can be used in place of most values in the configuration using the syntax: ${propName}

Recall that properties can also be referenced from the mybatis-config.xml file, or specified directly within it. Therefore it’s important to understand the priority. We mentioned it earlier in this document, but here it is again for easy reference:


If a property exists in more than one of these places, MyBatis loads them in the following order.

  • Properties specified in the body of the properties element are read first,
  • Properties loaded from the classpath resource or url attributes of the properties element are read second, and override any duplicate properties already specified,
  • Properties passed as a method parameter are read last, and override any duplicate properties that may have been loaded from the properties body and the resource/url attributes.

Thus, the highest priority properties are those passed in as a method parameter, followed by resource/url attributes and finally the properties specified in the body of the properties element.


So to summarize, the first four methods are largely the same, but with overrides to allow you to optionally specify the environment and/or properties. Here is an example of building a SqlSessionFactory from an mybatis-config.xml file.

  1. String resource = "org/mybatis/builder/mybatis-config.xml";
  2. InputStream inputStream = Resources.getResourceAsStream(resource);
  3. SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
  4. SqlSessionFactory factory = builder.build(inputStream);

Notice that we’re making use of the Resources utility class, which lives in the org.apache.ibatis.io package. The Resources class, as its name implies, helps you load resources from the classpath, filesystem or even a web URL. A quick look at the class source code or inspection through your IDE will reveal its fairly obvious set of useful methods. Here’s a quick list:

  1. URL getResourceURL(String resource)
  2. URL getResourceURL(ClassLoader loader, String resource)
  3. InputStream getResourceAsStream(String resource)
  4. InputStream getResourceAsStream(ClassLoader loader, String resource)
  5. Properties getResourceAsProperties(String resource)
  6. Properties getResourceAsProperties(ClassLoader loader, String resource)
  7. Reader getResourceAsReader(String resource)
  8. Reader getResourceAsReader(ClassLoader loader, String resource)
  9. File getResourceAsFile(String resource)
  10. File getResourceAsFile(ClassLoader loader, String resource)
  11. InputStream getUrlAsStream(String urlString)
  12. Reader getUrlAsReader(String urlString)
  13. Properties getUrlAsProperties(String urlString)
  14. Class classForName(String className)

The final build method takes an instance of Configuration. The Configuration class contains everything you could possibly need to know about a SqlSessionFactory instance. The Configuration class is useful for introspecting on the configuration, including finding and manipulating SQL maps (not recommended once the application is accepting requests). The configuration class has every configuration switch that you’ve learned about already, only exposed as a Java API. Here’s a simple example of how to manually a Configuration instance and pass it to the build() method to create a SqlSessionFactory.

  1. DataSource dataSource = BaseDataTest.createBlogDataSource();
  2. TransactionFactory transactionFactory = new JdbcTransactionFactory();
  3. Environment environment = new Environment("development", transactionFactory, dataSource);
  4. Configuration configuration = new Configuration(environment);
  5. configuration.setLazyLoadingEnabled(true);
  6. configuration.setEnhancementEnabled(true);
  7. configuration.getTypeAliasRegistry().registerAlias(Blog.class);
  8. configuration.getTypeAliasRegistry().registerAlias(Post.class);
  9. configuration.getTypeAliasRegistry().registerAlias(Author.class);
  10. configuration.addMapper(BoundBlogMapper.class);
  11. configuration.addMapper(BoundAuthorMapper.class);
  12. SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
  13. SqlSessionFactory factory = builder.build(configuration);

Now you have a SqlSessionFactory that can be used to create SqlSession instances.