ScalikeJDBC是一個Scala的JDBC框架,官網說easy-to-use and very flexible,易用又靈活~java
一、添加依賴mysql
<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>
二、上代碼sql
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) } }
三、排錯
1)版本session
java.lang.NoSuchMethodError: scala.Product.$init$
這種方法找不到的問題一般都是jar包沒引對或版本不一致,pom.xml
中配置的scalikejdbc_2.11
必須和系統sdk的scala版本一致(也是2.11.x)
2)MySQL時區app
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錯誤)flex