溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么使用ScalikeJDBC操作MySQL數(shù)據(jù)庫

發(fā)布時間:2021-12-22 14:09:45 來源:億速云 閱讀:301 作者:iii 欄目:數(shù)據(jù)庫

本篇內(nèi)容介紹了“怎么使用ScalikeJDBC操作MySQL數(shù)據(jù)庫”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

環(huán)境:

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 = {

    // 加載驅(qū)動

    Class.forName("com.mysql.jdbc.Driver")

    // 指定數(shù)據(jù)庫連接url,userName,password

    val url = "jdbc:mysql://192.168.1.108:3306/ruozedata?useUnicode=true&characterEncoding=UTF8"

    val userName = "root"

    val password = "root"

    // 設(shè)置數(shù)據(jù)庫連接參數(shù)

    val settings = ConnectionPoolSettings(

      initialSize = 5,

      maxSize = 20,

      connectionTimeoutMillis = 3000L,

      validationQuery = " select 1 from test ")  // test表是專門創(chuàng)建的一個測試表

    // 構(gòu)建數(shù)據(jù)庫連接池

    ConnectionPool.singleton(url, userName, password, settings)

    //  新增數(shù)據(jù)

    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)

    // 查詢數(shù)據(jù)操作

    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()

    }

    // 循環(huán)遍歷 users對象 并輸出

    for(user <- users) {

      println(user.id + "," + user.name + "," + user.age)

    }

  }

}

4 添加jdbc的MySQL驅(qū)動包

 mysql-connector-java-commercial-5.1.25-bin.jar

5 運(yùn)行結(jié)果如下:

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數(shù)據(jù)庫”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI