Adding Dependencies

Gradle 프로젝트

val exposed_version = "0.51.1"
dependencies {
    implementation("org.jetbrains.exposed", "exposed-core", exposed_version)
    implementation("org.jetbrains.exposed", "exposed-dao", exposed_version)
    implementation("org.jetbrains.exposed", "exposed-jdbc", exposed_version)
}

// Exposed
//	// exposed-core: 기본모듈, DSL 와 mapping을 포함한다.
	implementation("org.jetbrains.exposed:**exposed-core**:0.51.1")
//	// exposed-crypt: DB 제장할때 추가적인 칼럼에 대해서 암호화를 지원한다.
	implementation("org.jetbrains.exposed:**exposed-crypt**:0.51.1")
//	// exposed-dao: DAO api
	implementation("org.jetbrains.exposed:**exposed-dao**:0.51.1")
//	// exposed-java-time: date-time 을 지원하며, Java8 Time API를 기반으로한다.
	implementation("org.jetbrains.exposed:**exposed-java-time**:0.51.1")
//	// exposed-jdbc: Java JDBC API를 기반으로한 구현체
	implementation("org.jetbrains.exposed:**exposed-jdbc**:0.51.1")
//	// exposed-joda-time: JodaTime 라이브러리를 기반으로 하는 date-time 확장라이브러리
////	implementation("org.jetbrains.exposed:**exposed-joda-time**:0.51.1")
//	// exposed-json: JSON과 JSONB 데이터타입 지원
	implementation("org.jetbrains.exposed:**exposed-json**:0.51.1")
//	// exposed-kotlin-datetime: kotlinx-datetime을 기반으로한 date-time 확장
	implementation("org.jetbrains.exposed:**exposed-kotlin-datetime**:0.51.1")
//	// exposed-money: "javax.money:money-api" 로 부터 금전에 대한 확장 지원
	implementation("org.jetbrains.**exposed:exposed-money**:0.51.1")
//	// exposed-spring-boot-starter: SpringBoot에서 Hibernate 를 대체하기 위한 유틸리티
	implementation("org.jetbrains.exposed:**exposed-spring-boot-starter**:0.51.1")
	implementation("mysql:mysql-connector-java:8.0.33")

Transaction 만들기

데이터베이스 커넥션

Database.connect("jdbc:h2:mem:test", driver = "org.h2.Driver")

근데 우리는 h2가 아니라 mysql 쓸거임

Database.connect("jdbc:mysql://localhost:3306/test", driver = "com.mysql.cj.jdbc.Driver",
                 user = "testuser", password = "testuser1234")
// Gradle
implementation("mysql:mysql-connector-java:8.0.33")

커넥션을 확인하기 위해서 docker-compose.yaml 확인함

  mysql:
    image: mysql:latest
    platform: linux/arm64
    restart: always
    volumes:
      - ./db/mysql/data:/var/lib/mysql
      - ./db/mysql/init:/docker-entrypoint-initdb.d
    ports:
      - 3306:3306
    environment:
      MYSQL_ROOT_PASSWORD: root1234
      TZ: Asia/Seoul
      **MYSQL_DATABASE: myDb
      MYSQL_USER: testuser
      MYSQL_PASSWORD: testuser1234**
fun main(args: Array<String>) {
  //an example connection to H2 DB
  Database.connect("jdbc:h2:mem:test", driver = "org.h2.Driver")

  transaction {
    // print sql to std-out
    addLogger(StdOutSqlLogger)

    SchemaUtils.create (Cities)

    // insert new city. SQL: INSERT INTO Cities (name) VALUES ('St. Petersburg')
    val stPeteId = Cities.insert {
      it[name] = "St. Petersburg"
    } get Cities.id

    // 'select *' SQL: SELECT Cities.id, Cities.name FROM Cities
    println("Cities: ${Cities.selectAll()}")
  }
}

object Cities: IntIdTable() {
    val name = varchar("name", 50)
}
fun main(args: Array<String>) {
  //an example connection to H2 DB
  Database.connect("jdbc:h2:mem:test", driver = "org.h2.Driver")

  transaction {
    // print sql to std-out
    addLogger(StdOutSqlLogger)

    SchemaUtils.create (Cities)

    // insert new city. SQL: INSERT INTO Cities (name) VALUES ('St. Petersburg')
    val stPete = City.new {
            name = "St. Petersburg"
    }

    // 'select *' SQL: SELECT Cities.id, Cities.name FROM Cities
    println("Cities: ${City.all()}")
  }
}

object Cities: IntIdTable() {
    val name = varchar("name", 50)
}

class City(id: EntityID<Int>) : IntEntity(id) {
    companion object : IntEntityClass<City>(Cities)

    var name by Cities.name
}