scalikejdbc的簡介:
ScalikeJDBC是一款給Scala開發者使用的簡介訪問類庫,它是基於SQL的,使用者只需要關注SQL邏輯的編寫,所有的數據庫操作都交給ScalikeJDBC。這個類庫內置包含了JDBCAPI,並且給用戶提供了簡單易用並且非常靈活的API。並且,QueryDSl(通用查詢查詢框架)使你的代碼類型安全,半年過去可重復使用。我們可以在生產環境大膽地使用這款DB訪問類庫。
筆者用的是MySQL數據庫,所以此處以MySQL為例,以下是通過ScalikeJDBC來操作數據庫的具體過程
【操作大綱】
1.拿到connection
2.拿到statement
3.拿到rs
4.結果獲取出來
5.關掉rs,stmt和connection釋放資源
【pom文件的配置】
<!--Scala相關依賴--> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-library</artifactId> <version>${scala.version}</version> </dependency> <!--scalikejdbc相關依賴--> <dependency> <groupId>org.scalikejdbc</groupId> <artifactId>scalikejdbc_2.11</artifactId> <version>${scalikejdbc.version}</version> </dependency> <dependency> <groupId>org.scalikejdbc</groupId> <artifactId>scalikejdbc-config_2.11</artifactId> <version>${scalikejdbc.version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.jdbc.version}</version> </dependency>
<properties>
<scala.version>2.11.8</scala.version> <scalikejdbc.version>3.3.2</scalikejdbc.version> <mysql.jdbc.version>5.1.38</mysql.jdbc.version>
</properties>
【配置連接數據庫的信息文件】
1.在src的main目錄下配置一個resource文件夾,文件夾下再創建一個application.conf

【配置使文件能夠被讀取】
File=》Project Structure ####若想使配置文件能夠被加載,需將config設置為resource

【application.conf文件配置信息如下】
db.default.driver="com.mysql.jdbc.Driver" db.default.url="jdbc:mysql://hadoop001/ruoze_d6?characterEncoding=utf-8" db.default.user="root" db.default.password="123456" # Connection Pool settings db.default.poolInitialSize=10 db.default.poolMaxSize=20 db.default.connectionTimeoutMillis=1000
【在ruoze_d6創建Employer表格】
create table Employer( name varchar(10), age varchar(4), salary varchar(10) );
【scala編程實現增刪改查操作】
package com.wsk.bigdata.scala.scalikejdbc import scalikejdbc._ import scalikejdbc.config._ case class Employer(name: String, age: Int, salary: Long) object JdbcTest { def main(args: Array[String]): Unit = { DBs.setupAll() val config = DBs.config val employers = List(Employer("zhangsan", 20, 18000), Employer("zhangliu", 50, 300000), Employer("lisi", 22, 22000)) //批量插入 insert(employers) println("----------------insert執行完畢---------------") //查詢出結果 val results = select() for (employer <- results) { println(employer.name, employer.age, employer.salary) } println("----------------select執行完畢---------------") //修改 update(1000, "zhangsan") println("----------------update執行完畢---------------") //根據姓名刪除 deleteByname("zhangliu") println("----------------deleteByname執行完畢---------------") //刪除所有記錄 deleteAll() println("----------------deleteAll執行完畢---------------") DBs.closeAll() } def insert(employers: List[Employer]): Unit = { DB.localTx { implicit session => for (employer <- employers) { SQL("insert into wsktest(name,age,salary) values(?,?,?)") .bind(employer.name, employer.age, employer.salary) .update().apply() } } } def select(): List[Employer] = { DB.readOnly { implicit session => SQL("select * from wsktest") .map(rs => Employer(rs.string("name"), rs.int("age"), rs.long("salary"))) .list().apply() } } def update(age: Int, name: String) { DB.autoCommit { implicit session => SQL("update wsktest set age = ? where name = ?").bind(age, name).update().apply() } } def deleteByname(name: String): Unit = { DB.autoCommit { implicit session => SQL("delete from wsktest where name = ?").bind(name).update().apply() } } def deleteAll(): Unit ={ DB.autoCommit { implicit session => SQL("delete from wsktest ").update().apply() } } }
