Merge pull request #189 from vfedoriv/master

PostgreSQL database support
This commit is contained in:
Antoine Rey 2016-10-15 14:41:31 +02:00 committed by GitHub
commit bae94cf327
5 changed files with 220 additions and 2 deletions

20
pom.xml
View file

@ -29,6 +29,8 @@
<!-- Others --> <!-- Others -->
<mysql-driver.version>5.1.36</mysql-driver.version> <mysql-driver.version>5.1.36</mysql-driver.version>
<postgresql-driver.version>9.4.1211.jre7</postgresql-driver.version>
<cobertura.version>2.7</cobertura.version> <cobertura.version>2.7</cobertura.version>
@ -443,6 +445,24 @@
</dependency> </dependency>
</dependencies> </dependencies>
</profile> </profile>
<profile>
<id>POSTGRESQL</id>
<properties>
<jpa.database>POSTGRESQL</jpa.database>
<jpa.databasePlatform>org.hibernate.dialect.PostgreSQLDialect</jpa.databasePlatform>
<jdbc.driverClassName>org.postgresql.Driver</jdbc.driverClassName>
<jdbc.url>jdbc:postgresql://localhost:5432/petclinic</jdbc.url>
<jdbc.username>postgres</jdbc.username>
<jdbc.password>petclinic</jdbc.password>
</properties>
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgresql-driver.version}</version>
</dependency>
</dependencies>
</profile>
</profiles> </profiles>
<url>demopetclinic</url> <url>demopetclinic</url>

View file

@ -19,8 +19,11 @@ Our issue tracker is available here: https://github.com/spring-projects/spring-p
## Database configuration ## Database configuration
In its default configuration, Petclinic uses an in-memory database (HSQLDB) which 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. gets populated at startup with data.
To run petclinic locally using MySQL database, it is needed to run with 'MYSQL' profile defined in main pom.xml file. A similar setups is provided for MySql and PostgreSQL in case a persistent database configuration is needed.
To run petclinic locally using persistent database, it is needed to run with profile defined in main pom.xml file.
For MySQL database, it is needed to run with 'MYSQL' profile defined in main pom.xml file.
``` ```
./mvnw tomcat7:run -P MYSQL ./mvnw tomcat7:run -P MYSQL
@ -44,6 +47,26 @@ You may start a MySql database with docker:
docker run -e MYSQL_ROOT_PASSWORD=petclinic -e MYSQL_DATABASE=petclinic -p 3306:3306 mysql:5.7.8 docker run -e MYSQL_ROOT_PASSWORD=petclinic -e MYSQL_DATABASE=petclinic -p 3306:3306 mysql:5.7.8
``` ```
For PostgreSQL database, it is needed to run with 'POSTGRESQL' profile defined in main pom.xml file.
```
./mvnw tomcat7:run -P POSTGRESQL
```
Before do this, would be good to check properties defined in POSTGRESQL profile inside pom.xml file.
```
<properties>
<jpa.database>POSTGRESQL</jpa.database>
<jpa.databasePlatform>org.hibernate.dialect.PostgreSQLDialect</jpa.databasePlatform>
<jdbc.driverClassName>org.postgresql.Driver</jdbc.driverClassName>
<jdbc.url>jdbc:postgresql://localhost:5432/petclinic</jdbc.url>
<jdbc.username>postgres</jdbc.username>
<jdbc.password>petclinic</jdbc.password>
</properties>
```
## Working with Petclinic in Eclipse/STS ## Working with Petclinic in Eclipse/STS
### prerequisites ### prerequisites

View file

@ -0,0 +1,90 @@
CREATE DATABASE petclinic
WITH OWNER = postgres
ENCODING = 'UTF8'
TABLESPACE = pg_default
CONNECTION LIMIT = -1;
CREATE TABLE IF NOT EXISTS vets (
id SERIAL,
first_name VARCHAR(30),
last_name VARCHAR(30),
CONSTRAINT pk_vets PRIMARY KEY (id)
);
CREATE INDEX idx_vets_last_name ON vets (last_name);
ALTER SEQUENCE vets_id_seq RESTART WITH 100;
CREATE TABLE IF NOT EXISTS specialties (
id SERIAL,
name VARCHAR(80),
CONSTRAINT pk_specialties PRIMARY KEY (id)
);
CREATE INDEX idx_specialties_name ON specialties (name);
ALTER SEQUENCE specialties_id_seq RESTART WITH 100;
CREATE TABLE IF NOT EXISTS vet_specialties (
vet_id INT NOT NULL,
specialty_id INT NOT NULL,
FOREIGN KEY (vet_id) REFERENCES vets(id),
FOREIGN KEY (specialty_id) REFERENCES specialties(id),
CONSTRAINT unique_ids UNIQUE (vet_id,specialty_id)
);
CREATE TABLE IF NOT EXISTS types (
id SERIAL,
name VARCHAR(80),
CONSTRAINT pk_types PRIMARY KEY (id)
);
CREATE INDEX idx_types_name ON types (name);
ALTER SEQUENCE types_id_seq RESTART WITH 100;
CREATE TABLE IF NOT EXISTS owners (
id SERIAL,
first_name VARCHAR(30),
last_name VARCHAR(30),
address VARCHAR(255),
city VARCHAR(80),
telephone VARCHAR(20),
CONSTRAINT pk_owners PRIMARY KEY (id)
);
CREATE INDEX idx_owners_last_name ON owners (last_name);
ALTER SEQUENCE owners_id_seq RESTART WITH 100;
CREATE TABLE IF NOT EXISTS pets (
id SERIAL,
name VARCHAR(30),
birth_date DATE,
type_id INT NOT NULL,
owner_id INT NOT NULL,
FOREIGN KEY (owner_id) REFERENCES owners(id),
FOREIGN KEY (type_id) REFERENCES types(id),
CONSTRAINT pk_pets PRIMARY KEY (id)
);
CREATE INDEX idx_pets_name ON pets (name);
ALTER SEQUENCE pets_id_seq RESTART WITH 100;
CREATE TABLE IF NOT EXISTS visits (
id SERIAL,
pet_id INT NOT NULL,
visit_date DATE,
description VARCHAR(255),
FOREIGN KEY (pet_id) REFERENCES pets(id),
CONSTRAINT pk_visits PRIMARY KEY (id)
);
ALTER SEQUENCE visits_id_seq RESTART WITH 100;

View file

@ -0,0 +1,32 @@
================================================================================
=== Spring PetClinic sample application - PostgreSQL Configuration ===
================================================================================
@author Sam Brannen
@author Costin Leau
--------------------------------------------------------------------------------
1) Download and install the PostgreSQL database,
which can be found here: https://www.postgresql.org/download/
2) Dependency for postgresql-driver included in Petclinic pom.xml file.
Alternatively, download PostgreSQL JDBC driver from here: https://jdbc.postgresql.org/download.html
and copy the PostgreSQL JDBC driver JAR file (e.g., postgresql-9.4.1211.jre7.jar) into
the db/postgesql directory.
3) Create the PetClinic database
CREATE DATABASE petclinic
WITH OWNER = postgres
ENCODING = 'UTF8'
TABLESPACE = pg_default
CONNECTION LIMIT = -1;
and execute the "db/postgresql/initDB.sql" script, then execute "db/postgresql/populateDB.sql"
4) Open "src/main/resources/spring/datasource-config.xml"; comment block
<jdbc:initialize-database data-source="dataSource">
<jdbc:script location="${jdbc.initLocation}"/>
<jdbc:script location="${jdbc.dataLocation}"/>
</jdbc:initialize-database>

View file

@ -0,0 +1,53 @@
INSERT INTO vets VALUES (1, 'James', 'Carter');
INSERT INTO vets VALUES (2, 'Helen', 'Leary');
INSERT INTO vets VALUES (3, 'Linda', 'Douglas');
INSERT INTO vets VALUES (4, 'Rafael', 'Ortega');
INSERT INTO vets VALUES (5, 'Henry', 'Stevens');
INSERT INTO vets VALUES (6, 'Sharon', 'Jenkins');
INSERT INTO specialties VALUES (1, 'radiology');
INSERT INTO specialties VALUES (2, 'surgery');
INSERT INTO specialties VALUES (3, 'dentistry');
INSERT INTO vet_specialties VALUES (2, 1);
INSERT INTO vet_specialties VALUES (3, 2);
INSERT INTO vet_specialties VALUES (3, 3);
INSERT INTO vet_specialties VALUES (4, 2);
INSERT INTO vet_specialties VALUES (5, 1);
INSERT INTO types VALUES (1, 'cat');
INSERT INTO types VALUES (2, 'dog');
INSERT INTO types VALUES (3, 'lizard');
INSERT INTO types VALUES (4, 'snake');
INSERT INTO types VALUES (5, 'bird');
INSERT INTO types VALUES (6, 'hamster');
INSERT INTO owners VALUES (1, 'George', 'Franklin', '110 W. Liberty St.', 'Madison', '6085551023');
INSERT INTO owners VALUES (2, 'Betty', 'Davis', '638 Cardinal Ave.', 'Sun Prairie', '6085551749');
INSERT INTO owners VALUES (3, 'Eduardo', 'Rodriquez', '2693 Commerce St.', 'McFarland', '6085558763');
INSERT INTO owners VALUES (4, 'Harold', 'Davis', '563 Friendly St.', 'Windsor', '6085553198');
INSERT INTO owners VALUES (5, 'Peter', 'McTavish', '2387 S. Fair Way', 'Madison', '6085552765');
INSERT INTO owners VALUES (6, 'Jean', 'Coleman', '105 N. Lake St.', 'Monona', '6085552654');
INSERT INTO owners VALUES (7, 'Jeff', 'Black', '1450 Oak Blvd.', 'Monona', '6085555387');
INSERT INTO owners VALUES (8, 'Maria', 'Escobito', '345 Maple St.', 'Madison', '6085557683');
INSERT INTO owners VALUES (9, 'David', 'Schroeder', '2749 Blackhawk Trail', 'Madison', '6085559435');
INSERT INTO owners VALUES (10, 'Carlos', 'Estaban', '2335 Independence La.', 'Waunakee', '6085555487');
INSERT INTO pets VALUES (1, 'Leo', '2000-09-07', 1, 1);
INSERT INTO pets VALUES (2, 'Basil', '2002-08-06', 6, 2);
INSERT INTO pets VALUES (3, 'Rosy', '2001-04-17', 2, 3);
INSERT INTO pets VALUES (4, 'Jewel', '2000-03-07', 2, 3);
INSERT INTO pets VALUES (5, 'Iggy', '2000-11-30', 3, 4);
INSERT INTO pets VALUES (6, 'George', '2000-01-20', 4, 5);
INSERT INTO pets VALUES (7, 'Samantha', '1995-09-04', 1, 6);
INSERT INTO pets VALUES (8, 'Max', '1995-09-04', 1, 6);
INSERT INTO pets VALUES (9, 'Lucky', '1999-08-06', 5, 7);
INSERT INTO pets VALUES (10, 'Mulligan', '1997-02-24', 2, 8);
INSERT INTO pets VALUES (11, 'Freddy', '2000-03-09', 5, 9);
INSERT INTO pets VALUES (12, 'Lucky', '2000-06-24', 2, 10);
INSERT INTO pets VALUES (13, 'Sly', '2002-06-08', 1, 10);
INSERT INTO visits VALUES (1, 7, '2010-03-04', 'rabies shot');
INSERT INTO visits VALUES (2, 8, '2011-03-04', 'rabies shot');
INSERT INTO visits VALUES (3, 8, '2009-06-04', 'neutered');
INSERT INTO visits VALUES (4, 7, '2008-09-04', 'spayed');