Java JPA-Specification

The Specification of the JPA framework is part of the Java Persistence API, which provides a type-safe query construction method that can dynamically construct complex query conditions at runtime.

First of all, to use Specification, you need to add corresponding dependencies. For example, using Maven, pom.xmlthe following dependencies can be added to the file:

<dependency>
    <groupId>javax.persistence</groupId>
    <artifactId>javax.persistence-api</artifactId>
    <version>2.2</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

Next, we can create a Repository interface that inherits from JpaRepositoryand can use JpaSpecificationExecutorthe interface to support Specification queries. For example:

import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

public interface UserRepository extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> {
    
    
}

In the Repository interface, we can define some custom query methods, such as querying the user list according to conditions. Using Specification, we can dynamically build query conditions. For example:

import org.springframework.data.jpa.domain.Specification;
import javax.persistence.criteria.Predicate;

public class UserSpecifications {
    
    
    public static Specification<User> findByUsernameAndEmail(String username, String email) {
    
    
        return (root, query, criteriaBuilder) -> {
    
    
            Predicate predicate = criteriaBuilder.conjunction();
            if (username != null) {
    
    
                predicate = criteriaBuilder.and(predicate, criteriaBuilder.equal(root.get("username"), username));
            }
            if (email != null) {
    
    
                predicate = criteriaBuilder.and(predicate, criteriaBuilder.equal(root.get("email"), email));
            }
            return predicate;
        };
    }
}

In the above code, findByUsernameAndEmailthe method returns an Specificationobject, and in the method Specificationof the interface toPredicate, we can use to criteriaBuilderbuild query conditions. Use root.get()to obtain entity attributes, and then use criteriaBuilderto build various conditions, such as equal, likeetc.

Finally, in the Service layer or Controller layer, we can use UserRepositorythe findAllmethod to execute the query. For example:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class UserService {
    
    
    private final UserRepository userRepository;

    @Autowired
    public UserService(UserRepository userRepository) {
    
    
        this.userRepository = userRepository;
    }

    public List<User> getUsersByUsernameAndEmail(String username, String email) {
    
    
        Specification<User> spec = UserSpecifications.findByUsernameAndEmail(username, email);
        return userRepository.findAll(spec);
    }
}

In the above code, we UserSpecifications.findByUsernameAndEmailget an Specificationobject by calling a method, and then pass it to userRepository.findAllthe method for query.

This is the basic usage of the Specification of the JPA framework. By dynamically constructing query conditions, complex query operations can be performed conveniently.

Guess you like

Origin blog.csdn.net/qq_41177135/article/details/131716125