目录
1 Specifications单条件查询
2 Specifications多条件查询
3 Specifications模糊查询
4 Specifications的分页查询
5 Specifications的排序分页查询
root:Root接口,代表查询的根对象,可以通过root获取实体中的属性
query:代表一个顶层查询对象,用来自定义查询
cb:用来构建查询,此对象里有很多条件方法
相关依赖:
4.0.0 org.springframework.boot spring-boot-starter-parent2.6.0 com.liubujun springboot-jpa0.0.1-SNAPSHOT springboot-jpa Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starterorg.projectlombok lomboktrue org.springframework.boot spring-boot-starter-testtest org.springframework.boot spring-boot-starter-web2.4.0-SNAPSHOT org.springframework.boot spring-boot-starter-data-jpa2.4.0-SNAPSHOT mysql mysql-connector-java8.0.22 org.springframework.boot spring-boot-maven-pluginorg.projectlombok lombok
相关配置文件:
server: port: 8099 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/skywalking?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8 username: root password: lj123456 jpa: hibernate: ddl-auto: update show-sql: true properties: hibernate: format_sql: true
dao层配置:
public interface UserRepository extends JpaRepository, JpaSpecificationExecutor { }
实体类:
@Entity @Table(name = "t_user") @Data public class User implements Serializable { @Id private Integer id; private String name; private String address; private Integer age; private Date createTime; }1 Specifications单条件查询
测试类:
@Test public void testSpeci(){ Optionalone = userRepository.findOne(new Specification () { @Override public Predicate toPredicate(Root root, CriteriaQuery> query, CriteriaBuilder cb) { Path
输出结果:
2 Specifications多条件查询@Test public void testSpeci2(){ Listall = userRepository.findAll(new Specification () { @Override public Predicate toPredicate(Root root, CriteriaQuery> query, CriteriaBuilder cb) { Path
测试结果:
3 Specifications模糊查询@Test public void testSpeci3(){ Listall = userRepository.findAll(new Specification () { @Override public Predicate toPredicate(Root root, CriteriaQuery> query, CriteriaBuilder cb) { Path name = root.get("name"); Predicate namePredicate = cb.like(name.as(String.class), "王美丽"); Path address = root.get("address"); Predicate addressPredicate = cb.equal(address, "地球村"); Predicate predicate = cb.or(namePredicate, addressPredicate); return predicate; } }); System.out.println(all.size()); }
测试结果:
4 Specifications的分页查询@Test public void testSpeci3(){ //当前页是从0开始的 PageRequest pageRequest = PageRequest.of(0,1); Pagepage = userRepository.findAll(new Specification () { @Override public Predicate toPredicate(Root root, CriteriaQuery> query, CriteriaBuilder cb) { Path name = root.get("name"); Predicate namePredicate = cb.like(name.as(String.class), "王美丽"); Path address = root.get("address"); Predicate addressPredicate = cb.equal(address, "地球村"); Predicate predicate = cb.or(namePredicate, addressPredicate); return predicate; } },pageRequest); System.out.println("总记录数:"+page.getTotalElements()); System.out.println("总页数:"+page.getTotalPages()); List content = page.getContent(); content.forEach(System.out::println); }
测试结果:
5 Specifications的排序分页查询@Test public void testSpeci3(){ //当前页是从0开始的 PageRequest pageRequest = PageRequest.of(0,1,Sort.by(Sort.Direction.DESC,"createTime")); Pagepage = userRepository.findAll(new Specification () { @Override public Predicate toPredicate(Root root, CriteriaQuery> query, CriteriaBuilder cb) { Path name = root.get("name"); Predicate namePredicate = cb.like(name.as(String.class), "王美丽"); Path address = root.get("address"); Predicate addressPredicate = cb.equal(address, "地球村"); Predicate predicate = cb.or(namePredicate, addressPredicate); return predicate; } },pageRequest); System.out.println("总记录数:"+page.getTotalElements()); System.out.println("总页数:"+page.getTotalPages()); List content = page.getContent(); }
测试结果:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)