scala play framework 框架配置

sbt 配置

name := "myle"
 
version := "1.0" 
      
lazy val `myle` = (project in file(".")).enablePlugins(PlayScala)

resolvers += "scalaz-bintray" at "https://dl.bintray.com/scalaz/releases"
      
resolvers += "Akka Snapshot Repository" at "https://repo.akka.io/snapshots/"
      
scalaVersion := "2.12.6"

libraryDependencies ++= Seq( jdbc , ehcache , ws , specs2 % Test , guice )

unmanagedResourceDirectories in Test <+=  baseDirectory ( _ /"target/web/public/test" )

libraryDependencies ++= Seq(javaJdbc, "mysql" % "mysql-connector-java" % "5.1.18")

application.conf 配置

db {
  default.driver = com.mysql.jdbc.Driver
  default.url = "jdbc:mysql://localhost:3306/my_db?characterEncoding=UTF-8&useSSL=false"
  default.username = root
  default.password = 123
  default.logSql = true
}

play.db {
  # The combination of these two settings results in "db.default" as the
  # default JDBC pool:
  config = "db"
  default = "default"

  # Play uses HikariCP as the default connection pool.  You can override
  # settings by changing the prototype:
  prototype {
    # Sets a fixed JDBC connection pool size of 50
    hikaricp.minimumIdle = 50
    hikaricp.maximumPoolSize = 50
  }
}

自定义一个controller


@Singleton
class UserController @Inject()(@NamedDatabase("default") db: Database, val controllerComponents: ControllerComponents) extends BaseController {

  def index = Action{

    val res = new mutable.ListBuffer[Map[String,Object]]()
    db.withConnection {
      conn =>
        val state = conn.createStatement()
        val result = state.executeQuery("select * from sys_user")
        while (result.next()) {
          val username = result.getString("username")
          val age = result.getString("age")
          val addr = result.getString("addr")
          res+=Map("username"->username,"age"->age,"addr"->addr)
        }
        conn.close()
    }

    res.foreach(println(_))

    Ok(views.html.userList("res.toList"))
  }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值