Gradle

我们可以使用 ebean-init 自动设置 Gradle 应用程序。

以下是不使用 ebean-init 设置 Gradle 项目的说明。

添加 Ebean 插件

添加 ebean 插件,它将在 gradle 构建期间执行增强。

plugins {
    id('idea')
    id('java')
    id('io.ebean') version '13.25.0'
}

添加 Ebean 依赖项

dependencies {
    ...
    compile 'io.ebean:ebean:13.25.0'

    // query bean generation
    annotationProcessor 'io.ebean:querybean-generator:13.25.0'

    testCompile 'io.ebean:ebean-test:13.25.0'
    ...
}
  • querybean-generator - 查询 bean 的 APT 生成
  • ebean-test - 包括使用 docker 容器的测试

Kotlin

对于 Kotlin,我们将 annotationProcessor 更改为 kapt(如下所示),并使用 kotlin-querybean-generator,它会将查询 bean 生成为 Kotlin 而不是 Java。

dependencies {
    ...
    compile 'io.ebean:ebean:13.25.0'

    // query bean generation
    kapt 'io.ebean:kotlin-querybean-generator:13.25.0'

    testCompile 'io.ebean:ebean-test:13.25.0'
    ...
}

Ebean 部分选项

添加 ebean 部分以控制增强的调试级别。

ebean {
  debugLevel = 1
}

测试输出

Gradle 默认禁止输出日志记录。如果我们想要查看 SQL、DDL、事务日志记录等,我们需要在测试部分中将 testLogging.showStandardStreams 设置为 true,如下所示

test {
  testLogging.showStandardStreams = true
}

示例项目

测试输出

以下是 ./gradlew clean test 的示例输出。具体来说,我们可以看到

  • 正在增强的类(由于 ebean debugLevel = 1)
  • DDL、SQL 和事务(在本例中由 logback-test.xml 控制)
$ ./gradlew clean test

> Task :compileJava
ebean-enhance> cls: org/example/domain/BaseDomain  msg: enhanced
ebean-enhance> cls: org/example/domain/Customer  msg: enhanced

> Task :test
enhancement prior to running tests

Gradle suite > Gradle test > org.example.domain.CustomerTest.saveAndFind STANDARD_OUT
14:48:49.900 [Test worker] INFO  io.ebean.internal.DefaultContainer - DatabasePlatform name:db platform:h2
14:48:50.063 [Test worker] TRACE io.ebean.TXN - txn[1001] Begin
14:48:50.064 [Test worker] INFO  io.ebean.DDL - Executing db-create-all.sql - 1 statements
14:48:50.064 [Test worker] DEBUG io.ebean.DDL - executing 1 of 1 create table customer ( id                            bigint auto_increment not...
14:48:50.076 [Test worker] DEBUG io.ebean.TXN - txn[1001] Commit
14:48:50.086 [Test worker] DEBUG io.ebean.SQL - txn[1002] insert into customer (name, start_date, comments, version) values (?,?,?,?); --bind(Hello Rob,2018-01-30,[LOB],1)
14:48:50.098 [Test worker] DEBUG io.ebean.SUM - txn[1002] Inserted [Customer] [1]
14:48:50.098 [Test worker] DEBUG io.ebean.TXN - txn[1002] Commit
14:48:50.122 [Test worker] DEBUG io.ebean.SQL - txn[1003] select t0.id, t0.name, t0.start_date, t0.version from customer t0 where t0.id = ?  ; --bind(1, )
14:48:50.124 [Test worker] DEBUG io.ebean.SUM - txn[1003] FindBean type[Customer] origin[D88YBi.A.A] exeMicros[3052] rows[1] bind[1, ]
14:48:50.129 [Test worker] DEBUG io.ebean.SQL - txn[1004] select t0.id, t0.name, t0.start_date, t0.version from customer t0 where t0.id is not null ; --bind()
14:48:50.130 [Test worker] DEBUG io.ebean.SUM - txn[1004] FindMany type[Customer] origin[D88YBm.A.A] exeMicros[0] rows[0] predicates[t0.id is not null ] bind[]
hello Hello Rob
.. started on: 2018-01-30
14:48:50.136 [Test worker] DEBUG io.ebean.SQL - txn[1005] select t0.id, t0.name, t0.start_date, t0.version from customer t0 where t0.name like ?  and t0.id > ? ; --bind(BatOutOfHell%,1)
14:48:50.136 [Test worker] DEBUG io.ebean.SUM - txn[1005] FindMany type[Customer] origin[D88YBj.A.A] exeMicros[1619] rows[0] predicates[t0.name like ?  and t0.id > ? ] bind[BatOutOfHell%,1]
bats:BeanList size[0] list[]


BUILD SUCCESSFUL in 1s