提交 630d6790 authored 作者: wzj's avatar wzj

11

上级 f94b4edb
...@@ -7,9 +7,11 @@ import io.swagger.annotations.Api; ...@@ -7,9 +7,11 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.time.LocalDate;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -107,7 +109,13 @@ public class StudentController { ...@@ -107,7 +109,13 @@ public class StudentController {
} }
@ApiOperation(value = "分页查询模糊查询名字 根据年份查询人数") @ApiOperation(value = "分页查询模糊查询名字 根据年份查询人数")
@GetMapping("/findNameYear") @GetMapping("/findNameYear")
public Page<Student> findNameYear(String name ,Integer page, Integer pageSize,Integer year){ public Page<Student> findNameYear(@RequestParam int page, @RequestParam int size, @RequestParam String name,@RequestParam String year){
return studentService.findNameYear(name,page,pageSize,year); return studentService.findNameYear(page, size, year,name );
}
@ApiOperation(value = "分页查询 根据年龄查询 并将这个年龄上下10岁的人都查出来")
@GetMapping("/findObscureAge")
public Page<Student> findObscureAge(@RequestParam Integer page, @RequestParam Integer size, @RequestParam Integer age){
return studentService.findAge(page,size,age);
} }
} }
...@@ -2,6 +2,9 @@ package com.example.demo.dao; ...@@ -2,6 +2,9 @@ package com.example.demo.dao;
import com.example.demo.pojo.Student; import com.example.demo.pojo.Student;
import io.swagger.models.auth.In; import io.swagger.models.auth.In;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.support.JpaRepositoryImplementation; import org.springframework.data.jpa.repository.support.JpaRepositoryImplementation;
...@@ -15,5 +18,15 @@ public interface StudentDao extends JpaRepository<Student,Integer>, JpaRepositor ...@@ -15,5 +18,15 @@ public interface StudentDao extends JpaRepository<Student,Integer>, JpaRepositor
List<Student> findByNameContaining(String name); List<Student> findByNameContaining(String name);
/**
*
* @param name
* @param year 2022-01-01 00:00:00
* @param year2 2022-12-31 23:59;59
* @param pageable
* @return
*/
@Query(value = "select * from student where name = ?1 and year between ?2 and ?3" ,nativeQuery = true)
Page<Student> findNameYear(String name,String year,String year2,Pageable pageable);
} }
...@@ -6,13 +6,16 @@ import com.example.demo.service.StudentService; ...@@ -6,13 +6,16 @@ import com.example.demo.service.StudentService;
import com.github.wenhao.jpa.PredicateBuilder; import com.github.wenhao.jpa.PredicateBuilder;
import com.github.wenhao.jpa.Specifications; import com.github.wenhao.jpa.Specifications;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification; import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.query.Jpa21Utils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.persistence.criteria.*; import javax.persistence.criteria.*;
import java.time.LocalDate; import java.time.LocalDate;
...@@ -594,19 +597,31 @@ public class StudentImpl implements StudentService { ...@@ -594,19 +597,31 @@ public class StudentImpl implements StudentService {
@Override @Override
//分页查询 模糊查询名字 根据年份查询人数 //分页查询 模糊查询名字 根据年份查询人数
public Page<Student> findNameYear(String name ,Integer page, Integer pageSize,Integer year) { public Page<Student> findNameYear(int page, int size, String year,String name) {
Specification<Student> studentSpecification=new Specification<Student>() { // Sort sort=Sort.by(Sort.Direction.DESC,year);
@Override Pageable pageable=PageRequest.of(page,size);
public Predicate toPredicate(Root<Student> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) { // Page<Student> studentList=null;
PredicateBuilder<Student> and = Specifications.and();
Path adminName =root.get("name"); and.like(name != null, "name", "%" + name + "%");
Predicate predicate =criteriaBuilder.like(adminName,"%"+query+"%"); and.le( year != null, "date", LocalDate.parse(year + "-12-31"));
return predicate; and.ge( year != null, "date", LocalDate.parse(year + "-01-01"));
}
}; Specification<Student> spec = and.build();
PageRequest pageRequest=PageRequest.of(page -1,pageSize); return studentDao.findAll(spec, pageable);
Page<Student> page1=studentDao.findAll(studentSpecification,pageRequest); }
return page1;
@Override
//分页查询 根据年龄查询 并将这个年龄上下10岁的人都查出来
public Page<Student> findAge(Integer page, Integer size, Integer age) {
Pageable pageable=PageRequest.of(page,size);
PredicateBuilder<Student> and=Specifications.and();
and.le(age !=null,"age",age+10);
and.ge(age !=null,"age",age-10);
Specification<Student> spec=and.build();
return studentDao.findAll(spec,pageable);
} }
...@@ -619,6 +634,7 @@ public class StudentImpl implements StudentService { ...@@ -619,6 +634,7 @@ public class StudentImpl implements StudentService {
} }
return sb.toString(); return sb.toString();
} }
public static Integer randomAge(){ public static Integer randomAge(){
long round = Math.round(Math.random() * 50); long round = Math.round(Math.random() * 50);
return new Long(round).intValue(); return new Long(round).intValue();
...@@ -629,6 +645,7 @@ public class StudentImpl implements StudentService { ...@@ -629,6 +645,7 @@ public class StudentImpl implements StudentService {
return new Long(round).intValue(); return new Long(round).intValue();
} }
public static void main(String[] args) { public static void main(String[] args) {
} }
......
...@@ -2,6 +2,7 @@ package com.example.demo.service; ...@@ -2,6 +2,7 @@ package com.example.demo.service;
import com.example.demo.pojo.Student; import com.example.demo.pojo.Student;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.List; import java.util.List;
...@@ -105,5 +106,14 @@ public interface StudentService { ...@@ -105,5 +106,14 @@ public interface StudentService {
* @param * @param
* @return * @return
*/ */
Page<Student> findNameYear(String name ,Integer page, Integer pageSize,Integer year); Page<Student> findNameYear(int page, int size, String year,String name);
/**
* 分页查询 根据年龄查询 并将这个年龄上下10岁的人都查出来
* @param page
* @param size
* @param age
* @return
*/
Page<Student> findAge(Integer page, Integer size,Integer age);
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论