scala-jdbc-scalike操作jdbc数据库

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)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值