Skip to content

Instantly share code, notes, and snippets.

@mnesarco
Created February 14, 2012 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mnesarco/1828758 to your computer and use it in GitHub Desktop.
Save mnesarco/1828758 to your computer and use it in GitHub Desktop.
Mybatis Scala sample
/*
* Copyright 2011 The myBatis Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.scala.samples
import org.mybatis.scala.mapping._
import org.mybatis.scala.config._
import org.mybatis.scala.session._
// Model beans =================================================================
class Person {
var id : Int = _
var firstName : String = _
var lastName : String = _
}
// Data access layer ===========================================================
object DB {
// Simple select function
val findAll = new SelectList[String,Person] {
def xsql =
<xsql>
SELECT
id_ as id,
first_name_ as firstName,
last_name_ as lastName
FROM
person
WHERE
first_name_ LIKE #{{name}}
</xsql>
}
// Datasource configuration
val config = Configuration(
Environment(
"default",
new JdbcTransactionFactory(),
new PooledDataSource(
"org.postgresql.Driver",
"jdbc:postgresql://127.0.0.1:5432/scala",
"scala",
"test"
)
)
)
// Add the data access method to the default namespace
config += findAll
// Build the session manager
lazy val context = config.createPersistenceContext
}
// Application code ============================================================
object SelectSample {
// Do the Magic ...
def main(args : Array[String]) : Unit = {
DB.context.readOnly { implicit session =>
DB.findAll("%a%").foreach { p =>
println( "Person(%d): %s %s".format(p.id, p.firstName, p.lastName) )
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment