Spring Boot Tutorial
Spring Boot - Software Setup and Configuration (STS/Eclipse/IntelliJ)
Prerequisite (Spring Core Concepts)
Spring Boot Core
Spring Boot with REST API
Spring Boot with Database and Data JPA
Spring Boot with Kafka
Spring Boot with AOP
Below is a step-by-step guide to create a Spring Boot JPA sample Maven project with query methods:
Start by adding necessary dependencies to your pom.xml
:
<dependencies> <!-- Spring Boot Starter Web for RESTful APIs --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Boot Starter Data JPA for JPA support --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- H2 database for demonstration purposes --> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> </dependencies>
Assume a simple Book
entity:
package com.example.demo.entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class Book { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String title; private String author; // Getters, setters, constructors, etc. }
Create a repository with query methods:
package com.example.demo.repository; import com.example.demo.entity.Book; import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; public interface BookRepository extends JpaRepository<Book, Long> { List<Book> findByTitle(String title); List<Book> findByAuthor(String author); // Add more custom query methods as needed }
Note: The methods findByTitle
and findByAuthor
are examples of query methods that will be auto-implemented by Spring Data JPA without requiring actual SQL or JPQL.
Use the repository in a service:
package com.example.demo.service; import com.example.demo.entity.Book; import com.example.demo.repository.BookRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class BookService { @Autowired private BookRepository bookRepository; public List<Book> findBooksByTitle(String title) { return bookRepository.findByTitle(title); } public List<Book> findBooksByAuthor(String author) { return bookRepository.findByAuthor(author); } // ... other methods as required }
Create a REST controller:
package com.example.demo.controller; import com.example.demo.entity.Book; import com.example.demo.service.BookService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class BookController { @Autowired private BookService bookService; @GetMapping("/books/title") public List<Book> getBooksByTitle(@RequestParam String title) { return bookService.findBooksByTitle(title); } @GetMapping("/books/author") public List<Book> getBooksByAuthor(@RequestParam String author) { return bookService.findBooksByAuthor(author); } }
In application.properties
:
# H2 configurations spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.driver-class-name=org.h2.Driver spring.datasource.username=sa spring.datasource.password= spring.h2.console.enabled=true # Hibernate configurations spring.jpa.hibernate.ddl-auto=update
Ensure you have a Spring Boot application runner class, typically with a @SpringBootApplication
annotation.
Now, run the application and you can access endpoints like /books/title?title=SomeTitle
and /books/author?author=SomeAuthor
to filter books by title or author, respectively.
With this structure, you have a fully functioning Spring Boot JPA application with custom query methods, and you can easily extend it with additional entities, repositories, and services as required.
Building a simple Spring Boot JPA Maven project:
// Main Application Class @SpringBootApplication public class SpringBootJpaApplication { public static void main(String[] args) { SpringApplication.run(SpringBootJpaApplication.class, args); } }
Query methods in Spring Boot JPA with Maven project:
// UserRepository Interface public interface UserRepository extends JpaRepository<User, Long> { List<User> findByFirstName(String firstName); Optional<User> findByIdAndLastName(Long id, String lastName); }
CRUD operations and query methods in Spring Boot JPA:
// User Entity @Entity public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String firstName; private String lastName; // Getters and setters }
Using JpaRepository and query annotations in Spring Boot Maven project:
// UserRepository Interface public interface UserRepository extends JpaRepository<User, Long> { @Query("SELECT u FROM User u WHERE u.age > :age") List<User> findByAgeGreaterThan(@Param("age") int age); }
Custom query methods with Spring Boot JPA and Maven:
// UserRepository Interface public interface UserRepository extends JpaRepository<User, Long> { List<User> findCustomQuery(String param); }
Dynamic queries with QueryDSL in Spring Boot JPA Maven project:
// UserRepository Interface public interface UserRepository extends JpaRepository<User, Long>, QuerydslPredicateExecutor<User> { }
Testing query methods in Spring Boot JPA Maven project:
// UserRepositoryTest Class @RunWith(SpringRunner.class) @DataJpaTest public class UserRepositoryTest { @Autowired private TestEntityManager entityManager; @Autowired private UserRepository userRepository; // Test methods }