play-slick基础:CRUD操作

学习URL

  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>


Vue-Slick是一个基于Vue.js的响应式轮播组件,它基于Slick库,提供了许多强大的轮播功能和选项。以下是Vue-Slick的中文文档: 1. 安装 你可以通过npm安装Vue-Slick: ``` npm install vue-slick --save ``` 2. 引入 在你的Vue组件中,引入Vue-Slick: ``` import VueSlick from 'vue-slick'; ``` 3. 使用 在你的Vue组件中,使用Vue-Slick: ``` <vue-slick :settings="slickOptions"> <div><img src="slide1.jpg"></div> <div><img src="slide2.jpg"></div> <div><img src="slide3.jpg"></div> </vue-slick> ``` 4. 配置选项 你可以通过给Vue-Slick传递一个settings选项来配置它的选项。以下是一些常用的配置选项: ``` slickOptions: { arrows: true, // 显示箭头 dots: true, // 显示小圆点 autoplay: true, // 自动播放 autoplaySpeed: 3000, // 自动播放速度 infinite: true, // 无限循环 speed: 500, // 切换速度 slidesToShow: 3, // 每次显示的幻灯片数量 slidesToScroll: 1, // 滚动数量 responsive: [ { breakpoint: 1024, settings: { slidesToShow: 2, slidesToScroll: 1, infinite: true, dots: true } }, { breakpoint: 600, settings: { slidesToShow: 1, slidesToScroll: 1 } } ] } ``` 5. 事件 Vue-Slick提供了一些事件来处理轮播的交互,以下是一些常用的事件: ``` <vue-slick @beforeChange="beforeChange" @afterChange="afterChange"> ... </vue-slick> methods: { beforeChange: function (oldIndex, newIndex) { console.log('Before change', oldIndex, newIndex); }, afterChange: function (index) { console.log('After change', index); } } ``` 以上就是Vue-Slick的中文文档,希望能对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值