Spring Data JPA Using Spring Boot Application - Dinesh On Java
Spring Data JPA Using Spring Boot Application - Dinesh On Java
Follow
Home Core Java Spring Spring Boot Hibernate Struts 2 J2EE Web Services Hadoop Design Patterns Maven MongoDB
Replay
*T&C apply.
*T&C apply.
Hello friends !!! In this tutorial we are going to discuss how to use and congure Spring Data JPA with Spring Boot
Applica on with minimal congura on as always with Spring Boot. Spring Data JPA and Hibernate (as JPA implementa on)
will be used to implement the data access layer.
Dinesh
20,502 like
Like Page
Related Tutorials
Spring Data?
Spring Data is a high level project developed by Spring community aimed at simplifying the data access opera ons for the
applica ons. If you are using Spring Data in your project, you are not going to write most of the low level data access
opera ons like wri ng SQL query DAO classes etc. Spring Data will generate everything dynamically at run- me by crea ng
the proxy instances of your abstract repositories and perform the required opera ons.
There are several sub projects provided by Spring Data like Spring Data JPA, Spring Data Solr, Spring Data MongoDB, Spring
Data REST etc. Here we are going to discuss one of them Spring Data JPA.
About My Profession
AJAX (4)
HibernateOne of the most popular JPA implementa ons. Ant (11)
Spring Data JPAMakes it easy to implement JPA-based repositories. cloud computing
collection
Spring ORMsCore ORM support from the Spring Framework
Core JAVA
Core Java Interview
http://www.dineshonjava.com/2016/08/spring-data-jpa-using-in-spring-boot-application.html 1/12
7/31/2017 Spring Data JPA using Spring Boot Application | Dinesh on Java
project with selec ng maven build and required dependencies for this project like WEB, SPRING DATA JPA, and MYSQL. Spring Batch3
Spring Boot
Now lets see required part of the maven le for this project.
Spring HATEOAS
Spring Mobile
Spring MVC
Spring MVC interview
1. <?xml version="1.0" encoding="UTF-8"?> Spring REST
http://www.dineshonjava.com/2016/08/spring-data-jpa-using-in-spring-boot-application.html 2/12
7/31/2017 Spring Data JPA using Spring Boot Application | Dinesh on Java
http://www.dineshonjava.com/2016/08/spring-data-jpa-using-in-spring-boot-application.html 3/12
7/31/2017 Spring Data JPA using Spring Boot Application | Dinesh on Java
1. # DataSource settings: set here your own configurations for the database
2. # connection. In this example we have "dojsb" as database name and
3. # "root" as username and password.
4. spring.datasource.url = jdbc:mysql://localhost:3307/dojdb
5. spring.datasource.username = root
6. spring.datasource.password = root
7.
8. # Keep the connection alive if idle for a long time (needed in production)
9. spring.datasource.testWhileIdle = true
10. spring.datasource.validationQuery = SELECT 1
11.
12. # Show or not log for each sql query
13. spring.jpa.show-sql = true
14.
15. # Hibernate ddl auto (create, create-drop, update)
16. spring.jpa.hibernate.ddl-auto = create
17.
18. # Naming strategy
19. spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
20.
21. # Use spring.jpa.properties.* for Hibernate native properties (the prefix is
22. # stripped before adding them to the entity manager)
23.
24. # The SQL dialect makes Hibernate generate better SQL for the chosen database
25. spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
26.
27. server.port = 8181
Using hibernate congura on ddl-auto = update the database schema will be automa cally created (and updated),
crea ng tables and columns, accordingly to java en es found in the project. For this applica on I am using server port is
8181.
En ty le Booking.java
Create an en ty class represen ng a table in your db. Here I am using Booking.java as en ty les which map to the
booking table in the database dojdb.
1. /**
2. *
3. */
4. package com.dineshonjava.sdjpa.models;
5.
6. import java.io.Serializable;
7. import java.util.Date;
8.
9. import javax.persistence.Column;
10. import javax.persistence.Entity;
http://www.dineshonjava.com/2016/08/spring-data-jpa-using-in-spring-boot-application.html 4/12
7/31/2017 Spring Data JPA using Spring Boot Application | Dinesh on Java
http://www.dineshonjava.com/2016/08/spring-data-jpa-using-in-spring-boot-application.html 5/12
7/31/2017 Spring Data JPA using Spring Boot Application | Dinesh on Java
The @En ty annota on marks this class as a JPA en ty. The @Table annota on species the db tables name.
1. package com.dineshonjava.sdjpa.models;
2.
3. import org.springframework.data.repository.CrudRepository;
4. import org.springframework.transaction.annotation.Transactional;
5.
6. @Transactional
7. public interface BookingRepository extends CrudRepository {
8.
9. /**
10. * This method will find an Boooking instance in the database by its departure.
11. * Note that this method is not implemented and its working code will be
12. * automatically generated from its signature by Spring Data JPA.
13. */
14. public Booking findByDeparture(String departure);
15. }
16.
http://www.dineshonjava.com/2016/08/spring-data-jpa-using-in-spring-boot-application.html 6/12
7/31/2017 Spring Data JPA using Spring Boot Application | Dinesh on Java
Here adding one more le for accessing Rest APIs is RestController it provide me some URLs for CRUD opera onFollow
the
booking table.
1. /**
2. *
3. */
4. package com.dineshonjava.sdjpa.controller;
5.
6. import java.util.Date;
7.
8. import org.springframework.beans.factory.annotation.Autowired;
9. import org.springframework.web.bind.annotation.RequestMapping;
10. import org.springframework.web.bind.annotation.RequestParam;
11. import org.springframework.web.bind.annotation.RestController;
12.
13. import com.dineshonjava.sdjpa.models.Booking;
14. import com.dineshonjava.sdjpa.models.BookingRepository;
15.
16. /**
17. * @author Dinesh.Rajput
18. *
19. */
20. @RestController
21. @RequestMapping("/booking")
22. public class BookingController {
23.
24. @Autowired
25. BookingRepository bookingRepository;
26. /**
27. * GET /create --> Create a new booking and save it in the database.
28. */
29. @RequestMapping("/create")
30. public Booking create(Booking booking) {
31. booking.setTravelDate(new Date());
32. booking = bookingRepository.save(booking);
33. return booking;
34. }
35.
36. /**
37. * GET /read --> Read a booking by booking id from the database.
38. */
39. @RequestMapping("/read")
40. public Booking read(@RequestParam Long bookingId) {
41. Booking booking = bookingRepository.findOne(bookingId);
42. return booking;
43. }
44.
45. /**
46. * GET /update --> Update a booking record and save it in the database.
http://www.dineshonjava.com/2016/08/spring-data-jpa-using-in-spring-boot-application.html 7/12
7/31/2017 Spring Data JPA using Spring Boot Application | Dinesh on Java
47. */ Follow
48. @RequestMapping("/update")
49. public Booking update(@RequestParam Long bookingId, @RequestParam String psngrName) {
50. Booking booking = bookingRepository.findOne(bookingId);
51. booking.setPsngrName(psngrName);
52. booking = bookingRepository.save(booking);
53. return booking;
54. }
55.
56. /**
57. * GET /delete --> Delete a booking from the database.
58. */
59. @RequestMapping("/delete")
60. public String delete(@RequestParam Long bookingId) {
61. bookingRepository.delete(bookingId);
62. return "booking #"+bookingId+" deleted successfully";
63. }
64. }
Thats all! The connec on with the database is done when we run the following le as Java Applica on or Spring Boot
applica on.
1. import org.springframework.boot.SpringApplication;
2. import org.springframework.boot.autoconfigure.SpringBootApplication;
3.
4. @SpringBootApplication
5. public class SpringBootJpaSpringDataApplication {
6.
7. public static void main(String[] args) {
8. SpringApplication.run(SpringBootJpaSpringDataApplication.class, args);
9. }
10. }
Project Structure:
http://www.dineshonjava.com/2016/08/spring-data-jpa-using-in-spring-boot-application.html 8/12
7/31/2017 Spring Data JPA using Spring Boot Application | Dinesh on Java
Follow
Test the controller launching the Spring Boot web applica on and using these urls:
Reading a Record:
/booking/read?bookingId=[bookingId]: Read the booking with the passed bookingId.
For Example:
h p://localhost:8181/booking/read?bookingId=1
{
bookingId: 1,
psngrName: "Dinesh",
departure: "Noida",
des na on: "Pune",
travelDate: 1470828563000
}
Upda ng a Record:
/booking/update?bookingId=[bookingId]&psngrName=Sweety: Update the booking for a given booking id.
For Example:
h p://localhost:8181/booking/update?bookingId=5&psngrName=Suresh
{
bookingId: 5,
psngrName: "Suresh",
departure: "Agra",
des na on: "Noida",
travelDate: 1470828994000
}
Dele ng a Record:
http://www.dineshonjava.com/2016/08/spring-data-jpa-using-in-spring-boot-application.html 9/12
7/31/2017 Spring Data JPA using Spring Boot Application | Dinesh on Java
For Example:
h p://localhost:8181/booking/delete?bookingId=5
booking #5 deleted successfully
h ps://github.com/DOJ-So wareConsultant/SpringBootJPASpringData
Summary
In this chapter I have explained how to use Spring Data JPA and Spring Boot frameworks. Also explained in short about the
Spring Data Repositories and query methods.
http://www.dineshonjava.com/2016/08/spring-data-jpa-using-in-spring-boot-application.html 10/12
7/31/2017 Spring Data JPA using Spring Boot Application | Dinesh on Java
5 Comments dineshonjava
1 Login
Follow
Sort by Best
Recommend 4 Share
LOG IN WITH
Name
import org.springframework.data.repository.CrudRepository;
import javax.transaction.Transactional;
@Transactional
public interface BookingRepository extends CrudRepository<booking, long=""> {
Thanks.
Reply Share
Yes, It is good.
Thanks
Reply Share
ALSO ON DINESHONJAVA
Method injection with Spring using Lookup method Spring Boot Actuator A Complete Guide
property 5 comments a year ago
2 comments 6 months ago Dinesh Rajput Thanks Mohan for your view.
Dinesh Rajput Thanks Naved.
RequestMapping Annotation in Spring MVC | DOJ How to find all Pairs in Array of Integers whose Sum is
Software Consultant equal to a given Number
2 comments a year ago 2 comments 7 months ago
Dinesh Rajput Thanks for your feedback. Dinesh Rajput Could you please suggest best solution if you
have!
Newer Post Home Older Post
http://www.dineshonjava.com/2016/08/spring-data-jpa-using-in-spring-boot-application.html 11/12
7/31/2017 Spring Data JPA using Spring Boot Application | Dinesh on Java
POPULAR POSTS
Follow
http://www.dineshonjava.com/2016/08/spring-data-jpa-using-in-spring-boot-application.html 12/12