scalikejdbc 学习笔记
Posted AK47Sonic
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了scalikejdbc 学习笔记相关的知识,希望对你有一定的参考价值。
重用connection:
package com.citi.scalikejdbc import scalikejdbc._ import scalikejdbc.config._ object ConnReuse { def main(args: Array[String]): Unit = { DBsWithEnv("dev").setupAll() case class User(id: Int, name: String, age: Int) val allColumns = (rs: WrappedResultSet) => User( id = rs.int("id"), name = rs.string("name"), age = rs.int("age")) val id = 1 using(ConnectionPool.borrow()) { conn: java.sql.Connection => val db: DB = DB(conn) // set as auto-close disabled db.autoClose(false) db.localTx { implicit session => sql"update user set name = ‘tom‘ where id = ${id}".update.apply() } // localTx won‘t close the current Connection db.localTx { implicit session => sql"insert into user values (3, ‘marry‘, 12)".update.apply() } // localTx won‘t close the current Connection // this block also works fine! val users: List[User] = db.localTx { implicit session => SQL("select * from user limit 10").map(allColumns).list.apply() } for (user <- users) { println(user.id + "," + user.name + "," + user.age) } } DBsWithEnv("dev").closeAll() } }
运行结果:
[SQL Execution]
update user set name = ‘tom‘ where id = 1; (0 ms)
[Stack Trace]
...
com.citi.scalikejdbc.ConnReuse$$anonfun$main$1$$anonfun$2.apply(ConnReuse.scala:26)
com.citi.scalikejdbc.ConnReuse$$anonfun$main$1$$anonfun$2.apply(ConnReuse.scala:25)
scalikejdbc.DBConnection$$anonfun$3.apply(DBConnection.scala:326)
scalikejdbc.DBConnection$class.scalikejdbc$DBConnection$$rollbackIfThrowable(DBConnection.scala:290)
scalikejdbc.DBConnection$class.localTx(DBConnection.scala:319)
scalikejdbc.DB.localTx(DB.scala:60)
com.citi.scalikejdbc.ConnReuse$$anonfun$main$1.apply(ConnReuse.scala:25)
com.citi.scalikejdbc.ConnReuse$$anonfun$main$1.apply(ConnReuse.scala:19)
scalikejdbc.LoanPattern$class.using(LoanPattern.scala:18)
scalikejdbc.LoanPattern$.using(LoanPattern.scala:7)
scalikejdbc.package$.using(package.scala:60)
com.citi.scalikejdbc.ConnReuse$.main(ConnReuse.scala:19)
com.citi.scalikejdbc.ConnReuse.main(ConnReuse.scala)
...
22:30:30.747 [main] DEBUG scalikejdbc.StatementExecutor$$anon$1 - SQL execution completed
[SQL Execution]
insert into user values (3, ‘marry‘, 12); (1 ms)
[Stack Trace]
...
com.citi.scalikejdbc.ConnReuse$$anonfun$main$1$$anonfun$3.apply(ConnReuse.scala:30)
com.citi.scalikejdbc.ConnReuse$$anonfun$main$1$$anonfun$3.apply(ConnReuse.scala:29)
scalikejdbc.DBConnection$$anonfun$3.apply(DBConnection.scala:326)
scalikejdbc.DBConnection$class.scalikejdbc$DBConnection$$rollbackIfThrowable(DBConnection.scala:290)
scalikejdbc.DBConnection$class.localTx(DBConnection.scala:319)
scalikejdbc.DB.localTx(DB.scala:60)
com.citi.scalikejdbc.ConnReuse$$anonfun$main$1.apply(ConnReuse.scala:29)
com.citi.scalikejdbc.ConnReuse$$anonfun$main$1.apply(ConnReuse.scala:19)
scalikejdbc.LoanPattern$class.using(LoanPattern.scala:18)
scalikejdbc.LoanPattern$.using(LoanPattern.scala:7)
scalikejdbc.package$.using(package.scala:60)
com.citi.scalikejdbc.ConnReuse$.main(ConnReuse.scala:19)
com.citi.scalikejdbc.ConnReuse.main(ConnReuse.scala)
...
22:30:30.761 [main] DEBUG scalikejdbc.StatementExecutor$$anon$1 - SQL execution completed
[SQL Execution]
select * from user limit 10; (2 ms)
[Stack Trace]
...
com.citi.scalikejdbc.ConnReuse$$anonfun$main$1$$anonfun$4.apply(ConnReuse.scala:35)
com.citi.scalikejdbc.ConnReuse$$anonfun$main$1$$anonfun$4.apply(ConnReuse.scala:34)
scalikejdbc.DBConnection$$anonfun$3.apply(DBConnection.scala:326)
scalikejdbc.DBConnection$class.scalikejdbc$DBConnection$$rollbackIfThrowable(DBConnection.scala:290)
scalikejdbc.DBConnection$class.localTx(DBConnection.scala:319)
scalikejdbc.DB.localTx(DB.scala:60)
com.citi.scalikejdbc.ConnReuse$$anonfun$main$1.apply(ConnReuse.scala:34)
com.citi.scalikejdbc.ConnReuse$$anonfun$main$1.apply(ConnReuse.scala:19)
scalikejdbc.LoanPattern$class.using(LoanPattern.scala:18)
scalikejdbc.LoanPattern$.using(LoanPattern.scala:7)
scalikejdbc.package$.using(package.scala:60)
com.citi.scalikejdbc.ConnReuse$.main(ConnReuse.scala:19)
com.citi.scalikejdbc.ConnReuse.main(ConnReuse.scala)
...
1,tom,20
2,bill,30
3,marry,12
3,marry,12
以上是关于scalikejdbc 学习笔记的主要内容,如果未能解决你的问题,请参考以下文章
ScalaScala使用scalikejdbc工具连接MySQL(推荐)
[原创]java WEB学习笔记61:Struts2学习之路--通用标签 property,uri,param,set,push,if-else,itertor,sort,date,a标签等(代码片段