Giriş
H2 ve HSQLDB farklı şeyler ve ben hep bu ikisini karıştırıyorum. jar dosyası com.h2database.h2 dizininde. Driver sınıfı ise org.h2.Driver ismine sahip.
H2 ve HSQLDB farklı şeyler ve ben hep bu ikisini karıştırıyorum. jar dosyası com.h2database.h2 dizininde. Driver sınıfı ise org.h2.Driver ismine sahip.
Bu ayarları yapınca SpringBoot bizim için bir EmbeddedDatabaseBuilder yaratır
Maven
<dependency><groupId>com.h2database</groupId><artifactId>h2</artifactId><scope>test</scope></dependency>
Şu satırı dahil ederiz. scope olarak runtime seçilmesinin sebebi, derleme için H2'ye mahsus bir kodun kullanılmaması ancak uygulamayı çalıştırmak için H2 gerekiyor.
<dependency><groupId>com.h2database</groupId><artifactId>h2</artifactId><scope>runtime</scope></dependency>
application-test.properties
url Alanı
Bu alan en önemli şey. Eğer bellekte veri tabanı istiyorsak "jdbc:h2:mem:dbname" şeklinde kullanırız. Eğer dosya veri tabanı istiyorsak "jdbc:h2:pathtofile" şeklinde kullanırız
Örnek
Şöyle yaparız. Burada veri tabanı integration test için kullanılıyor. Bu yüzden ilave parametreler veriliyor.
"jdbc:h2:mem:test:sample;DB_CLOSE_ON_EXIT=FALSE;
INIT=RUNSCRIPT FROM 'classpath:schema-generator.sql';"
Örnek
Bellekte veri tabanı için şöyle yaparız. h2:mem şeklinde kullanılıyor# Data source
spring.datasource.url=jdbc:h2:mem:test
spring.datasource.username=admin
spring.datasource.password=admin
spring.datasource.driver-class-name=org.h2.DriverDosyada veri tabanı için şöyle yaparız.
spring.datasource.url=jdbc:h2:~/test
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password= Bellekte veri tabanı için şöyle yaparız. h2:mem şeklinde kullanılıyor
spring:
    datasource:
        database: HSQL
        driverClassName: org.h2.Driver
        url: jdbc:h2:mem:MyDBName
        username: sa
        password:H2 web console ayarı için şöyle yaparız.
# H2
spring.h2.console.enabled=true
spring.h2.console.path=/h2
spring.datasource.url=jdbc:h2:file:~/mydb.db
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.DriverÖrnek
H2 web console ayarı için şöyle yaparız
server:
  port: ${port:8080}
spring:
  application:
    name: flyway-demo
  datasource:
    driver-class-name: org.h2.Driver
    username: sa
    password:
    url: "jdbc:h2:mem:db;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;DATABASE_TO_LOWER=TRUE;CASE_INSENSITIVE_IDENTIFIERS=TRUE"
  h2:
    console:
      enabled: true
      path: /h2-console
  jpa:
    show-sql: true
    hibernate:
      ddl-auto: noneH2 Konsol
http://localhost:8080/h2-consolehttp://localhost:8080/h2-console adresine gideriz. Böylece tabloları ve veriyi görebiliriz.. Diğer girdiler şöyle
JDBC URL: jdbc:h2:mem:db User Name: sa Password: <empty>



 
Hiç yorum yok:
Yorum Gönder