scalajdbc连接数据库 初级版 写了很多注释

该博客展示了如何使用 Scala 的 scalikejdbc 库进行数据库操作,包括连接配置、读写事务处理,以及增删改查(CRUD)的具体实现。示例中涉及了 MySQL 数据库的连接,展示了 SQL 查询和数据绑定的方法。
摘要由CSDN通过智能技术生成

main.resources.application.conf

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"

代码

package cn.kgc.scalajdbc.jdbc

import scalikejdbc.{NamedDB, SQL}
import scalikejdbc.config.DBs

object JdbcTest {
  def main(args: Array[String]): Unit = {
    //加载配置信息 : 自动解析 *.conf 下 db.db_shop12._的所有参数
    val db:Symbol = Symbol.apply("test")
    DBs.setup(db)

    //创建增删改查的操作方法
    //readonly : 查询
    //autocommit : 非事务增删改
    //localTx : 事务操作

    def select(where:String="")={
      val builder = new StringBuilder("select user_name,user_phone,user_pid from user_info")
      if(where.trim.length>0){
        builder.append(where)
      }
      //NameDB(db)  负责连接池创建
      NamedDB(db).readOnly{
        //sql(...) 解析数据语句
        //bind(...)赋给sql语句中的参数?传值
        //map() 执行查询操作并逐行处理
        //list() 将Map()的结果以列表返回
        //apply() 提取执行结果
        implicit session => SQL(builder.toString).map(
          //查询sql执行完毕后,逐行row提取,x就是数据表中的一行记录
          row=>(
            row.string("user_name"),
            row.string("user_phone"),
            row.string("user_pid")
          )
        ).list().apply()
      }
    }

    //批量插入  localTx:  事务操作
    def batchInsert(arr:Array[(Int,String,Int,String,String,String,Int)])={
      //负责连接池创建
      NamedDB(db).localTx{
        implicit session=>{
          var affectedRows = 0
          arr.foreach(x=>{
            //sql(...) 解析数据语句
            //bind(...)赋给sql语句中的参数?传值
            //update() 执行增删改操作
            //apply() 提取执行结果
            affectedRows += SQL("insert into studentinfo(stuId,stuName,stuAge,stuGender,mobile,tuition,fkClassId) values(?,?,?,?,?,?,?)")
              .bind(x._1,x._2,x._3,x._4,x._5,x._6,x._7).update().apply()
            //affectedRows += SQL("insert into sqp_incr_time(incrName) values(?)").bind(x).update().apply()
          })
          affectedRows
        }
      }
    }

    //autocommit :非事务增删改
    //单条记录新增

    //批量修改
    def update(x:(String,Int,String,String,String,Int,Int))={
      //负责连接池创建
      NamedDB(db).autoCommit{
        implicit session=>{
          var affectedRows:Int = 0
            //sql(...) 解析数据语句
            //bind(...)赋给sql语句中的参数?传值
            //update() 执行增删改操作
            //apply() 提取执行结果
          affectedRows += SQL("update studentinfo set stuName=?,stuAge=?,stuGender=?,mobile=?,tuition=?,fkClassId=? where stuId=?")
              .bind(x._1,x._2,x._3,x._4,x._5,x._6,x._7).update().apply()
            //affectedRows += SQL("insert into sqp_incr_time(incrName) values(?)").bind(x).update().apply()
          affectedRows
        }
      }
    }

    //删除
    def delete(x:Int)={
      //负责连接池创建
      NamedDB(db).autoCommit{
        implicit session=>{
          //sql(...) 解析数据语句
          //bind(...)赋给sql语句中的参数?传值
          //update() 执行增删改操作
          //apply() 提取执行结果
          SQL("delete from studentinfo where stuId=?")
            .bind(x).update().apply()
          //affectedRows += SQL("insert into sqp_incr_time(incrName) values(?)").bind(x).update().apply()
        }
      }
    }


    //select().foreach(println)

    //println(batchInsert(Array((52,"任志杰1",18,"男","13327791024","23345",1),(53,"任志杰2",18,"男","13327791036","23325",2))))
    //println(batchInsert(Array((54,"任志杰3",18,"男","13327791345","23125",1))))

    //println(update("任志杰4",18,"男","13327791345","23125",1,54))

    //println(delete(52))

    //println(batchInsert(Array("abc","def")))
  }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值