1, 引入maven依赖
<!-- 使用 sclaikeJDBC --><dependency><groupId>org.scalikejdbc</groupId><artifactId>scalikejdbc_2.11</artifactId><version>3.3.1</version></dependency><dependency><groupId>org.scalikejdbc</groupId><artifactId>scalikejdbc-core_2.11</artifactId><version>3.3.1</version></dependency><dependency><groupId>org.scalikejdbc</groupId><artifactId>scalikejdbc-config_2.11</artifactId><version>3.3.1</version></dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency>
2, 在resource下使用配置文件
application.conf
// 使用 scalike db.pvuv.driver="com.mysql.jdbc.Driver" db.pvuv.url="jdbc:mysql://10.110.123.84:3306/spark?characterEncoding=utf-8" db.pvuv.user="wenbronk" db.pvuv.password="a75767626"
3, 使用scalike进行读取或者插入操作
import scalikejdbc._ import scalikejdbc.config.{DBs, DBsWithEnv}object ScalikeJDBCTests {def main(args: Array[String]): Unit = {DBs.setup('pvuv) // DBs.setupAll() // DBsWithEnv("prod").setup('pvuv)NamedDB('pvuv) readOnly(implicit session => {sql"select * from host_dict".map(_.long(1)).list().apply()})}}
也可以对环境进行区分
development.db.default.driver="org.h2.Driver" development.db.default.url="jdbc:h2:file:./db/default" development.db.default.user="sa" development.db.default.password=""prod {db {sandbox {driver="org.h2.Driver"url="jdbc:h2:file:./are-you-sure-in-production"user="user"password="pass"}} }
配置文件的加载方式为:
DBsWithEnv("development").setupAll() DBsWithEnv("prod").setup('sandbox)