BAEL-4230 - Adding Introduction to Spring Data JDBC Project (#9665)
This commit is contained in:
+59
@@ -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()));
|
||||
|
||||
}
|
||||
}
|
||||
+26
@@ -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')");
|
||||
}
|
||||
|
||||
}
|
||||
+57
@@ -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 + "]";
|
||||
}
|
||||
|
||||
}
|
||||
+23
@@ -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);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user