From 581286816c4f27bfcb12be8d10204c024a53960b Mon Sep 17 00:00:00 2001 From: Charlotte Beale Date: Wed, 1 Jan 2020 11:17:33 -0500 Subject: [PATCH] complete Part 1 + --- .../persistenceapp/DataSourceConfig.java | 36 ++++++++++ .../io/zipcoder/persistenceapp/Person.java | 72 +++++++++++++++++++ .../persistenceapp/PersonService.java | 8 +++ src/main/resources/application-h2.properties | 13 +++- src/main/resources/application.properties | 11 ++- src/main/resources/data-h2.sql | 13 ++++ src/main/resources/script.sql | 24 +++++++ 7 files changed, 174 insertions(+), 3 deletions(-) create mode 100644 src/main/java/io/zipcoder/persistenceapp/DataSourceConfig.java create mode 100644 src/main/java/io/zipcoder/persistenceapp/Person.java create mode 100644 src/main/java/io/zipcoder/persistenceapp/PersonService.java create mode 100644 src/main/resources/data-h2.sql create mode 100644 src/main/resources/script.sql diff --git a/src/main/java/io/zipcoder/persistenceapp/DataSourceConfig.java b/src/main/java/io/zipcoder/persistenceapp/DataSourceConfig.java new file mode 100644 index 0000000..5702da9 --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/DataSourceConfig.java @@ -0,0 +1,36 @@ +package io.zipcoder.persistenceapp; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.datasource.DriverManagerDataSource; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; + +import javax.sql.DataSource; + +@Configuration +@ComponentScan +public class DataSourceConfig { + + @Bean + public JdbcTemplate jdbcTemplate(){ + return new JdbcTemplate(dataSource()); + } + + @Bean(name = "dataSource") + public DataSource dataSource(){ + EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder(); + builder.setName("testdb"); + EmbeddedDatabase build = builder + .setName("testdb;MODE=MySQL;DB_CLOSE_ON_EXIT=false") + .setType(EmbeddedDatabaseType.H2) + .addScript("classpath:schema-h2.sql") + .addScript("classpath:data-h2.sql") + .build(); + return build; + + } +} diff --git a/src/main/java/io/zipcoder/persistenceapp/Person.java b/src/main/java/io/zipcoder/persistenceapp/Person.java new file mode 100644 index 0000000..151163b --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/Person.java @@ -0,0 +1,72 @@ +package io.zipcoder.persistenceapp; + +import java.util.Date; + +public class Person { + private Integer id; + private String first_name; + private String last_name; + private String mobile; + private Date birthday; + private Integer home_id; + + public Person (){} + + public Person(Integer id, String first_name, String last_name, + String mobile, Date birthday, Integer home_id) { + this.id = id; + this.first_name = first_name; + this.last_name = last_name; + this.mobile = mobile; + this.birthday = birthday; + this.home_id = home_id; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getFirst_name() { + return first_name; + } + + public void setFirst_name(String first_name) { + this.first_name = first_name; + } + + public String getLast_name() { + return last_name; + } + + public void setLast_name(String last_name) { + this.last_name = last_name; + } + + public String getMobile() { + return mobile; + } + + public void setMobile(String mobile) { + this.mobile = mobile; + } + + public Date getBirthday() { + return birthday; + } + + public void setBirthday(Date birthday) { + this.birthday = birthday; + } + + public Integer getHome_id() { + return home_id; + } + + public void setHome_id(Integer home_id) { + this.home_id = home_id; + } +} diff --git a/src/main/java/io/zipcoder/persistenceapp/PersonService.java b/src/main/java/io/zipcoder/persistenceapp/PersonService.java new file mode 100644 index 0000000..477a502 --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/PersonService.java @@ -0,0 +1,8 @@ +package io.zipcoder.persistenceapp; + +import org.springframework.stereotype.Service; + +@Service +public class PersonService { + +} diff --git a/src/main/resources/application-h2.properties b/src/main/resources/application-h2.properties index 74765cc..946488f 100644 --- a/src/main/resources/application-h2.properties +++ b/src/main/resources/application-h2.properties @@ -1,4 +1,13 @@ -spring.datasource.url=jdbc:h2:mem:testdb;Mode=Oracle +spring.datasource.url=jdbc:h2:mem:testdb +spring.datasource.driver-class-name=org.h2.Driver spring.datasource.platform=h2 +spring.datasource.username=sa +spring.datasource.password=password +spring.jpa.database-platform=org.hibernate.dialect.H2Dialect + spring.jpa.hibernate.ddl-auto=none -spring.datasource.continue-on-error=true \ No newline at end of file + + +spring.h2.console.path=/h2-console +spring.h2.console.settings.trace=false +spring.h2.console.settings.web-allow-others=false diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 7d4dc6f..797a6bf 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,3 +1,12 @@ spring.profiles.active=h2 logging.level.org.springframework.boot.context.embedded=INFO -spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect \ No newline at end of file +spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect + +spring.datasource.tomcat.test-on-borrow=true +spring.datasource.tomcat.validation-query=SELECT 1 +spring.datasource.tomcat.validation-interval=0 + + +#org.postgresql.Driver=select version(); + +#spring.datasource.url = jdbc:h2:file:/src/main/resources/schema-h2.sql diff --git a/src/main/resources/data-h2.sql b/src/main/resources/data-h2.sql new file mode 100644 index 0000000..e66321a --- /dev/null +++ b/src/main/resources/data-h2.sql @@ -0,0 +1,13 @@ +INSERT INTO HOME (ADDRESS, HOMENUMBER) VALUES ('36 E. Bayberry Rd.Savannah, GA 31404', '565-6895'); +INSERT INTO HOME (ADDRESS, HOMENUMBER) VALUES ('11 Essex Dr.Farmingdale, NY 11735', '454-4544'); +INSERT INTO HOME (ADDRESS, HOMENUMBER) VALUES ('920 Arlington Street Clifton, NJ 07011', '985-4515'); +INSERT INTO HOME (ADDRESS, HOMENUMBER) VALUES ('234 High Street, PA 19159 ', '267-3940'); + + +INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID ) VALUES ('Carbral', 'Sheeri', '230-4233', '1970-02-23', 2); +INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID) VALUES ( 'Sharam', 'Raj', '186-5223', '1980-08-31', 3); +INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID)VALUES ('Durand', 'Noelle', '395-6161', '1960-07-06', 1); +INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID)VALUES ('Smith', 'Thomas', '395-6181', '1987-07-06', 1); +INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID)VALUES ('Smith', 'Jane', '393-6181', '1987-12-06', 3); +INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID)VALUES ('Brown', 'Doug', '466-6241', '1954-12-07', 3); + diff --git a/src/main/resources/script.sql b/src/main/resources/script.sql new file mode 100644 index 0000000..c0c928a --- /dev/null +++ b/src/main/resources/script.sql @@ -0,0 +1,24 @@ +INSERT INTO movies (id, title, runtime, genre, imdb_score, rating) VALUES (DEFAULT, 'Howard the Duck', '110','Sci-Fi', '4.6', 'PG'); +INSERT INTO movies (id, title, runtime, genre, imdb_score, rating) VALUES (DEFAULT, 'Lavalantula', '83','Horror', '4.7', 'TV-14'); +INSERT INTO movies (id, title, runtime, genre, imdb_score, rating) VALUES (DEFAULT, 'Starship Trooper', '129', 'Sci-Fi', '7.2', 'PG-13'); +INSERT INTO movies (id, title, runtime, genre, imdb_score, rating) VALUES (DEFAULT, 'Waltz With Bashir', '90', 'Documentary', '8.0', 'R'); +INSERT INTO movies (id, title, runtime, genre, imdb_score, rating) VALUES (DEFAULT, 'Spaceballs', '96', 'Comedy', '7.1', 'PG'); +INSERT INTO movies (id, title, runtime, genre, imdb_score, rating) VALUES (DEFAULT, 'Monsters Inc.', '92', 'Animation', '8.1', 'G'); + +SELECT * FROM movies WHERE genre='Sci-Fi'; + +SELECT * FROM movies WHERE imdb_score > 6.5; + +SELECT * FROM movies WHERE rating='G' OR rating='PG' AND runtime < 100; + +SELECT genre, AVG(runtime) FROM movies WHERE imdb_score <= 7 GROUP BY genre; + +UPDATE movies SET rating='R' WHERE title='Starship Troopers'; + +SELECT id, rating FROM movies WHERE genre='Horror' OR genre='Documentary'; + +SELECT AVG(imdb_score), MAX(imdb_score), MIN(imdb_score) FROM movies GROUP BY rating; + +SELECT AVG(imdb_score), MAX(imdb_score), MIN(imdb_score) FROM movies GROUP BY rating HAVING COUNT(*) > 1; + +DELETE FROM movies WHERE rating='R';