Skip to content

Commit

Permalink
페이징 적용해보기
Browse files Browse the repository at this point in the history
  • Loading branch information
Leesanghun19 committed Aug 7, 2022
1 parent 2a6c37b commit 47d4119
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 19 deletions.
14 changes: 14 additions & 0 deletions src/main/java/pj/circles/config/SwaggerConfiguration.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package pj.circles.config;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.Pageable;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.AlternateTypeRules;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
Expand All @@ -21,6 +26,9 @@ public class SwaggerConfiguration {
@Bean
public Docket api() {
return new Docket(DocumentationType.OAS_30)

.alternateTypeRules(AlternateTypeRules
.newRule(Pageable.class, Page.class))
.useDefaultResponseMessages(false)
.select()
.apis(RequestHandlerSelectors.any())
Expand Down Expand Up @@ -55,4 +63,10 @@ private ApiInfo apiInfo() {
.version("1.0")
.build();
}
@Data
@ApiModel
static class Page{
@ApiModelProperty(value = "페이지번호")
private Integer page;
}
}
10 changes: 9 additions & 1 deletion src/main/java/pj/circles/controller/CircleController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import lombok.RequiredArgsConstructor;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -50,7 +53,12 @@ public Result circlesAll() {
.map(o -> new CirclesDto(o)).collect(Collectors.toList());
return new Result(collect);
}

@GetMapping("/circlePage")
public Result circlePaging(@PageableDefault(size = 5) Pageable pageable){
List<CirclesDto> collect = circleService.findAllPage(pageable).stream()
.map(o -> new CirclesDto(o)).collect(Collectors.toList());
return new Result(collect);
}
/**
* 단건조회
*/
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/pj/circles/service/CircleService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pj.circles.domain.*;
Expand Down Expand Up @@ -41,7 +43,9 @@ public Circle findById(Long circleId){
public List<Circle> findAll(){
return circleRepository.findAll();
}

public Page<Circle> findAllPage(Pageable pageable){
return circleRepository.findAll(pageable);
}
public List<Circle> findByCircleCategory(CircleCategory circleCategory){
return circleRepository.findByCircleCategory(circleCategory);
}
Expand Down
24 changes: 8 additions & 16 deletions src/main/java/pj/circles/service/InitDb.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public class InitDb {
@PostConstruct
public void init(){
//initService.dbInit();
//initService.dbInit2();
//initService.dbInit3();
//initService.dbInit4();
}
Expand All @@ -38,23 +37,16 @@ static class InitService {
@Value("${ps}")
private String ps;
public void dbInit() {
Member member = new Member("닉1","비번1","이메일1");
//member.setId(999L);
em.persist(member);
Circle circle = new Circle("모집중-문화-가동아리","첫줄소개",
"소개", CircleCategory.문화, CircleDivision.가동아리,true,"http~",LocalDateTime.now(),null,null,null,null,null,null,member);
em.persist(circle);

for (int num = 1; num < 31; num++){
Member member = new Member("닉"+num, "비번"+num, "이메일"+num);
//member.setId(999L);
em.persist(member);
Circle circle = new Circle("모집중-문화-가동아리"+num, "첫줄소개",
"소개", CircleCategory.문화, CircleDivision.가동아리, true, "http~", LocalDateTime.now(), null, null, null, null, null, null, member);
em.persist(circle);
}
}
public void dbInit2() {
Member member = new Member("닉2","비번2","이메일2");

em.persist(member);
Circle circle = new Circle("모집안함-학술-중앙동아리","첫줄소개2",
"소개", CircleCategory.학술, CircleDivision.중앙동아리,false,"http~",LocalDateTime.now(),null,null,null,null,null,null,member);
em.persist(circle);

}
public void dbInit3() {
Member member = new Member("string",passwordEncoder.encode("string"),"string");
Email email = new Email("string","1234");
Expand Down
5 changes: 4 additions & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ spring: #띄어쓰기 없음
username: ${dbusername}
password: ${dbpassword}
driver-class-name: com.mysql.cj.jdbc.Driver

data:
web:
pageable:
one-indexed-parameters: true
jpa: #띄어쓰기 2칸
hibernate: #띄어쓰기 4칸
#ddl-auto: create #띄어쓰기 6칸
Expand Down

0 comments on commit 47d4119

Please sign in to comment.