JpaSpecificationExecutor.class

Java code Collection code

    public interface JpaSpecificationExecutor<T> { 
     
        T findOne(Specification<T> spec); 
     
        List<T> findAll(Specification<T> spec); 
     
        Page<T> findAll(Specification<T> spec, Pageable pageable); 
     
        List<T> findAll(Specification<T> spec, Sort sort); 
     
        long count(Specification<T> spec); 
    } 



The most frequent class in this interface is Specification.class, and this class mainly revolves around Specification Created, Specification.class is a query specification provided by Spring Data JPA, and you only need to set your query conditions around this specification. Let's take a look at what is in the Specification.class interface.

Specification.class
Java code Collection code

    public interface Specification<T> { 
     
        Predicate toPredicate(Root<T> root, 
    } 


There is only one method toPredicate, and the parameters in it are not unfamiliar to you. They are all in the JPA specification, the conditional expression in the ROOT query, the CriteriaQuery conditional query designer, and the CriteriaBuilder conditional query constructor, and when we use complex object queries , to implement this method, use JPA to construct object query.

Let's take a look at a small example:
Java code Collection code

    @Repository("userDao") 
    public interface IUserDao extends JpaSpecificationExecutor<User>{ 
    } 


is still just an empty interface, this time JpaSpecificationExecutor is inherited.
Write another test case: Query the records whose name contains Sam in the user table, and paginate the
Java code in reverse order by birth. Collection code

    public class UserDaoTest { 
     
        private static ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); 
     
        private static IUserDao userDao = (IUserDao) context.getBean("userDao"); 
     
        public void findBySpecAndPaginate() { 
            Page<User> page = userDao.findAll(new Specification<User>() { 
                @Override 
                public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) { 
                    root = query.from(User.class); 
                    Path<String> nameExp = root.get("name"); 
                    return cb.like(nameExp, "%Sam%"); 
                } 
     
            }, new PageRequest(1, 5, new Sort(Direction.DESC, new String[] { "birth" }))); 
     
            StringBuilder stout = new StringBuilder(" 以下是姓名包含Sam人员信息 : ").append("\n"); 
            stout.append("| 序号 | username | password | name | sex | birth |").append("\n"); 
            int sortIndex = 1; 
            for (User u : page.getContent()) { 
                stout.append(" | ").append(sortIndex); 
                stout.append(" | ").append(u.getUsername()); 
                stout.append(" | ").append(u.getPassword()); 
                stout.append(" | ").append(u.getName()); 
                stout.append(" | ").append(u.getSex()); 
                stout.append(" | ").append(u.getBirth()); 
                stout.append(" | \n"); 
                sortIndex++; 
            } 
            System.err.println(stout); 
        } 
     
        public static void main(String[] args) { 
            UserDaoTest test = new UserDaoTest(); 
            test.findBySpecAndPaginate(); 
        } 
    } 



Of ​​course, this is just a test, a simple conditional query method. You can also design complex queries to get the results you need, I just wrote a very simple method to get you started.

I have written two articles, but I haven't talked about why Spring Data JPA can be used only by defining an interface. In fact, it is not difficult to find out. Looking at the source code, you can find that there is an implementation class for JpaRepository and JpaSpecificationExecutor, SimpleJpaRepository.class, this class implements The two interfaces just mentioned. When Spring injects us with the implementation class, it is this SimpleJpaRepository.class. I will not repeat the specific implementation method in this sense. If you are interested, you can check its source code, which is the same as the traditional JPA implementation. .

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326615270&siteId=291194637