使用scalikejdbc config (src\main\resources)
# MySQL(dev) dev.db.default.driver="com.mysql.jdbc.Driver" dev.db.default.url="jdbc:mysql://192.168.1.151:3306/scalalearning?useUnicode=true&characterEncoding=UTF8" dev.db.default.user="root" dev.db.default.password="12345" # Connection Pool settings dev.db.default.poolInitialSize=5 dev.db.default.poolMaxSize=7 dev.db.default.poolConnectionTimeoutMillis=1000 dev.db.default.poolValidationQuery="select 1 as one" # MySQL(prod) prod.db.default.driver="com.mysql.jdbc.Driver" prod.db.default.url="jdbc:mysql://192.168.1.151:3306/scalalearning?useUnicode=true&characterEncoding=UTF8" prod.db.default.user="root" prod.db.default.password="12345" # Connection Pool settings prod.db.default.poolInitialSize=5 prod.db.default.poolMaxSize=7 prod.db.default.poolConnectionTimeoutMillis=1000 prod.db.default.poolValidationQuery="select 1 as one"
import scalikejdbc._ import scalikejdbc.config._ object ConfigDemo { 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 users: List[User] = DB readOnly { 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() } }
时间: 2024-10-05 19:08:10