Package

org.mybatis

scala

Permalink

package scala

Visibility
  1. Public
  2. All

Value Members

  1. package cache

    Permalink

    Provides Cache supporting types and objects

  2. package config

    Permalink

    Provides main configuration classes.

    Provides main configuration classes.

    Basic usage

    Usual steps are:

    • Load the configuration from an external XML file
    • Add one or more configuration spaces with mapped statements
    • Create the persistenceContext

    Code sample

    val config = Configuration("META-INF/mybatis.xml")
    config.addSpace("ns") { space =>
      space ++= MyDAO
    }
    val persistenceContext = config.createPersistenceContext

    Another example without external XML file and with default namespace

    val config = Configuration(
     Environment(
       "default",
       new JdbcTransactionFactory(),
       new PooledDataSource(
         "org.hsqldb.jdbcDriver",
         "jdbc:hsqldb:mem:scala",
         "sa",
         ""
       )
     )
    )
    config ++= MyDAO
    
    val persistenceContext = config.createPersistenceContext
  3. package mapping

    Permalink

    Statement and result mapping classes.

    Statement and result mapping classes.

    Code sample

    Sample database

    CREATE TABLE people_group (
       id_ serial,
       name_ varchar(255),
       primary key (id_)
    );
    
    CREATE TABLE person (
       id_ serial,
       first_name_ varchar(255),
       last_name_ varchar(255),
       group_id_ integer not null,
       primary key (id_),
       foreign key (group_id_) references people_group(id_)
    );
    
    CREATE TABLE contact_info (
       id_ serial,
       owner_id_ integer not null,
       street_address_ varchar(255),
       phone_number_ varchar(20),
       primary key (id_),
       foreign key (owner_id_) references person(id_)
    );

    Sample Model

    // Simple Group POJO
    class Group {
      var id : Int = _
      var name : String = _
    }
    
    // Simple ContactInfo POJO
    class ContactInfo {
      var id : Int = _
      var address : String = _
      var phone : String = _
    }
    
    // Simple Person POJO
    class Person {
      var id : Int = _
      var firstName : String = _
      var lastName : String = _
      var group : Group = _
      var contact : java.util.List[ContactInfo] = _
    }

    Sample Mapping

    // Define the result mapping
    resultMap = new ResultMap[Person] {
    
      id(property="id", column="id_")
      result(property="firstName", column="first_name_")
      result(property="lastName", column="last_name_")
    
      association[Group] (property="group", column="group_id_",
        resultMap= new ResultMap[Group] {
          id(property="id", column="group_id_")
          result(property="name", column="group_name_")
        }
      )
    
      collection[ContactInfo] (property="contact", column="cinfo_id_",
        resultMap= new ResultMap[ContactInfo] {
          id(property="id", column="cinfo_id_")
          result(property="address", column="street_address_")
          result(property="phone", column="phone_number_")
        }
      )
    }
  4. package session

    Permalink

    Session Management classes.

    Session Management classes. Provides classes needed to execute the mapped statements.

    Basic usage

    Usual steps are:

    • Obtain a SessionManager instance from a Configuration
    • Call any of the lifecycle methods of the SessionManager passing the code to be executed.

    Code sample

    val db = Config.persistenceContext
    db.transaction { implicit session =>
      MyDAO.insert(...)
      MyDAO.update(...)
      // etc...
    }
    Version

    $Revision$

Ungrouped