Home  >  Article  >  Database  >  scala数据库访问tool slick

scala数据库访问tool slick

WBOY
WBOYOriginal
2016-06-07 15:35:591146browse

Slick 是 TypeSafe 推出的 Scala 数据库访问库。开发者可以使用 Scala 语言风来编写数据查询,而不是用 SQL,示例代码: package com.testimport scala.slick.driver.MySQLDriver.simple._import com.mysql.jdbc.jdbc2.optional.MysqlDataSourceimport scala

Slick 是 TypeSafe 推出的 Scala 数据库访问库。开发者可以使用 Scala 语言风格来编写数据查询,而不是用 SQL,示例代码:

package com.test

import scala.slick.driver.MySQLDriver.simple._
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource
import scala.slick.session.Database
import scala.slick.session.Session

object Supplier extends Table[(String,Int)]("test") {
	def name = column[String]("name")
	def age = column[Int]("age")
	
	def * = name ~ age
	
	def main(args: Array[String]) {
		val session = Sandbox.database
		val query = tableToQuery(Supplier)
		
		//查询
		//query.selectStatement
		
		//query.foreach(println(_))(session)
		
		//更新
//		val uq = Supplier.filter( p => p.name==="ricki").map(_.age)
//		
//		 uq.updateStatement
//		 
//		 uq.update(24)(session)
		//插入
		
//		val in = Supplier.name ~ Supplier.age
//		
//		in.insertStatement
//		
//		in.insert("cherry",1)(session)
		
		//删除
		
		val dq = Supplier.filter(_.name==="cherry")
		
		dq.deleteStatement
		
		dq.delete(session)
		
		session.close
	}
}

object Sandbox {
  
	def database:Session = {
	  val dataSource = new MysqlDataSource()
	  		dataSource.setUser("root")
	  		dataSource.setPassword("root")
	  		dataSource.setDatabaseName("test")
	  
	  val dataBase = Database.forDataSource(dataSource)
	  dataBase.createSession
	}
}

详情教程见官网:http://slick.typesafe.com/docs/

官方提供的第三方教程:http://mackler.org/LearningSlick/#id11540725

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn