Scalikejdbc connecting to MySQL


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

1. Add dependency




2. Upper code

import scalikejdbc._

object MySQLDAO {

  def main(args: Array[String]): Unit = {
    // initialize JDBC driver & connection pool
    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 """

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

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