第一个实体
创建一个包 org.example.domain
,并在其中创建一个实体 Bean,如 Customer.java
package org.example.domain;
import javax.persistence.Id;
import javax.persistence.Entity;
@Entity
public class Customer {
@Id
long id;
String name;
// getters and setters
}
package org.example.domain
import javax.persistence.Entity
import javax.persistence.Id
@Entity
class Customer {
@Id
var id: Long = 0
var name: String? = null
}
第一个测试
在 src/test
中创建一个测试,如 CustomerTest.java
package org.example.domain;
import org.junit.Test;
import io.ebean.DB;
import io.ebean.Database;
public class CustomerTest {
@Test
public void insertFindDelete() {
Customer customer = new Customer();
customer.setName("Hello world");
// insert the customer in the DB
DB.save(customer);
// Find by Id
Customer foundHello = database.find(Customer.class, 1);
// delete the customer
DB.delete(customer);
}
}
package org.example.domain
import io.ebean.DB
import org.junit.Test
class CustomerTest {
@Test
fun insert_update_delete() {
val customer = Customer()
customer.name = "Hello entity bean"
// insert
DB.save(customer)
// find by Id
var found = DB.find(Customer::class.java, 1);
DB.delete(found);
}
}
运行测试
通过 IDE 以及 Maven 或 Gradle 运行测试。检查日志以确认您看到了预期的 DDL
和 SQL
。