play slick3 实例
所属分类 play
浏览量 698
https://github.com/pedrorijo91/play-slick3-steps
https://gitee.com/dyyx/hellocode/tree/master/demo/scala/play-slick3-steps
docker 安装 MySQL
创建数据库 CREATE DATABASE example;
docker 部署 MySQL
application.conf
slick.dbs.default.profile = "slick.jdbc.MySQLProfile$"
slick.dbs.default.db.driver = "com.mysql.jdbc.Driver"
slick.dbs.default.db.url = "jdbc:mysql://localhost/example?serverTimezone=UTC"
slick.dbs.default.db.user = "root"
slick.dbs.default.db.password = "123456"
数据库初始化
conf/evolutions/default/1.sql
-- User schema
-- !Ups
create table `user` (
`id` BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`first_name` TEXT NOT NULL,
`last_name` TEXT NOT NULL,
`mobile` BIGINT NOT NULL,
`email` TEXT NOT NULL
)
-- !Downs
drop table `user`
数据库访问类
models/User.scala
case class User(id: Long, firstName: String, lastName: String, mobile: Long, email: String)
class Users
services.UserService
controller
controllers.ApplicationController
def addUser() = Action.async { implicit request: Request[AnyContent] =>
UserForm.form.bindFromRequest.fold(
// if any error in submitted data
errorForm => {
logger.warn(s"Form submission with error: ${errorForm.errors}")
Future.successful(Ok(views.html.index(errorForm, Seq.empty[User])))
},
data => {
val newUser = User(0, data.firstName, data.lastName, data.mobile, data.email)
userService.addUser(newUser).map( _ => Redirect(routes.ApplicationController.index()))
})
}
def bindFromRequest()(implicit request: play.api.mvc.Request[_]): Form[T] = {
sbt 创建运行 Scala play 项目
上一篇
下一篇
kubectl命令介绍
kubectl常用命令
scala class 和 object 的 apply 方法
scala implicit 参数实例
scala运算符 :: :+ +: ::: ++
SBT依赖 % 与 %% 的区别