Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
S
student
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
wzj
student
Commits
630d6790
提交
630d6790
authored
7月 08, 2022
作者:
wzj
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
11
上级
f94b4edb
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
64 行增加
和
16 行删除
+64
-16
StudentController.java
...n/java/com/example/demo/controller/StudentController.java
+10
-2
StudentDao.java
src/main/java/com/example/demo/dao/StudentDao.java
+13
-0
StudentImpl.java
src/main/java/com/example/demo/service/Impl/StudentImpl.java
+30
-13
StudentService.java
src/main/java/com/example/demo/service/StudentService.java
+11
-1
没有找到文件。
src/main/java/com/example/demo/controller/StudentController.java
浏览文件 @
630d6790
...
@@ -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
);
}
}
}
}
src/main/java/com/example/demo/dao/StudentDao.java
浏览文件 @
630d6790
...
@@ -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
);
}
}
src/main/java/com/example/demo/service/Impl/StudentImpl.java
浏览文件 @
630d6790
...
@@ -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
)
{
}
}
...
...
src/main/java/com/example/demo/service/StudentService.java
浏览文件 @
630d6790
...
@@ -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
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论