scalikeJDBC的使用

# MySQL example
db.default.driver="com.mysql.jdbc.Driver"
db.default.url="jdbc:mysql://hadoop01:3306/test?characterEncoding=UTF-8"
db.default.user="root"
db.default.password="123456"
package scalikejdbc

import scalikejdbc.config.DBs

object Demo {
  def main(args: Array[String]): Unit = {
    DBs.setup()
   // val sel=select()
   // println(sel)
   // update
   // delete
    insertBacth
  }

  /**
    * 查詢
    * @return
    */
  def select()={
    DB readOnly{ implicit session=>
      SQL("select * from emp").map(rs=> rs.string("name")).list().apply()
    }
  }

  /**
    * 更新
    * @return
    */
  def update()={
    DB.autoCommit{ implicit session=>
      SQL("update emp set salary=? where id=?").bind(1000,1201).update().apply()
    }
  }

  def delete()={
    DB.autoCommit{ implicit session=>
      SQL("delete from emp where id=? ").bind(1201).update().apply()

    }
  }

  /**
    * 插入的時候建立一個事務
    */
  def insertBacth()={
    DB.localTx{ implicit session=>
      SQL("insert into emp (id,name,deg,salary,dept) values(?,?,?,?,?)").bind(1001,"xiaoming","php dey",30000,"TP").update().apply()
      //val r=2/0
      SQL("insert into emp (id,name,deg,salary,dept) values(?,?,?,?,?)").bind(1002,"xiaohong","php dey",50000,"TP").update().apply()
    }
  }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章