[spring] Let's use Spring Data JPA

I've touched java, When I look at the source code that includes the framework \ (^ O ^) / <"What's this?" And I often stopped, so I just wrote a memo.

What is Spring Data JPA?

What is equivalent to ** DAO ** (Data Access Object) in Java In Spring, it's called the ** Repository class **.

What makes the ** Repository class ** convenient (easy) to use It's a library called ** Spring Data JPA **.

Spring Data JPA usage example

Immediately, the code below is for "getting the employee name".

EmployeeRepository.java


@Service("employeeService")
public class EmployeeServiceImpl implements EmployeeService{
    @Autowired
    EmployeeRepository employeeRepository;

    public Employee getName(String name) {
        return Employee employee = employeeRepository.findByName(name);
    }

}

And it is possible to get the employee name from the database with the following simple code. For me, who knew only PureJava, I thought, "What? That's it?"

EmployeeRepository.java


@Repository("EmployeeRepository")
public interface EmployeeRepository extends JpaRepository<Employee, String>{

public Employee findByName(String Name);

}

In the above ** Repository class **, the following steps are performed. "Get the exact match from the Employee table that matches the string specified in the field named name."

Summary of usage

To summarize briefly, -Capitalize the first field name you want to search -Define methods for specific formats such as findBy ~ Only these two points are done without leaking, and JPA, which is a library, automatically implements the processing after ! </ font> I want to try database access for the time being! Those who say need verification.

Afterword

Originally, you should know how to write it if you look at the document. It is a document with high hurdles for those who are just starting to learn, with words that have never been seen in a specialized way. ** ** I felt that only those who were familiar with the framework could read it. It will take several months to read with accurate conviction, as you will have to look up the meaning of the word.

Recommended Posts

[spring] Let's use Spring Data JPA
Until the use of Spring Data and JPA Part 2
Until the use of Spring Data and JPA Part 1
How to use Spring Data JDBC
[How to install Spring Data Jpa]
Spring Data JPA SQL log output
OR search with Spring Data Jpa Specification
Exists using Specification in Spring Data JPA
Let's use jcmd
Spring Data JPA save select-insert is only insert
Spring Data JPA Entity cross-reference and its notes
Sort by Spring Data JPA (with compound key sort)
Creating a common repository with Spring Data JPA
Spring Boot + Spring Data JPA About multiple table joins
Use Interceptor in Spring
Spring Data JDBC Preview
[Swift] Let's use extension
spring data dynamodb trap
A memorandum when trying Spring Data JPA with STS
I tried to get started with Spring Data JPA
Make the where clause variable in Spring Data JPA
Let's try Zoomdata (Big Data: 3)
Spring Boot Introductory Guide I tried [Accessing Data with JPA]
[Swift] Let's use Segmented Control
[RSpec] Let's use FactoryBot [Rails]
Let's use Swift Firebase Firebase Auth
Let's try Zoomdata (Big Data: 4)
Let's try Zoomdata (Big Data: 1)
Spring Boot + Springfox springfox-boot-starter 3.0.0 Use
Use Spring JDBC with Spring Boot
Let's try Zoomdata (Big Data: 2)
Spring Data JPA: Write a query in Pure SQL in @Query of Repository
Dynamically generate queries with Spring Data JPA (multi-word search) (paging support)
See the behavior of entity update with Spring Boot + Spring Data JPA
Creating REST APIs with Spring JPA Data with REST and Lombok incredibly easy.
How to use Lombok in Spring
Use Basic Authentication with Spring Boot
Let's use Set more [Collections Framework]
Let's use Twilio in Java! (Introduction)
Let's use Amazon Textract with Ruby
Let's use reflection Private method access
Let's use it after understanding sudo!
Use DBUnit for Spring Boot test
How to use ModelMapper (Spring boot)
Beginning with Spring Boot 0. Use Spring CLI
Let's thoroughly explain Spring Cloud Gateway
Implementation method for multi-data source with Spring boot (Mybatis and Spring Data JPA)