zoukankan      html  css  js  c++  java
  • 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)
  • 相关阅读:
    C#中判断为空
    ArcGIS中的AddIn开发示例
    当前不会命中断点,还没有加载该文档加载任何符号
    设置ArcGIS的外观改回到出厂
    读取Style符号库样式的方法
    ArcEngine中的缩放地图
    修改字段结构之GP工具
    修改字段结构之ArcGIS Diagrammer
    merage语句
    Windows下Redis的安装使用
  • 原文地址:https://www.cnblogs.com/wenbronk/p/9717286.html
Copyright © 2011-2022 走看看