scala写jdbc连接数据库 实现数据库的批量操作

结构

在这里插入图片描述

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>cn.kgc</groupId>
  <artifactId>scalajdbc</artifactId>
  <version>1.0</version>

  <name>scalajdbc</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.47</version>
    </dependency>
    <!--scala-jdbc-->
    <dependency>
      <groupId>org.scalikejdbc</groupId>
      <artifactId>scalikejdbc_2.12</artifactId>
      <version>3.3.2</version>
    </dependency>
    <!--自动解析.*conf 配置信息-->
    <dependency>
      <groupId>org.scalikejdbc</groupId>
      <artifactId>scalikejdbc-config_2.12</artifactId>
      <version>3.3.2</version>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

datasource.properties

db.test.driver=com.mysql.jdbc.Driver
db.test.url=jdbc:mysql://192.168.75.245:3306/test?useSSL=false&characterEncoding=utf-8
db.test.username=root
db.test.password=Fang@123

Base

package cn.kgc.scalajdbc.core {

  import scala.collection.mutable.ArrayBuffer
  trait Dao[T]{
    def exeUpdate(sql:String,params:Any*):Int
    def exeQuery(sql:String,params:Any*):ArrayBuffer[T]
  }
  trait Batch{
    def exeBatch(sql:String,colSize:Int,params:Any*):Int
  }
}

package cn.kgc.scalajdbc.conf {

  import java.io.{FileReader, FilenameFilter}
  import java.sql.{Connection, DriverManager, PreparedStatement, ResultSet}
  import scala.collection.mutable.ArrayBuffer
  import scala.util.control.Breaks

  case class Configuration(driver:String="com.mysql.jdbc.Driver",
                           url:String="jdbc:mysql://192.168.75.245:3306/test?useSSL=false&characterEncoding=utf-8",
                           username:String="root",
                           password:String="Fang@123")
  class DBs{
    var config:Configuration = Configuration()
    val items: Array[String] = Array("driver", "url", "username", "password")

    def setup(dbName:String="default")={
      import java.io.File
      import java.util.Properties

      //定位配置文件
      val url = Thread.currentThread().getContextClassLoader.getResource("").getFile
      val dir = new File(url)
      val files: Array[File] = dir.listFiles(new FilenameFilter {
        override def accept(dir: File, name: String): Boolean = name.endsWith(".properties")
      })

      //解析配置信息
      if(files.size>0){
        val file = files.apply(0)
        val pro = new Properties()
        pro.load(new FileReader(file))
        val builder:StringBuilder = new StringBuilder("db.")
        builder.append(dbName)
        builder.append(".")
        val initLen = builder.size
        import scala.util.control.Breaks._
        val loop = Breaks
        val buf = new ArrayBuffer[String]()
        loop.breakable(
          for (elem <- items) {
            builder.append(elem)
            if (!pro.containsKey(builder.toString())){
              println("缺少配置项:"+builder.toString())
              loop.break()
            }
            buf.append(pro.getProperty(builder.toString()))
            builder.delete(initLen,builder.size)
          }
        )
        config = Configuration(buf.apply(0),buf.apply(1),buf.apply(2),buf.apply(3))

      }
      //装载驱动
      Class.forName(config.driver)
    }
  }

  object DBs{
    val dbs = new DBs()
    dbs.setup("test")
    def con():Connection = DriverManager.getConnection(dbs.config.url,dbs.config.username,dbs.config.password)

    def pst(con:Connection,sql:String,params:Seq[Any]):PreparedStatement={
      val pst = con.prepareStatement(sql)
      if(params.size>0){
        params.zipWithIndex.foreach(e=>{
          pst.setObject(e._2+1,e._1)
        })
      }
      pst
    }

    def update(pst:PreparedStatement):Int=pst.executeUpdate()

    def query(pst:PreparedStatement):ResultSet=pst.executeQuery()

    def close(closes:AutoCloseable*):Unit={
      for (elem <- closes) {
        if (null != elem) {
          elem.close()
        }
      }
    }
  }

}

package cn.kgc.scalajdbc.studentinfo{
  import cn.kgc.scalajdbc.core._
  import cn.kgc.scalajdbc.conf.DBs
  import scala.collection.mutable.ArrayBuffer

  case class StudentInfo(stuId:Int,stuName:String,stuAge:Int,stuGender:String,mobile:String,tuition:String,fkClassId:Int)
  class StudentInfoDao extends Dao[StudentInfo] with Batch {
    override def exeUpdate(sql: String, params: Any*): Int = {
      val con = DBs.con()
      val pst = DBs.pst(con, sql, params)
      val rst = DBs.update(pst)
      DBs.close(pst,con)
      rst
    }
    override def exeQuery(sql: String, params: Any*): ArrayBuffer[StudentInfo] = {
      val con = DBs.con()
      val pst = DBs.pst(con, sql, params)
      var rst = DBs.query(pst)
      val buf = new ArrayBuffer[StudentInfo]()
      while (rst.next()){
        buf.append(StudentInfo(rst.getInt(1),
          rst.getString(2),
          rst.getInt(3),
          rst.getString(4),
          rst.getString(5),
          rst.getString(6),
          rst.getInt(7)
        ))
      }
      DBs.close(rst,pst,con)
      buf
    }
    override def exeBatch(sql: String, colSize:Int, params: Any*): Int = {
      val con = DBs.con()
      val pst = DBs.pst(con,sql,Seq())
      var rst,i = 0
      val commitBatch = () =>{
        rst += pst.executeBatch().sum
        pst.clearBatch()
      }
      params.grouped(colSize).foreach(seq=>{
        seq.zipWithIndex.foreach(e=>pst.setObject(e._2+1,e._1))
        pst.addBatch()
        i+=1
        if(i % 1000 == 0){
          commitBatch()
        }
      })
      commitBatch()
      DBs.close(pst,con)
      rst
    }
  }
}

Test

package cn.kgc.scalajdbc.oop03
import cn.kgc.scalajdbc.core._
import cn.kgc.scalajdbc.studentinfo._

object Test {
  implicit class ExtStudentInfo(v:StudentInfo){
    def view()=println(s"${v.stuId}\t${v.stuName}\t${v.stuAge}\t${v.stuGender}\t${v.mobile}\t${v.tuition}\t${v.fkClassId}")
  }

  def main(args: Array[String]): Unit = {
/* val stuDao:Dao[StudentInfo] = new StudentInfoDao()

 stuDao.exeQuery("select stuId,stuName,stuAge,stuGender,mobile,tuition,fkClassId from studentinfo")
   .foreach(_.view)*/


   /* val sql = "insert into studentinfo(stuId,stuName,stuAge,mobile,tuition,fkClassId)values(81,'xxx',23,)"
 val ctDao:StudentInfoDao = new StudentInfoDao()
    ctDao.exeBatch(sql,1,80,81,82)

*/


    val sql = "insert into studentinfo(stuId,stuName,stuAge,stuGender,mobile,tuition,fkClassId) values(?,?,?,?,?,?,?)"
    val studentInfoDao:Batch = new StudentInfoDao()
    println(studentInfoDao.exeBatch(sql,7,55,"任志杰2",18,"男","13327791036",23345,1,54,"任志杰3",18,"男","13327791134",
      23325,2))
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值