ScalikeJDBC连接MySQL

ScalikeJDBC是一个Scala的JDBC框架,官网说easy-to-use and very flexible,易用又灵活~

1、添加依赖

        
            org.scalikejdbc
            scalikejdbc_2.11
            3.3.2
        

        
            com.h2database
            h2
            1.4.197
        
        
            ch.qos.logback
            logback-classic
            1.2.3
        

        
            mysql
            mysql-connector-java
            8.0.15
        

2、上代码

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 = "复联%"
    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、排错
1)版本

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

这种方法找不到的问题通常都是jar包没引对或版本不一致,pom.xml中配置的scalikejdbc_2.11必须和系统sdk的scala版本一致(也是2.11.x)
2)MySQL时区

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.

这是因为mysql-connector-java的版本是8.0.15

解决方法(以下几种皆可):
1.要么换成5.1.x的版本
2.在cmd mysql客户端输入set global time_zone = '+8:00';
3.在jdbc的url中加上serverTimezone=Hongkong请求参数
这些同样可以解决在IDEA的DataSource配置中连不上MySQL的时候。(08001错误)

你可能感兴趣的:(scala,mysql,maven)