play!连接mysql配置,play framework 数据库连接

接着上一篇的play framework redis缓存、作为一个优秀的框架怎么没有数据库操作呢、这篇介绍一下怎么配置jdbc连接、使用的数据库是mysql。

92395461370a

使用教程

build.sbt 添加依赖

libraryDependencies += javaJdbc

libraryDependencies += "mysql" % "mysql-connector-java" % "5.1.47"

jdbc配置

db {

default.driver = com.mysql.jdbc.Driver

default.url = "jdbc:mysql://localhost:3306/testdb?characterEncoding=UTF-8&useSSL=false"

default.username = root

default.password = root

default.logSql = true

}

控制器注入使用

@Singleton

class HomeController @Inject()(@NamedDatabase("default") db: Database, cc: ControllerComponents)(implicit assetsFinder: AssetsFinder)

extends AbstractController(cc) {

def index = Action {

val res = new mutable.ListBuffer[String]()

db.withConnection {

conn =>

val state = conn.createStatement()

val result = state.executeQuery("select * from user")

while (result.next()) {

res += result.getString("nickName")

}

}

Ok(views.html.index(res.mkString(",")))

}

}

刷新页面就可以看到啦

92395461370a

image

连接池配置

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

}

}

默认初始50个连接

验证mysql是不是

root@385ff263e2bd:/# mysqladmin -uroot -proot status

mysqladmin: [Warning] Using a password on the command line interface can be insecure.

Uptime: 6053 Threads: 51 Questions: 2898 Slow queries: 0 Opens: 569 Flush tables: 1 Open tables: 384 Queries per second avg: 0.478

可以看到Threads:是51个、有一个连接是其它项目的。

最后

play framework真的很好用、添加代码动态加载。真的很棒。

下一篇文章继续介绍在playframework中如何上传文件。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值