JPA of Spring Data Series (2)

Let's take a look at the interface it provides us with complex queries.

JpaSpecificationExecutor.class
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 is mainly built around Specification Yes, 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, CriteriaQuery<?> query, CriteriaBuilder cb); 


There is only one method toPredicate, and the parameters are not unfamiliar to you, they are all in the JPA specification, conditional expressions in ROOT query, CriteriaQuery Conditional query designer, CriteriaBuilder conditional query constructor, and when we use complex object query, we can implement this method and 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(" 
 
    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.


Guess you like

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