<dependencies> <dependency> <groupId>org.scalikejdbc</groupId> <artifactId>scalikejdbc_2.11</artifactId> <version>2.5.0</version> </dependency> <!-- 解析配置文件application.properties --> <dependency> <groupId>org.scalikejdbc</groupId> <artifactId>scalikejdbc-config_2.11</artifactId> <version>2.5.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> </dependencies>
# MySQL example db.default.driver="com.mysql.jdbc.Driver" db.default.url="jdbc:mysql://localhost:3306/bank?characterEncoding=utf-8" db.default.user="root" db.default.password="root" #测试 db.test.driver="com.mysql.jdbc.Driver" db.test.url="jdbc:mysql://localhost:3306/test?characterEncoding=utf-8" db.test.user="root" db.test.password="root"
// 加载数据库配置信息 // 默认加载 db.default.* DBs.setup() // 加载 db.sheep 配置信息 DBs.setup('sheep)
import java.sql.Connection import scalikejdbc.{ConnectionPool, DB, SQL} import scalikejdbc.config.DBs //使用scala 操作数据库 /*case class offsets(topic:String,groupId:String,partitionId:Int,offset)*/ object TestCRUD1 { case class Student(age:Int,name:String,sex:Int) //查询 def query()={ import scalikejdbc._ val students = DB readOnly(implicit session => { sql"select * from accounter ".map(rs => Student(rs.int(1), rs.string("aname"), rs.int(3))).list().apply() }) students.foreach(stu=>println(s"id:${stu.age},name:${stu.name},count:${stu.sex}")) //查询数据并返回单个列 并将列数据封装到集合中 } //测试库查询 def test()={ import scalikejdbc._ val students = NamedDB('test) readOnly(implicit session => { sql"select * from accounter ".map(rs => Student(rs.int(1), rs.string(2), rs.int(3))).list().apply() }) students.foreach(println) } //插入 def insert()={ import scalikejdbc._ val i = DB.autoCommit { implicit session => SQL("insert into accounter(aname,balance) value(?,?)").bind("te", "1").update().apply() } println(i) } //更新 def update()={ import scalikejdbc._ DB.localTx{implicit session=> sql"update accounter set aname=? where aid=2".bind("123").update().apply() } } //更新修改 def update1()={ val s = DB autoCommit (implicit session => { import scalikejdbc._ sql"update accounter set aname='ls' where aid=?".bind(2).update().apply() }) } //删除数据 def del()={ import scalikejdbc._ val row=DB autoCommit(implicit session=>{ sql"delete from accounter where aid=?".bind(1).update().apply() }) println(s"受影响的行数+$row") } def data(): Unit ={ import scalikejdbc._ val result: List[String] = NamedDB('zcw) readOnly(implicit session => { sql"select username from tb_user".map(rs =>{ rs.string("username") }).list().apply() }) result.foreach(println(_)) } def main(args: Array[String]): Unit = { //解析配置文件 DBs.setupAll() //加载指定数据库的配置文件 DBs.setup('test) //query() //test() //insert() //update() // update1() //del() //连接池 import scalikejdbc._ val conn: Connection = ConnectionPool.borrow() val tuples = using(DB(ConnectionPool.borrow())) { db => db.readOnly { implicit session => sql"select * from accounter".map(rs => (rs.int(1), rs.string(2), rs.int(3))).list().apply() } } tuples.foreach(println(_)) } }