BAEL-4230 - Adding Introduction to Spring Data JDBC Project (#9665)

This commit is contained in:
Rando Shtishi 2020-08-08 22:21:57 +02:00 committed by GitHub
parent 8da19e2b88
commit 15e94bb577
9 changed files with 212 additions and 1 deletions

View File

@ -59,12 +59,16 @@
<module>spring-data-elasticsearch</module>
<module>spring-data-gemfire</module>
<module>spring-data-geode</module>
<module>spring-data-jpa-annotations</module>
<module>spring-data-jpa-crud</module>
<module>spring-data-jpa-enterprise</module>
<module>spring-data-jpa-filtering</module>
<module>spring-data-jpa-query</module>
<module>spring-data-jpa-repo</module>
<module>spring-data-jdbc</module>
<module>spring-data-keyvalue</module>
<module>spring-data-mongodb</module>
<module>spring-data-neo4j</module>

View File

@ -0,0 +1,29 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-boot-2</relativePath>
</parent>
<artifactId>spring-data-jdbc</artifactId>
<name>spring-data-jdbc</name>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>

View File

@ -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<Person> 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()));
}
}

View File

@ -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')");
}
}

View File

@ -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 + "]";
}
}

View File

@ -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<Person,Long> {
@Query("select * from person where first_name=:firstName")
List<Person> 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);
}

View File

@ -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

View File

@ -0,0 +1,5 @@
create table person (
id integer identity primary key,
first_name varchar(30),
last_name varchar(30)
);

View File

@ -1407,4 +1407,4 @@
<h2.version>1.4.197</h2.version>
</properties>
</project>
</project>