Make easier switch between MySQL and HSQLDB #184

Signed-off-by: Mariusz Borwin <mariusz.borwin@gmail.com>
This commit is contained in:
Mariusz Borwin 2016-09-19 14:57:19 +02:00
parent 08bf0a5ae6
commit fcf1415a89
3 changed files with 59 additions and 28 deletions

44
pom.xml
View file

@ -41,6 +41,12 @@
<cobertura.version>2.7</cobertura.version>
<jpa.database>HSQL</jpa.database>
<jdbc.driverClassName>org.hsqldb.jdbcDriver</jdbc.driverClassName>
<jdbc.url>jdbc:hsqldb:mem:petclinic</jdbc.url>
<jdbc.username>sa</jdbc.username>
<jdbc.password></jdbc.password>
</properties>
<dependencyManagement>
@ -185,13 +191,6 @@
<scope>runtime</scope>
</dependency>
<!-- For MySql only -->
<!--dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-driver.version}</version>
</dependency-->
<!-- HIBERNATE -->
<dependency>
<groupId>org.hibernate</groupId>
@ -273,6 +272,12 @@
<build>
<defaultGoal>install</defaultGoal>
<finalName>petclinic</finalName>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<testResources>
<testResource>
<!-- declared explicitly so Spring config files can be placed next to their corresponding JUnit test class -->
@ -377,6 +382,14 @@
</execution>
</executions>
</plugin>
<!--Specify the encoding for copying resources-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
<reporting>
@ -451,6 +464,23 @@
</plugins>
</build>
</profile>
<profile>
<id>MYSQL</id>
<properties>
<jpa.database>MYSQL</jpa.database>
<jdbc.driverClassName>com.mysql.jdbc.Driver</jdbc.driverClassName>
<jdbc.url>jdbc:mysql://localhost:3306/petclinic?useUnicode=true</jdbc.url>
<jdbc.username>root</jdbc.username>
<jdbc.password>petclinic</jdbc.password>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-driver.version}</version>
</dependency>
</dependencies>
</profile>
</profiles>
<url>demopetclinic</url>

View file

@ -20,7 +20,23 @@ Our issue tracker is available here: https://github.com/spring-projects/spring-p
In its default configuration, Petclinic uses an in-memory database (HSQLDB) which
gets populated at startup with data. A similar setup is provided for MySql in case a persistent database configuration is needed.
Note that whenever the database type is changed, the data-access.properties file needs to be updated and the mysql-connector-java artifact from the pom.xml needs to be uncommented.
To run petclinic locally using MySQL database, it is needed to run with 'MYSQL' profile defined in main pom.xml file.
```
./mvnw tomcat7:run -P MYSQL
```
Before do this, would be good to check properties defined in MYSQL profile inside pom.xml file.
```
<properties>
<jpa.database>MYSQL</jpa.database>
<jdbc.driverClassName>com.mysql.jdbc.Driver</jdbc.driverClassName>
<jdbc.url>jdbc:mysql://localhost:3306/petclinic?useUnicode=true</jdbc.url>
<jdbc.username>root</jdbc.username>
<jdbc.password>petclinic</jdbc.password>
</properties>
```
You may start a MySql database with docker:

View file

@ -10,25 +10,10 @@ jdbc.dataLocation=classpath:db/hsqldb/populateDB.sql
jpa.showSql=true
#-------------------------------------------------------------------------------
# HSQL Settings
jdbc.driverClassName=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:mem:petclinic
jdbc.username=sa
jdbc.password=
jdbc.driverClassName=${jdbc.driverClassName}
jdbc.url=${jdbc.url}
jdbc.username=${jdbc.username}
jdbc.password=${jdbc.password}
# Property that determines which database to use with an AbstractJpaVendorAdapter
jpa.database=HSQL
#-------------------------------------------------------------------------------
# MySQL Settings
#jdbc.driverClassName=com.mysql.jdbc.Driver
#jdbc.url=jdbc:mysql://localhost:3306/petclinic?useUnicode=true&characterEncoding=UTF-8
#jdbc.username=root
#jdbc.password=petclinic
# Property that determines which database to use with an AbstractJpaVendorAdapter
#jpa.database=MYSQL
jpa.database=${jpa.database}