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;
}
上一篇: 想下个资源没分
下一篇: Spring Data JPA复杂查询
推荐阅读
-
Spring Data JPA实践与学习
-
荐 一篇文章带你快速入门 Spring Data JPA
-
Spring Data Jpa 中级联操作CascadeType的含义
-
Spring Data Solr 查询方法
-
Spring Data Jpa+SpringMVC+Jquery.pagination.js实现分页示例
-
springboot使用spring-data-jpa操作MySQL数据库
-
Spring Data JPA 实现多表关联查询的示例代码
-
SpringBoot Data JPA 关联表查询的方法
-
Spring Data JPA+kkpager实现分页功能实例
-
EasyUi+Spring Data 实现按条件分页查询的实例代码