• ScalikeJDBC


    ScalikeJDBC基于SQL的简介DB访问

    1. 什么是ScalikeJDBC

    ScalikeJDBC是一款给Scala开发者使用的简洁DB访问类库,它是基于SQL的,使用者只需要关注SQL逻辑的编写,

    所有的数据库操作都交给ScalikeJDBC。这个类库内置包含了JDBC API,并且给用户提供了简单易用并且非常灵活的API

    并且,QueryDSL(通用查询查询框架)使你的代码类型安全的并且可重复使用。我们可以在生产环境大胆地使用这款DB访问类库。

    2. 项目中使用ScalikeJDBC

    2.1. 添加依赖

     1 <!-- scalikejdbc_2.11 -->
     2 <dependency>
     3     <groupId>org.scalikejdbc</groupId>
     4     <artifactId>scalikejdbc_2.11</artifactId>
     5     <version>2.5.0</version>
     6 </dependency>
     7 
     8 <!-- scalikejdbc-config_2.11 -->
     9 <dependency>
    10     <groupId>org.scalikejdbc</groupId>
    11     <artifactId>scalikejdbc-config_2.11</artifactId>
    12     <version>2.5.0</version>
    13 </dependency>
    14 
    15 <dependency>
    16     <groupId>mysql</groupId>
    17     <artifactId>mysql-connector-java</artifactId>
    18     <version>5.1.38</version>
    19 </dependency>

    2.2. 数据库CURD

    2.2.1. 配置数据库信息

    1 # MySQL example
    2 db.default.driver="com.mysql.jdbc.Driver"
    3 db.default.url="jdbc:mysql://localhost/bbs?characterEncoding=utf-8"
    4 db.default.user="root"
    5 db.default.password="123456"

        或者

    # db.sheep.driver="com.mysql.jdbc.Driver"
    # db.sheep.url="jdbc:mysql://localhost/bbs"
    # db.sheep.user="root"
    # db.sheep.password="123456"

    更多配置请参考官网.

    2.2.2. 加载数据配置信息

    1 // 加载数据库配置信息
    2 // 默认加载db.default.*
    3 DBs.setup()
    4 
    5 // 加载db.sheep配置信息
    6 DBs.setup('sheep)

    2.2.3. 查询数据库并封装数据

    // 加载数据库配置信息
    // 默认加载db.default.*
    DBs.setup()
    // 查询数据并返回单个列, 并将列数据封装到集合中
    val list: List[String] = DB readOnly { implicit session =>
        sql"select content from post".map(rs => rs.string("content")).list().apply()
    }
    for (s <- list ) {
        println(s)
    }
    // 用户实体
    case class Users(id: String, name: String, nickName: String)
    /**
      * 查询数据库,并将数据封装成对象,并返回一个集合
      */
    // 初始化数据库链接
    DBs.setup('sheep)
    val userses: List[Users] = NamedDB('sheep) readOnly { implicit session =>
        sql"SELECT * from users".map(rs => Users(rs.string("id"), rs.string("name"), rs.string("nickname"))).list().apply()
    }
    for (usr <- userses ) {
        println(usr.nickName)
    }

    2.2.4. 插入数据

    2.2.4.1. AutoCommit

    /**
      * 插入数据, 使用autoCommit
      */
    val insertResult: Int = DB.autoCommit { implicit session =>
        SQL("insert into users(name, nickname) values(?,?)").bind("test01", "test01").update().apply()
    }
    println(insertResult)

    2.2.4.2. 插入返回主键标识

    /**
      * 插入数据, 并返回主键
      */
    val id: Long = DB.localTx(implicit session => {
        sql"INSERT INTO users(name, nickname, sex) VALUES (?,?,?)".bind("测试", "000", 1).updateAndReturnGeneratedKey("nickname").apply()
    })
    println(id)

    2.2.4.3. 事务插入

    /**
      * 使用事务插入数据库
      */
    val tx: Int = DB.localTx { implicit session =>
        sql"INSERT INTO users(name, nickname, sex) VALUES (?,?,?)".bind("犊子", "000", 1).update().apply()
        // var s = 1 / 0
        SQL("INSERT INTO users(name, nickname, sex) values(?,?,?)").bind("王八犊子", "xxx", 0).update().apply()
    }
    println(s"tx = ${tx}")

    1.1.1. 更新数据

    /**
      * 更新数据
      */
    DB.localTx{ implicit session =>
        sql"UPDATE users SET pwd = ?".bind("88999").update().apply()
    }
  • 相关阅读:
    leetcode第35题--Valid Sudoku
    leetcode 34 Search Insert Position
    leetcode第33题--Search for a Range
    leetcode第32题--Search in Rotated Sorted Array
    leetcode第31题--Longest Valid Parentheses
    leetcode第30题--Next Permutation
    leetcode第29题--Substring with Concatenation of All Words
    leetcode第28题--Divide Two Integers
    leetcode第27题--Implement strStr()
    17_7_7 JDBC存储过程 + 事务
  • 原文地址:https://www.cnblogs.com/VisionY/p/11792161.html
Copyright © 2020-2023  润新知