集成到 Spring Boot

Spring Boot 中集成使用 HSQLDB

首先我们添加最新的依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    <version>2.1.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.hsqldb</groupId>
    <artifactId>hsqldb</artifactId>
    <version>2.4.0</version>
    <scope>runtime</scope>
</dependency>

然后可以以服务器模式运行 HSQLDB:

java -cp ../lib/hsqldb.jar org.hsqldb.server.Server --database.0 file.testdb --dbname0.testdb

或者以内存模式运行,其配置方式分别如下:

spring.datasource.driver-class-name=org.hsqldb.jdbc.JDBCDriver
spring.datasource.url=jdbc:hsqldb:hsql://localhost/testdb
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=update

spring.datasource.driver-class-name=org.hsqldb.jdbc.JDBCDriver
spring.datasource.url=jdbc:hsqldb:mem:testdb;DB_CLOSE_DELAY=-1
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=create

然后我们创建实体类以及 CrudRepository:

@Entity
@Table(name = "customers")
public class Customer {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private long id;

  private String name;

  private String email;
// standard constructors / setters / getters / toString
}

@Repository
public interface CustomerRepository extends CrudRepository<Customer, Long> {}

最后的测试代码如下所示:

@RunWith(SpringRunner.class)
@SpringBootTest
public class CustomerRepositoryTest {
  @Autowired
  private CustomerRepository customerRepository;

  @Test
  public void whenFindingCustomerById_thenCorrect() {
    customerRepository.save(new Customer("John", "john@domain.com"));
    assertThat(customerRepository.findById(1L)).isInstanceOf(Optional.class);
  }

  @Test
  public void whenFindingAllCustomers_thenCorrect() {
    customerRepository.save(new Customer("John", "john@domain.com"));
    customerRepository.save(new Customer("Julie", "julie@domain.com"));
    assertThat(customerRepository.findAll()).isInstanceOf(List.class);
  }

  @Test
  public void whenSavingCustomer_thenCorrect() {
    customerRepository.save(new Customer("Bob", "bob@domain.com"));
    Customer customer = customerRepository
      .findById(1L)
      .orElseGet(() -> new Customer("john", "john@domain.com"));
    assertThat(customer.getName()).isEqualTo("Bob");
  }
}
下一页