ITPub博客

首页 > 大数据 > Hadoop > 使用ScalikeJDBC操作MySQL数据库

使用ScalikeJDBC操作MySQL数据库

原创 Hadoop 作者:hz_ganwei 时间:2018-07-31 17:59:19 0 删除 编辑

环境:

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


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29609890/viewspace-2168798/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2014-04-15

  • 博文量
    44
  • 访问量
    36478