Implement H2
In this case first will be use H2
- Add H2 to pom.xml, in the dependencies section.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
- Modify application.properties file adding the following properties:
The files is in scr/main/resources
- spring.h2.console.enabled=true
- spring.h2.console.path=/h2
The value in path property is the URL to see H2 database console.
Ex. http://localhost:8080/h2/
In this case the url is specifying that the db will be stored in a file
Other configurations:
Ex. http://localhost:8080/h2/
Migration
Use Flyway
Add in pom.xaml the dependency
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
- Create the folder in scr/resources
- db ->
- migration
- Add to the application.properties
- flyway.baseline-description=true
- spring.jpa.hibernate.ddl-auto=false;
- Create file
- V2_create_db.sql
Always start the file as V2__
Ex.
CREATE TABLE SHIPWRECK(
ID INT AUTO_INCREMENT,
NAME VARCHAR(255),
DESCRIPTION VARCHAR(2000),
CONDITION VARCHAR(255),
DEPTH INT,
LATITUDE DOUBLE,
LONGITUDE DOUBLE,
YEAR_DISCOVERED INT
);
Use Flyway
Add in pom.xaml the dependency
Add in pom.xaml the dependency
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
- Create the folder in scr/resources
- db ->
- migration
- Add to the application.properties
- flyway.baseline-description=true
- spring.jpa.hibernate.ddl-auto=false;
- Create file
- V2_create_db.sql
Always start the file as V2__
Ex.
Ex.
CREATE TABLE SHIPWRECK(
ID INT AUTO_INCREMENT,
NAME VARCHAR(255),
DESCRIPTION VARCHAR(2000),
CONDITION VARCHAR(255),
DEPTH INT,
LATITUDE DOUBLE,
LONGITUDE DOUBLE,
YEAR_DISCOVERED INT
);
Datasource Configuration
Add in application properties:
spring.datasource.url=jdbc:h2:file:~/dasboot
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
Other configurations:
spring.datasource.max-active=10
spring.datasource.max-idle=8
spring.datasource.max-wait=10000
spring.datasource.min-evictable-idle-time-millis=1000
spring.datasource.min-idle=8
spring.datasource.time-between-eviction-runs-millis=1
Datasource for Spring
- Create a folder for the configuration in
- scr/main/java
- <project>
- config
- Create a class as <Name>Configuration
- Add above the class the annotation
- @Configuration
- Create a DataSource Bean
-
@Bean@ConfigurationProperties(prefix="spring.datasource")@Primarypublic DataSource dataSource(){return DataSourceBuilder.create().build();}
- Second Datasource
- @Bean@ConfigurationProperties(prefix="datasource.flyway")@Primarypublic DataSource flyWayDataSource(){return DataSourceBuilder.create().build();}
- The @Primary annotation is to avoid problems when many datasources are configured
- The prefix is how is defined in the application.properties
Comentarios
Publicar un comentario