您好,登錄后才能下訂單哦!
本篇內容介紹了“怎么使用ScalikeJDBC操作MySQL數據庫”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
環境:
IDEA+Maven+ScalikeJDBC+MySQL5.6
1 新建Maven的Scala工程
2 pom.xml文件添加依賴:
<properties>
<scala.version>2.11.8</scala.version>
<!-- 添加scalikejdbc 和 mysql 的version 定義 -->
<scalikejdbc.version>2.5.2</scalikejdbc.version>
<mysql.version>5.6.23</mysql.version>
</properties>
<dependencies>
<!-- 添加scalikejdbc的dependency -->
<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>
<!-- 添加mysql的dependency -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
3 新增Scala對象
package com.ruozedata
import scalikejdbc._
object ScalaLikeJdbc {
def main(args: Array[String]): Unit = {
// 加載驅動
Class.forName("com.mysql.jdbc.Driver")
// 指定數據庫連接url,userName,password
val url = "jdbc:mysql://192.168.1.108:3306/ruozedata?useUnicode=true&characterEncoding=UTF8"
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, settings)
// 新增數據
val insertResult: Int = DB autoCommit { implicit session =>
SQL("insert into user(id, name,age) values(?,?,?)").bind(1, "Zhangsan",15).update().apply()
SQL("insert into user(id, name,age) values(?,?,?)").bind(2, "Ruoze",16).update().apply()
SQL("insert into user(id, name,age) values(?,?,?)").bind(3, "Jepson",17).update().apply()
}
println(insertResult) // 打印 insertResult 變量的值,如果新增成功,返回1,否則返回0
// 定義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)
}
}
}
4 添加jdbc的MySQL驅動包
mysql-connector-java-commercial-5.1.25-bin.jar
5 運行結果如下:
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/software/apache-maven-3.3.9/repository/org/slf4j/slf4j-log4j12/1.7.16/slf4j-log4j12-1.7.16.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/software/apache-maven-3.3.9/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
log4j:WARN No appenders could be found for logger (scalikejdbc.ConnectionPool$).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
1
1,Zhangsan,15
2,Ruoze,16
3,Jepson,17
Process finished with exit code 0
“怎么使用ScalikeJDBC操作MySQL數據庫”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。