[Java] [spring] Let’s use Spring Data JPA

1 minute read

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 **.

  • I will not touch on why it is called so in this article.

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 !
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.