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

11

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