Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Immutable case class mapping example added

  • Loading branch information...
commit 60775a45307052a275eb54f18be1770f1abc7deb 1 parent d9da4af
Frank David Martínez M authored
96 src/main/scala/org/mybatis/scala/samples/select/SelectImmutableSample.scala
... ... @@ -0,0 +1,96 @@
  1 +/*
  2 + * Copyright 2011-2012 The myBatis Team
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +
  17 +package org.mybatis.scala.samples.select
  18 +
  19 +import org.mybatis.scala.mapping._
  20 +import org.mybatis.scala.config._
  21 +import org.mybatis.scala.session._
  22 +import org.mybatis.scala.samples.util._
  23 +
  24 +// Model beans (Immutable case class) ==========================================
  25 +
  26 +case class CPerson(id : Int, firstName : String, lastName : String)
  27 +
  28 +// Data access layer ===========================================================
  29 +
  30 +object CDB {
  31 +
  32 + // Simple select function
  33 + val findAll = new SelectListBy[String,CPerson] {
  34 +
  35 + // Constructor Mapping (Warning: Order is important)
  36 + resultMap = new ResultMap[CPerson] {
  37 + idArg(column="id_", javaType=T[Int])
  38 + arg(column="first_name_", javaType=T[String])
  39 + arg(column="last_name_", javaType=T[String])
  40 + }
  41 +
  42 + def xsql =
  43 + """
  44 + SELECT
  45 + id_, first_name_, last_name_
  46 + FROM
  47 + person
  48 + WHERE
  49 + first_name_ LIKE #{name}
  50 + """
  51 + }
  52 +
  53 + // Datasource configuration
  54 + val config = Configuration(
  55 + Environment(
  56 + "default",
  57 + new JdbcTransactionFactory(),
  58 + new PooledDataSource(
  59 + "org.hsqldb.jdbcDriver",
  60 + "jdbc:hsqldb:mem:scala",
  61 + "sa",
  62 + ""
  63 + )
  64 + )
  65 + )
  66 +
  67 + // Add the data access method to the default namespace
  68 + config += findAll
  69 + config ++= DBSchema
  70 + config ++= DBSampleData
  71 +
  72 + // Build the session manager
  73 + lazy val context = config.createPersistenceContext
  74 +
  75 +}
  76 +
  77 +// Application code ============================================================
  78 +
  79 +object SelectImmutableSample {
  80 +
  81 + // Do the Magic ...
  82 + def main(args : Array[String]) : Unit = {
  83 + CDB.context.transaction { implicit session =>
  84 +
  85 + DBSchema.create
  86 + DBSampleData.populate
  87 +
  88 + CDB.findAll("%a%").foreach {
  89 + case CPerson(id, firstName, lastName) =>
  90 + println( "Person(%d): %s %s".format(id, firstName, lastName) )
  91 + }
  92 +
  93 + }
  94 + }
  95 +
  96 +}

0 comments on commit 60775a4

Please sign in to comment.
Something went wrong with that request. Please try again.