http://www.boyunjian.com/
转载: http://blog.csdn.net/zsuczw/article/details/50276555
Play框架提供了对Slick的集成模块play-slick。本文将介绍从后台数据库到前台请求的的全过程。
一、数据库建模
<code class="language-scala hljs has-numbering"><span class="hljs-class"><span class="hljs-keyword">case</span> <span class="hljs-keyword">class</span> <span class="hljs-title">User</span><span class="hljs-params">(name:String, password:String,id:Option[Long] = None)</span></span> <span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">User</span> {</span> implicit <span class="hljs-keyword">val</span> userWrites: Writes[User] = ( (JsPath \ <span class="hljs-string">"name"</span>).write[String] and (JsPath \ <span class="hljs-string">"password"</span>).write[String] and (JsPath \ <span class="hljs-string">"id"</span>).writeNullable[Long] )(unlift(User.unapply)) implicit <span class="hljs-keyword">val</span> userReads: Reads[User] = ( (JsPath \ <span class="hljs-string">"name"</span>).read[String] and (JsPath \ <span class="hljs-string">"password"</span>).read[String] and (JsPath \ <span class="hljs-string">"id"</span>).readNullable[Long] )(User.apply _) }</code>
注意:id是一个可选的属性,必须放在最后面,否则会导致play在Json转换的时候报错。
建表:mysql. 注意 ID要定义为自动增长的列。
<code class="language-sql hljs has-numbering"><span class="hljs-operator"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> <span class="hljs-keyword">USER</span> ( ID <span class="hljs-keyword">INT</span> <span class="hljs-keyword">NOT</span> <span class="hljs-keyword">NULL</span> AUTO_INCREMENT, NAME <span class="hljs-keyword">VARCHAR</span>(<span class="hljs-number">40</span>) <span class="hljs-keyword">NOT</span> <span class="hljs-keyword">NULL</span>, PASSWORD <span class="hljs-keyword">VARCHAR</span>(<span class="hljs-number">40</span>) <span class="hljs-keyword">NOT</span> <span class="hljs-keyword">NULL</span>, <span class="hljs-keyword">PRIMARY</span> <span class="hljs-keyword">KEY</span>(ID) );</span></code>
二、Slick schema
<code class="language-scala hljs has-numbering"><span class="hljs-keyword">import</span> model._ <span class="hljs-keyword">import</span> slick.driver.JdbcProfile <span class="hljs-class"><span class="hljs-keyword">trait</span> <span class="hljs-title">UserSchema</span> {</span> <span class="hljs-keyword">protected</span> <span class="hljs-keyword">val</span> driver: JdbcProfile <span class="hljs-keyword">import</span> driver.api._ <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UserTable</span><span class="hljs-params">(tag: Tag)</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Table</span>[<span class="hljs-title">User</span>]<span class="hljs-params">(tag, <span class="hljs-string">"USER"</span>)</span> {</span> <span class="hljs-keyword">def</span> id = column[Long](<span class="hljs-string">"ID"</span>, O.PrimaryKey, O.AutoInc) <span class="hljs-keyword">def</span> name = column[String](<span class="hljs-string">"NAME"</span>) <span class="hljs-keyword">def</span> password = column[String](<span class="hljs-string">"PASSWORD"</span>) <span class="hljs-keyword">def</span> * = (name, password, id.?) <> (User.tupled, User.unapply _) } }</code>
三、Play Action定义
列出所有的用户
<code class="language-scala hljs has-numbering"> <span class="hljs-keyword">def</span> usersList = Action.async { implicit request => { dbConfig.db.run(Users.result).map(res => Ok(Json.toJson(res.toList)) ) } }</code>
创建一个用户
<code class="language-scala hljs has-numbering"> <span class="hljs-keyword">def</span> addUser = Action.async(BodyParsers.parse.json) { implicit request => { <span class="hljs-keyword">val</span> user = request.body.validate[User].get <span class="hljs-keyword">val</span> insertQuery = ( Users returning Users.map(_.id)) += user dbConfig.db.run(insertQuery).map { result => Ok(Json.toJson(user.copy(id = Some(result)))) } } }</code>
删除一个用户
<code class="language-scala hljs has-numbering"> <span class="hljs-keyword">def</span> deleteUser(id: Long) = Action { <span class="hljs-keyword">val</span> query = Users.filter(_.id === id).delete dbConfig.db.run(query) Ok(<span class="hljs-string">"ok"</span>) }</code>
更新一个用户
<code class="language-scala hljs has-numbering"><span class="hljs-keyword">def</span> updateUser(id: Long) = Action.async(BodyParsers.parse.json) { implicit request => { <span class="hljs-keyword">val</span> newUser = request.body.validate[User].get <span class="hljs-keyword">val</span> query = Users.filter(_.id === newUser.id).update(newUser) dbConfig.db.run(query).map { result => Ok(Json.toJson(newUser.id)) } } }</code>
application.conf配置
<code class="language-scala hljs has-numbering">slick.dbs.<span class="hljs-keyword">default</span>.driver= <span class="hljs-string">"slick.driver.MySQLDriver$"</span> slick.dbs.<span class="hljs-keyword">default</span>.db.driver=<span class="hljs-string">"com.mysql.jdbc.Driver"</span> slick.dbs.<span class="hljs-keyword">default</span>.db.properties.driver=<span class="hljs-string">"com.mysql.jdbc.Driver"</span> slick.dbs.<span class="hljs-keyword">default</span>.db.user=<span class="hljs-string">"root"</span> slick.dbs.<span class="hljs-keyword">default</span>.db.password=<span class="hljs-string">"root"</span> slick.dbs.<span class="hljs-keyword">default</span>.db.url=<span class="hljs-string">"jdbc:mysql://localhost:3306/xyz?useUnicode=true&characterEncoding=UTF-8"</span></code>
然后再定义Controller时,直接注入配置好得DB连接就可以了。
<code class="language-scala hljs has-numbering"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UserApplication</span> @<span class="hljs-title">Inject</span><span class="hljs-params">()</span><span class="hljs-params">(dbConfigProvider: DatabaseConfigProvider)</span></span> <span class="hljs-keyword">extends</span> Controller <span class="hljs-keyword">with</span> UserSchema <span class="hljs-keyword">with</span> HasDatabaseConfig[JdbcProfile] { <span class="hljs-keyword">val</span> dbConfig = dbConfigProvider.get[JdbcProfile] <span class="hljs-keyword">import</span> driver.api._ <span class="hljs-keyword">val</span> Users = TableQuery[UserTable] }</code>