Scala操作MySQL插件ScalikeJDBC的使用
1.什么是ScalikeJDBC
ScalikeJDBC是一款给Scala开发者使用的简洁DB访问类库,它是基于SQL的,使用者只需要关注SQL逻辑的编写,所有的数据库操作都交给ScalikeJDBC。这个类库内置包含了JDBC API,并且给用户提供了简单易用并且非常灵活的API。并且,QueryDSL(通用查询查询框架)使你的代码类型安全的并且可重复使用。我们可以在生产环境大胆地使用这款DB访问类库。
2.项目构建
项目是scala语言开发,使用maven做项目管理
2.1添加依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ruozedata.scalikejdbc</groupId>
<artifactId>g6-train-scalikejdbc</artifactId>
<version>1.0</version>
<inceptionYear>2008</inceptionYear>
<properties>
<scala.version>2.11.8</scala.version>
<hadoop.version>2.6.0-cdh5.7.0</hadoop.version>
<hive.version>1.1.0-cdh5.7.0</hive.version>
<scalikejdbc.version>3.3.2</scalikejdbc.version>
</properties>
<repositories>
<repository>
<id>cloudera</id>
<url>https://repository.cloudera.com/artifactory/cloudera-repos</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>
<!--添加Hadoop的依赖-->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>${hadoop.version}</version>
</dependency>
<!--hive UDF函数依赖包-->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>${hive.version}</version>
</dependency>
<!-- scalikejdbc_2.11_版本3.3.2 -->
<dependency>
<groupId>org.scalikejdbc</groupId>
<artifactId>scalikejdbc_2.11</artifactId>
<version>${scalikejdbc.version}</version>
</dependency>
<!-- scalikejdbc-config_2.11_版本3.3.2 -->
<dependency>
<groupId>org.scalikejdbc</groupId>
<artifactId>scalikejdbc-config_2.11</artifactId>
<version>${scalikejdbc.version}</version>
</dependency>
<!--MySQL的JDBC驱动包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.42</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.4</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
2.2配置数据库连接信息
在resource配置下添加配置文件application.conf,然后将一下内容添加进去
#默认的连接配置是db.default作为前缀,但是正常生产来说不会只配置一个数据库,所以要自定义连接的数据库前缀,这边我自定义连接的是mysql中的test数据库
db.test.driver="com.mysql.jdbc.Driver"
db.test.url="jdbc:mysql://192.168.9.128:3306/test?characterEncoding=utf-8&useSSL=true"
db.test.user="root"
db.test.password="123456"
2.3编写数据库操作代码
package com.ruozedata.scala.scalikejdbc
import scalikejdbc.config.DBs
import scalikejdbc.{NamedDB, SQL}
case class user(id:Int,name:String,age:Int)
object ScalikeJDBCTests {
def main (args: Array[String]): Unit = {
//加载数据库配置信息,默认加载db.default.*,不指定数据库默认是conf文件的default数据库
//如果指定了连接的数据库同时下面CURD操作中NameDB也要指定出所使用的数据库
DBs.setup('test)
println(select)
insert(9,"Jelly",20)
println(select)
update(9,50)
println(select)
}
//查询数据并将返回的结果放到一个user中,并将user对象放入List结合中返回
def select() = {
val result:List[user] = NamedDB('test) readOnly{ implicit session=>
SQL("select * from user").map(
i => user(i.int("id"),
i.string("name"),
i.int("age"))
).list().apply()
}
result
}
//插入数据,这边使用了事务插入localTx
def insert(id:Int,name:String,age:Int) = {
NamedDB('test).localTx{
implicit session =>
SQL("insert into user(id,name,age) values(?,?,?)").bind(id,name,age).update().apply()
}
}
//修改,这边没有使用事务,使用的是autoCommit
def update(id:Int,age:Int) = {
NamedDB('test).autoCommit{
implicit session =>
SQL("update user set age=? where id=?").bind(age,id).update().apply()
}
}
}