ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Anorm 기본 사용
    scala jdbc/anorm 2023. 6. 13. 18:37

    http://playframework.github.io/anorm/ 의 standalone 사용을 소개한다.

     

    build.sbt

    libraryDependencies += "org.playframework.anorm" %% "anorm" % "2.7.0",
    libraryDependencies += "com.zaxxer" % "HikariCP" % "5.0.1"
    libraryDependencies += "mysql" % "mysql-connector-java" % "8.0.33"

     

    코드

    import anorm._
    import com.mysql.cj.jdbc.MysqlDataSource
    import com.zaxxer.hikari.HikariConfig
    import com.zaxxer.hikari.pool.HikariPool
    
    import java.util.concurrent.TimeUnit.MINUTES
    import scala.util.Using
    
    case class Person2(name: String, age: Option[Int])
    
    object AnormTest {
        def main(args: Array[String]): Unit = {
        val mysqlDataSource = new MysqlDataSource()
        mysqlDataSource.setUrl("jdbc:mysql://localhost:3306/test")
        mysqlDataSource.setUser("admin")
        mysqlDataSource.setPassword("admin123")
        mysqlDataSource.setAutoReconnect(true)
    
        val hikariConfig = new HikariConfig()
        hikariConfig.setDataSource(mysqlDataSource)
        hikariConfig.setMaximumPoolSize(40)
        hikariConfig.setConnectionTimeout(MINUTES.toMillis(1))
        hikariConfig.setMaxLifetime(MINUTES.toMillis(60))
    
        ///////////////////////////////////////////////////////////////////////////////////////////
        // select single
        ///////////////////////////////////////////////////////////////////////////////////////////
        val pool = new HikariPool(hikariConfig)
        val age: Option[Int] = Using.resource(pool.getConnection(10000L)) { implicit connection =>
          SQL("""select name, age from person where name={name}""")
            .on("name" -> "KJM")
            .as(SqlParser.int("age").?.single)
        }
        println(age)
    
        ///////////////////////////////////////////////////////////////////////////////////////////
        // select multi(*), option(?)
        ///////////////////////////////////////////////////////////////////////////////////////////
        val personParser: RowParser[Person2] = Macro.namedParser[Person2]
    //    val personParser: RowParser[Person2] = Macro.parser[Person2]("name", "age") // 칼럼명을 직접 지정
    //    val personParser = (SqlParser.str("name") ~ SqlParser.int("age").?).map { case name ~ age =>
    //      Person2(name, age)
    //    }
    //    val personParser: RowParser[Person2] = for {
    //      name <- SqlParser.str("name")
    //      age <- SqlParser.int("age").?
    //    } yield Person2(name, age)
    
        val persons: Seq[Person2] = Using.resource(pool.getConnection(10000L)) { implicit connection =>
          SQL("select * from person").as(personParser.*)
        }
        println(persons)
    
        ///////////////////////////////////////////////////////////////////////////////////////////
        // Batch update
        ///////////////////////////////////////////////////////////////////////////////////////////
        val batch = BatchSql(
          "insert into person(name, age) values({name}, {age})",
          Seq[NamedParameter]("name" -> "A", "age" -> 1),
          Seq[NamedParameter]("name" -> "B", "age" -> 2)
        )
    
        // array of update count
        val batchRes: Array[Int] = Using.resource(pool.getConnection(10000L)) { implicit connection =>
          batch.execute()
        }
    
        pool.shutdown()
      }
    }

    댓글

Designed by Tistory.