设为首页 加入收藏

TOP

scala数据库工具类(一)
2017-10-10 12:11:56 】 浏览:8139
Tags:scala 数据库 工具

scala的数据库连接池,基于mysql

import java.util.concurrent.ConcurrentHashMap
import com.jolbox.bonecp.{ BoneCPConfig, BoneCP }
import java.util.ResourceBundle
import java.util.LinkedList
import java.sql.DriverManager
import java.sql.Connection

/**
 * 数据库连接池工具类
 * 语言:scala
 * 时间:2016-07-09
 */
object DBConnectionPool {
  private val reader = ResourceBundle.getBundle("connection")
  private val max_connection = reader.getString("jeecg.max_connection") //连接池总数
  private val connection_num = reader.getString("jeecg.connection_num") //产生连接数
  private var current_num = 0 //当前连接池已产生的连接数
  private val pools = new LinkedList[Connection]() //连接池
  private val driver = reader.getString("jeecg.driver")
  private val url = reader.getString("jeecg.url")
  private val username = reader.getString("jeecg.username")
  private val password = reader.getString("jeecg.password")
 /**
  * 加载驱动
  */
  private def before() {
    if (current_num > max_connection.toInt && pools.isEmpty()) {
      print("busyness")
      Thread.sleep(2000)
      before()
    } else {
      Class.forName(driver)
    }
  }
  /**
   * 获得连接
   */
  private def initConn(): Connection = {
    val conn = DriverManager.getConnection(url, username, password)
    conn
  }
  /**
   * 初始化连接池
   */
  private def initConnectionPool(): LinkedList[Connection] = {
    AnyRef.synchronized({
      if (pools.isEmpty()) {
        before()
        for (i <- 1 to connection_num.toInt) {
          pools.push(initConn())
          current_num += 1
        }
      }
      pools
    })
  }
  /**
   * 获得连接
   */
  def getConn():Connection={
     initConnectionPool()
     pools.poll()
  }
  /**
   * 释放连接
   */
  def releaseCon(con:Connection){
    pools.push(con)
  }
  
}

配置文件

#数据库连接池配置文件
jeecg.driver=org.gjt.mm.mysql.Driver
jeecg.url=jdbc\:mysql\://0.0.0.0\:3306/jeecg?useUnicode=true&characterEncoding=utf-8
jeecg.username=root
jeecg.password=****
jeecg.max_connection=8
jeecg.connection_num=10

dao类

import java.sql.ResultSet
import java.sql.PreparedStatement
import java.sql.Connection
import java.sql.Statement
/**
 * 数据操作工具类
 * 语言:scala
 * 时间:2016-07-09
 */
private[org] abstract class BaseDao[T](conn: Connection) {
  /**
   * 插入数据
   * @param sql SQL语句
   * @param params 参数列表
   * @param convert 主键转换方法
   * @return 转换结果
   */
  protected def insert[T](sql: String, params: Array[Any])(convert: ResultSet => T) = {
    val pstmt = conn prepareStatement (sql, Statement.RETURN_GENERATED_KEYS)
    setParameters(pstmt, params)
    pstmt.executeUpdate
    val rs = pstmt.getGeneratedKeys
    rs.next
    convert(rs)
  }

  /**
   * 更新数据
   * @param sql SQL语句
   * @param params 参数列表
   * @return 影响行数
   */
  protected def update(sql: String, params: Array[Any]) = createStatement(sql, params).executeUpdate

  /**
   * 查询对象
   * @param sql SQL语句
   * @param params 参数列表
   * @param convert 结果集转换方法
   * @return 泛型对象
   */
  protected def queryForObject[T](sql: String, params: Array[Any])(convert: ResultSet => T) = {
    val rs = query(sql, params)
    if (rs.next) {
      val result = convert(rs)
      if (rs.next) {
        val ex = new ResultsTooManyException
        throw ex
      } else Some(result)
    } else None
  }

  /**
   * 查询对象列表
   * @param sql SQL语句
   * @param params 参数列表
   * @param convert 结果集转换方法
   * @return 泛型对象列表
   */
  protected def queryForList[T](sql: Str
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Scalaz(46)- scalaz-stream 基.. 下一篇Cats(2)- Free语法组合,Copro..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目