Files
exposed-wiki/Getting-Started.md
2020-04-08 12:29:24 +03:00

3.0 KiB

Download

Maven

<repositories>
  <repository>
    <id>exposed</id>
    <name>exposed</name>
    <url>https://dl.bintray.com/kotlin/exposed</url>
  </repository>
</repositories>

<dependencies>
  <dependency>
    <groupId>org.jetbrains.exposed</groupId>
    <artifactId>exposed</artifactId>
    <version>0.9.1</version>
  </dependency>
</dependencies>

Gradle

repositories {
  maven {
    url  "https://dl.bintray.com/kotlin/exposed" 
  }
}
dependencies {
  compile 'org.jetbrains.exposed:exposed:0.9.1'
}
  • Note: There is a separate artifact for spring-transaction

Getting Started

Starting a transaction

Every database access using Expose is starting by obtaining a connection and creating a transaction.

To get a connection:

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

It is also possible to provide javax.sql.DataSource for advanced behaviors such as connection pooling:

Database.connect(dataSource)

After obtaining a connection all SQL statements should be placed inside a transaction:

transaction {
  // Statements here
}

To see the actual DB calls, add a logger:

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

DSL & DAO

Expose comes in two flavors: DSL (Domain Specific Language) and DAO (Data Access Object).
On a high level, DSL means type-safe syntax that is similar to SQL whereas DAO means doing CRUD operations on entities.
Observe the below examples and head on to the specific section of each API for more details.

Your first Exposed DSL



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
    logger.addLogger(StdOutSqlLogger)
    
    // 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)
}

More on DSL API

Your first Exposed DAO



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
    logger.addLogger(StdOutSqlLogger)
    
    // 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
}

More on DAO API

Or... back to Introduction