欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring data jpa复杂查询

程序员文章站 2022-04-25 16:21:52
...

我们只要实现Specification<User>接口就可以用来复杂的查询

引入类

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.criteria.*;
import javax.transaction.Transactional;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;

一、and

 public List<User> findUserComplex(String viuseacc,String viusepwd) {

        Specification<User> sp = new Specification<User>() {
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();
                //1.1.设置账号为查询的字段
                Path<String> useacc = root.get("useacc");
                //1.2设置密码为查询的字段
                Path<String> usepwd = root.get("usepwd");
                //2.1拼装条件
                Predicate p1 = cb.equal(useacc,viuseacc);
                Predicate p2 = cb.equal(usepwd,viusepwd);
                Predicate p3 =cb.and(p1,p2);
                list.add(p3);
                return p3;
            }
        };
        List<User> user=userDao.findAll(sp);
        System.out.println("user~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"+user);
        return user;
    }

二、or

 public List<User> findUserComplex(String viuseacc,String viusepwd) {
        System.out.println("useacc!!!!!!!!!!!!!"+viuseacc);
        System.out.println("usepwd!!!!!!!!!!!"+viusepwd);

        Specification<User> sp = new Specification<User>() {
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();
                //1.1.设置账号为查询的字段
                Path<String> useacc = root.get("useacc");
                //1.2设置密码为查询的字段
                Path<String> usepwd = root.get("usepwd");
                //2.1拼装条件
                Predicate p1 = cb.equal(useacc,viuseacc);
                Predicate p2 = cb.equal(usepwd,viusepwd);
                Predicate p3 =cb.or(p1,p2);
                list.add(p3);
                return p3;
            }
        };
        List<User> user=userDao.findAll(sp);
        System.out.println("user~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"+user);
        return user;
    }

 

三、like

 public List<User> findUserComplex(String viuseacc,String viusepwd) {
        System.out.println("useacc!!!!!!!!!!!!!"+viuseacc);
        System.out.println("usepwd!!!!!!!!!!!"+viusepwd);

        Specification<User> sp = new Specification<User>() {
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();
                //1.1.设置账号为查询的字段
                Path<String> useacc = root.get("useacc");
                //1.2设置密码为查询的字段
                Path<String> usepwd = root.get("usepwd");
                //2.1拼装条件
                Predicate p1 = cb.like(useacc,viuseacc);
                Predicate p2 = cb.equal(usepwd,viusepwd);
                Predicate p3 =cb.or(p1,p2);
                list.add(p3);
                return p3;
            }
        };
        List<User> user=userDao.findAll(sp);
        System.out.println("user~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"+user);
        return user;
    }

 

 

 

 

 

 

 

 

 

相关标签: jpa