spring data jpa复杂查询

1.传入根据对象的属性和值查询

    public List<User> getListByOrgId(String orgId) {
        //1.实例化user对象
        User user = new User();
        //2.设置机构主键
        user.setUniid(orgId);
        //3.获取Example对象
        Example<User> ex = Example.of(user);
        //4.执行查询
        return userDao.findAll(ex);
    }

执行的sql为:

Hibernate: select user0_.useid as useid1_3_, user0_.acctyp  where user0_.uniid=?

 

2.Specification类的复杂查询

在dao中声明方法

public interface UserDao extends JpaRepository<User, Integer> {


    List<User> findAll(Specification<User> spec);
}

一、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;

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值