Configuration

The main responsibility of the org.mybatis.scala.config package is to provide classes to load the main configurations like plugins, datasource and transaction services, etc...

Because this API is code centric and type safe, you don't need typeAliases. When you need to reference a type 'Type' just use T[Type].

Your main configuration is specified in a small XML file as usual: http://mybatis.org/dtd/mybatis-3-config.dtd But you don't need to specify typeAliases nor mappers. Look at the mybatis core users guide for more details.

A sample configuration file could be:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
  <environments default="standalone">
    <environment id="standalone">
      <transactionManager type="JDBC" />
      <dataSource type="POOLED">
        <property name="driver" value="org.postgresql.Driver"/>
        <property name="url" value="jdbc:postgresql://127.0.0.1:5432/mydb"/>
        <property name="username" value="scalauser"/>
        <property name="password" value="test"/>
      </dataSource>
    </environment>
  </environments>
</configuration>

Once you have your configuration file, you can load it with the Configuration class:

val config = Configuration("mybatis.xml")

Once you have your main configuration in place, you should add one or more configuration spaces with your mappings.

val config = Configuration("mybatis.xml")
config.addSpace("mynamespace") { space =>
    space += findPeople                             // Adds mapped statement findPeople
    space ++= PersonDAO                             // Adds all mapped statements declared in PersonDAO
    space ++= Seq(deletePerson, updatePerson, ...)  // Adds all specified statements
}

Configuration instances should be created only once per application, just like the SqlSessionFactory.

All the configuration code is disconnected, so you can put it in a static initializer (scala object) or wherever you want.