Scalikejdbc connecting to MySQL

Time:2019-12-10

Scalakejdbc is a Scala JDBC framework. The official website says easy-to-use and very flexible, easy to use and flexible~

1. Add dependency

        <dependency>
            <groupId>org.scalikejdbc</groupId>
            <artifactId>scalikejdbc_2.11</artifactId>
            <version>3.3.2</version>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.197</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.15</version>
        </dependency>

2. Upper code

import scalikejdbc._

object MySQLDAO {

  def main(args: Array[String]): Unit = {
    // initialize JDBC driver & connection pool
    Class.forName("org.h2.Driver")
    ConnectionPool.singleton("jdbc:mysql:///film?characterEncoding=utf8", "root", "root")

    val id = 1
    Val name = "duplicate%"
    val value = sql"""select * from movie where name LIKE $name ORDER BY id DESC """
    println(value)

    // simple example
    val lasts: List[Map[String, Any]] = DB.readOnly { implicit session =>
      sql"""select * from movie where name LIKE $name ORDER BY id DESC """.map(_.toMap()).list.apply()
    }
    println(lasts)
  }
}

3. Troubleshooting
1) version

java.lang.NoSuchMethodError: scala.Product.$init$

The problem that this method can’t find is usually that the jar package is not correctly referenced or the version is inconsistent,pom.xmlConfigured inscalikejdbc_2.11Must be consistent with the scala version of the system SDK (also 2.11. X)
2) MySQL time zone

java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time
zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a
more specifc time zone value if you want to utilize time zone support.

that is becausemysql-connector-javaThe version is8.0.15

Solution (the following are all available):
1. Replace with5.1.xVersion
2. Input in CMD MySQL clientset global time_zone = '+8:00';
3. Add theserverTimezone=HongkongRequest parameters
These can also solve the problem when MySQL cannot be connected in the data source configuration of idea. (08001 error)

Scalikejdbc connecting to MySQL