diff --git a/persistence-modules/pom.xml b/persistence-modules/pom.xml index 31b9aaaf6d..78f1afd39a 100644 --- a/persistence-modules/pom.xml +++ b/persistence-modules/pom.xml @@ -59,12 +59,16 @@ spring-data-elasticsearch spring-data-gemfire spring-data-geode + spring-data-jpa-annotations spring-data-jpa-crud spring-data-jpa-enterprise spring-data-jpa-filtering spring-data-jpa-query spring-data-jpa-repo + + spring-data-jdbc + spring-data-keyvalue spring-data-mongodb spring-data-neo4j diff --git a/persistence-modules/spring-data-jdbc/pom.xml b/persistence-modules/spring-data-jdbc/pom.xml new file mode 100644 index 0000000000..ff034104d7 --- /dev/null +++ b/persistence-modules/spring-data-jdbc/pom.xml @@ -0,0 +1,29 @@ + + 4.0.0 + + com.baeldung + parent-boot-2 + 0.0.1-SNAPSHOT + ../../parent-boot-2 + + spring-data-jdbc + spring-data-jdbc + + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-data-jdbc + + + com.h2database + h2 + runtime + + + diff --git a/persistence-modules/spring-data-jdbc/src/main/java/com/baeldung/springdatajdbcintro/Application.java b/persistence-modules/spring-data-jdbc/src/main/java/com/baeldung/springdatajdbcintro/Application.java new file mode 100644 index 0000000000..7f50aa87f1 --- /dev/null +++ b/persistence-modules/spring-data-jdbc/src/main/java/com/baeldung/springdatajdbcintro/Application.java @@ -0,0 +1,59 @@ +package com.baeldung.springdatajdbcintro; + +import java.util.Optional; + +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +import com.baeldung.springdatajdbcintro.entity.Person; +import com.baeldung.springdatajdbcintro.repository.PersonRepository; + +import ch.qos.logback.classic.Logger; + +@SpringBootApplication +public class Application implements CommandLineRunner { + + private static final Logger LOGGER = (Logger) LoggerFactory.getLogger(Application.class); + + @Autowired + private PersonRepository repository; + @Autowired + private DatabaseSeeder dbSeeder; + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + + @Override + public void run(String... arg0) throws Exception { + + LOGGER.info("@@ Inserting Data...."); + dbSeeder.insertData(); + LOGGER.info("@@ findAll() call..."); + repository.findAll() + .forEach(person -> LOGGER.info(person.toString())); + LOGGER.info("@@ findById() call..."); + Optional optionalPerson = repository.findById(1L); + optionalPerson.ifPresent(person -> LOGGER.info(person.toString())); + LOGGER.info("@@ save() call..."); + Person newPerson = new Person("Franz", "Kafka"); + Person result = repository.save(newPerson); + LOGGER.info(result.toString()); + LOGGER.info("@@ delete"); + optionalPerson.ifPresent(person -> repository.delete(person)); + LOGGER.info("@@ findAll() call..."); + repository.findAll() + .forEach(person -> LOGGER.info(person.toString())); + LOGGER.info("@@ findByFirstName() call..."); + repository.findByFirstName("Franz") + .forEach(person -> LOGGER.info(person.toString())); + LOGGER.info("@@ findByFirstName() call..."); + repository.updateByFirstName(2L, "Date Inferno"); + repository.findAll() + .forEach(person -> LOGGER.info(person.toString())); + + } +} diff --git a/persistence-modules/spring-data-jdbc/src/main/java/com/baeldung/springdatajdbcintro/DatabaseSeeder.java b/persistence-modules/spring-data-jdbc/src/main/java/com/baeldung/springdatajdbcintro/DatabaseSeeder.java new file mode 100644 index 0000000000..97055fd7ac --- /dev/null +++ b/persistence-modules/spring-data-jdbc/src/main/java/com/baeldung/springdatajdbcintro/DatabaseSeeder.java @@ -0,0 +1,26 @@ +package com.baeldung.springdatajdbcintro; + +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Component; + +import ch.qos.logback.classic.Logger; + +@Component +public class DatabaseSeeder { + + private static final Logger LOGGER = (Logger) LoggerFactory.getLogger(DatabaseSeeder.class); + + @Autowired + private JdbcTemplate jdbcTemplate; + + public void insertData() { + LOGGER.info("> Inserting data..."); + jdbcTemplate.execute("INSERT INTO Person(first_name,last_name) VALUES('Victor', 'Hygo')"); + jdbcTemplate.execute("INSERT INTO Person(first_name,last_name) VALUES('Dante', 'Alighieri')"); + jdbcTemplate.execute("INSERT INTO Person(first_name,last_name) VALUES('Stefan', 'Zweig')"); + jdbcTemplate.execute("INSERT INTO Person(first_name,last_name) VALUES('Oscar', 'Wilde')"); + } + +} diff --git a/persistence-modules/spring-data-jdbc/src/main/java/com/baeldung/springdatajdbcintro/entity/Person.java b/persistence-modules/spring-data-jdbc/src/main/java/com/baeldung/springdatajdbcintro/entity/Person.java new file mode 100644 index 0000000000..7c4c1eec73 --- /dev/null +++ b/persistence-modules/spring-data-jdbc/src/main/java/com/baeldung/springdatajdbcintro/entity/Person.java @@ -0,0 +1,57 @@ +package com.baeldung.springdatajdbcintro.entity; + +import org.springframework.data.annotation.Id; + +public class Person { + + @Id + private long id; + private String firstName; + private String lastName; + + public Person() { + super(); + } + + public Person(long id, String firstName, String lastName) { + super(); + this.id = id; + this.firstName = firstName; + this.lastName = lastName; + } + + public Person(String firstName, String lastName) { + this.firstName = firstName; + this.lastName = lastName; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + @Override + public String toString() { + return "Person [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]"; + } + +} diff --git a/persistence-modules/spring-data-jdbc/src/main/java/com/baeldung/springdatajdbcintro/repository/PersonRepository.java b/persistence-modules/spring-data-jdbc/src/main/java/com/baeldung/springdatajdbcintro/repository/PersonRepository.java new file mode 100644 index 0000000000..2f2329caec --- /dev/null +++ b/persistence-modules/spring-data-jdbc/src/main/java/com/baeldung/springdatajdbcintro/repository/PersonRepository.java @@ -0,0 +1,23 @@ +package com.baeldung.springdatajdbcintro.repository; + +import java.util.List; + +import org.springframework.data.jdbc.repository.query.Modifying; +import org.springframework.data.jdbc.repository.query.Query; +import org.springframework.data.repository.CrudRepository; +import org.springframework.data.repository.query.Param; +import org.springframework.stereotype.Repository; + +import com.baeldung.springdatajdbcintro.entity.Person; + +@Repository +public interface PersonRepository extends CrudRepository { + + @Query("select * from person where first_name=:firstName") + List findByFirstName(@Param("firstName") String firstName); + + @Modifying + @Query("UPDATE person SET first_name = :name WHERE id = :id") + boolean updateByFirstName(@Param("id") Long id, @Param("name") String name); + +} diff --git a/persistence-modules/spring-data-jdbc/src/main/resources/application.properties b/persistence-modules/spring-data-jdbc/src/main/resources/application.properties new file mode 100644 index 0000000000..45099222fc --- /dev/null +++ b/persistence-modules/spring-data-jdbc/src/main/resources/application.properties @@ -0,0 +1,8 @@ +#H2 DB +spring.jpa.hibernate.ddl-auto=none +spring.h2.console.enabled=true +spring.datasource.url=jdbc:h2:mem:persondb +spring.datasource.driverClassName=org.h2.Driver +spring.datasource.username=sa +spring.datasource.password=test +spring.jpa.database-platform=org.hibernate.dialect.H2Dialect \ No newline at end of file diff --git a/persistence-modules/spring-data-jdbc/src/main/resources/schema.sql b/persistence-modules/spring-data-jdbc/src/main/resources/schema.sql new file mode 100644 index 0000000000..5618822bf9 --- /dev/null +++ b/persistence-modules/spring-data-jdbc/src/main/resources/schema.sql @@ -0,0 +1,5 @@ +create table person ( + id integer identity primary key, + first_name varchar(30), + last_name varchar(30) +); \ No newline at end of file diff --git a/pom.xml b/pom.xml index b110cb0fe0..a69ffa2798 100644 --- a/pom.xml +++ b/pom.xml @@ -1407,4 +1407,4 @@ 1.4.197 - + \ No newline at end of file