Now that you could start up a Spring Web Application, where data persistence will play significant role. Thus, I will present how to integerate JPA(java persitence api) with Spring.
Gradle build file
First of all, you must finish your Gradle build file which is build.gradle.
In order to access database, which is MySQL that I use, we’d better append mysql connector to this project. And spring-boot-starter-data-jpa is spring jpa package. The spring-boot-starter-data-rest would be used later, which would enhance the RESTful data accessing.
Configure JPA
It’s neccessary to connect JPA with database. Of course, JPA would work out even without database. The requirement of data persistence force us to support with a database. As you know, The MySQL database is my choice. And this setting is stored in a file which is application.properties. Now let crate a new file.
1 2 3 4
└── src └── main └── resources └── application.properties
Spring Data JPA focuses on using JPA to store data in a relational database. Its most compelling feature is the ability to create repository implementations automatically, at runtime, from a repository interface. src/main/java/hello/CustomerRepository.java
@SpringBootApplication public class App { private static final Logger log = LoggerFactory.getLogger(App.class);
public static void main(String[] args) { SpringApplication.run(App.class, args); }
@Bean public CommandLineRunner demo(CustomerRepository repository) { return (args) -> { // save a couple of customers repository.save(new Customer("Jack", "Bauer")); repository.save(new Customer("Chloe", "O'Brian")); repository.save(new Customer("Kim", "Bauer")); repository.save(new Customer("David", "Palmer")); repository.save(new Customer("Michelle", "Dessler"));
// fetch all customers log.info("Customers found with findAll():"); log.info("-------------------------------"); for (Customer customer : repository.findAll()) { log.info(customer.toString()); } log.info("");
// fetch an individual customer by ID Customer customer = repository.findOne(1L); log.info("Customer found with findOne(1L):"); log.info("--------------------------------"); log.info(customer.toString()); log.info("");
// fetch customers by last name log.info("Customer found with findByLastName('Bauer'):"); log.info("--------------------------------------------"); for (Customer bauer : repository.findByLastName("Bauer")) { log.info(bauer.toString()); } log.info(""); }; } }
Build and run the project!
Database Result
You should see something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13
== Customers found with findAll(): Customer[id=1, firstName='Jack', lastName='Bauer'] Customer[id=2, firstName='Chloe', lastName='O'Brian'] Customer[id=3, firstName='Kim', lastName='Bauer'] Customer[id=4, firstName='David', lastName='Palmer'] Customer[id=5, firstName='Michelle', lastName='Dessler']
== Customer found with findOne(1L): Customer[id=1, firstName='Jack', lastName='Bauer']
== Customer found with findByLastName('Bauer'): Customer[id=1, firstName='Jack', lastName='Bauer'] Customer[id=3, firstName='Kim', lastName='Bauer']
As you could see, a new table whose name is customer has been created. And data has been stored successfully.