测试scalikejdbc的使用(crud)

package scalikejdbc

import scalikejdbc.config.DBs

//测试scalikejdbc的使用

object scalaJdbcDemo {
  def main(args: Array[String]): Unit = {
    //插入数据
    //想要加载配置文件中的jdbc,就要求表是必须存在的
    DBs.setup()

    //写sql语句(crud)
    
    //增加insert练习

      //导入隐式参数
//    DB.autoCommit{implicit session=>
        //写sql语句,“?”是占位符
        //往表中加入数据
//      SQL("insert into attachrwbureauanaylysis values (?,?,?,?,?,?,?,?,?,?,?)")
          //写入数据
          //一个数据对应一个占位符,不能多不能少,因为会把数据填入相对应的位置中
//        .bind("天津局",1,1,1,1,1,1,1,1,1,1)
//        .update()
//        .apply()
//    }

    //删除delete练习

      //传入隐式参数
//    DB.autoCommit {implicit session =>
//      SQL("delete from attachrwbureauanaylysis where AttachRWBureau=?")
//      //绑定参数
//        .bind("京")
//        .update()
//        .apply()
//    }

    //更新update练习

//    DB.autoCommit {implicit session =>
//      SQL("update attachrwbureauanaylysis set allData=? where AttachRWBureau=?")
//        //绑定参数
//        .bind(100000,"沈")
//        .update()
//        .apply()
//    }

    //查询select练习

//    val tuples: List[(String, Int)] = DB.autoCommit { implicit session =>
//      SQL("select * from attachrwbureauanaylysis where allData>?")
//        //绑定参数
//        .bind(150)
//        .map(line => (
//          //想查询几个值就写几个值
//          //查询AttachRWBureau属性的值
//          line.string("AttachRWBureau"),
//          //查询allData属性的值
//          line.int("allData")
//        )).list().apply()
//    }
      //打印出来
//    tuples.foreach(println(_))

    //事务控制

    DB.localTx{ implicit session =>
      //会循环十一次 ,也就是说表里会多十一条数据,具体的次数可以根据情况进行更改
      for(i <- 0 to 10){
        SQL("insert into attachrwbureauanaylysis values (?,?,?,?,?,?,?,?,?,?,?)")
          .bind("天津",1,1,1,1,1,1,1,1,1,1)
          .update()
          .apply()
      }
    }

  }

}

appliaction.conf

#配置文件
#配置压缩格式
parquet.code="snappy"
#配置序列化方式
spark.serializer="org.apache.spark.serializer.KryoSerializer"
#scalikejdbc链接
db.default.url="jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8"
db.default.driver="com.mysql.jdbc.Driver"
db.default.user="root"
db.default.password="000000"
发布了189 篇原创文章 · 获赞 13 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/NewBeeMu/article/details/103096448