在scala中有一个基于SQL的数据库访问库就是scalikeJDBC,官网地址如下:
http://scalikejdbc.org/
1.将pom依赖加入pom.xml文件
<!-- mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
<!-- scalikejdbc -->
<dependency>
<groupId>org.scalikejdbc</groupId>
<artifactId>scalikejdbc_2.11</artifactId>
<version>2.5.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.scalikejdbc/scalikejdbc-config -->
<dependency>
<groupId>org.scalikejdbc</groupId>
<artifactId>scalikejdbc-config_2.11</artifactId>
<version>2.5.0</version>
</dependency>
2. 在 src/main/resources/目录下创建application.conf,官网配置介绍:http://scalikejdbc.org/documentation/configuration.html。加入相关配置
# JDBC settings
db.default.driver="com.mysql.jdbc.Driver"
db.default.url="jdbc:mysql://hadoop000:3306/scalikejdbc"
db.default.user="root"
db.default.password="root"
# Connection Pool settings
db.default.poolInitialSize=10
db.default.poolMaxSize=20
db.default.connectionTimeoutMillis=1000
3.编写增删改查代码
package com.wxx.spark.scala.utils
import scalikejdbc.{DB, SQL}
import scalikejdbc.config.DBs
object ScalaLikeJdbc {
def main(args: Array[String]): Unit = {
// 加载配置
DBs.setupAll()
// insert()
// update()
// delete()
query()
}
def insert()={
// 插入使用的是localTx
DB.localTx(implicit session=>{
SQL("insert into user(id,name,age) values(?,?,?)").bind(1,"AAA",18).update.apply()
})
}
def update() ={
// 更新使用的是autoCommit
DB.autoCommit(implicit session=>{
// SQL里面是普通的sql语句,后面bind里面是语句中"?"的值,update().apply()是执行语句
SQL("update user set name = ? where id=?").bind("DDD",1).update().apply()
})
}
def delete()={
// 删除使用的也是autoCommit
DB.autoCommit(implicit session=>{
SQL("delete from user where id =?").bind(1).update().apply()
})
}
def query() ={
// 读取使用的是readOnly
val users = DB.readOnly(implicit session => {
SQL("select * from user").map(rs => {
User(
rs.int("id"),
rs.string("name"),
rs.int("age")
)
}).list().apply()
})
users.foreach(println)
}
}
case class User(id:Int, name :String, age : Int)
在代码中直接配置数据库参数
package com.wxx.spark.scala.utils
import scalikejdbc.{ConnectionPool, ConnectionPoolSettings, DB, SQL, WrappedResultSet}
object ScalaLikeJdbc2 {
def main(args: Array[String]): Unit = {
// 加载驱动
Class.forName("com.mysql.jdbc.Driver")
// 指定数据库连接url,userName,password
//jdbc:mysql://localhost:3306/testdb
val url = "jdbc:mysql://192.168.31.100:3306/scalike"
val userName = "root"
val password = "root"
// 设置数据库连接参数
val settings = ConnectionPoolSettings(
initialSize = 5,
maxSize = 20,
connectionTimeoutMillis = 3000L,
validationQuery = " select 1 from test ") // test表是专门创建的一个测试表
// 构建数据库连接池
ConnectionPool.singleton(url, userName, password)
// insert()
query()
}
def insert() ={
// 新增数据
val insertResult: Int = DB autoCommit { implicit session =>
SQL("insert into user(id, name,age) values(?,?,?)").bind(1, "AAA",15).update().apply()
SQL("insert into user(id, name,age) values(?,?,?)").bind(2, "BBB",16).update().apply()
SQL("insert into user(id, name,age) values(?,?,?)").bind(3, "CCC",17).update().apply()
}
println(insertResult) // 打印 insertResult 变量的值,如果新增成功,返回1,否则返回0
}
def query()={
// 定义User类
case class User(id: Int, name: String, age: Int)
// 查询数据操作
val allColumns = (rs: WrappedResultSet) => User(
id = rs.int("id"),
name = rs.string("name"),
age = rs.int("age"))
val users: List[User] = DB readOnly { implicit session =>
SQL("select * from user ").map(allColumns).list.apply()
}
// 循环遍历 users对象 并输出
for(user <- users) {
println(user.id + "," + user.name + "," + user.age)
}
}
}