JAVA/Spring(Springboot)
-
[Spring] ajax로 form 데이터 serialize() 전송하여 controller에서 객체로 받기/ajax로 json 데이터 전송하여 controller에서 객체로 받기(@RequestBody)JAVA/Spring(Springboot) 2023. 7. 22. 21:33
ajax로 데이터를 전송하여 controller에서 데이터를 받는 방법에 대해 알아보겠다. 데이터를 ajax로 넘길때 form 데이터를 serialize() 해서 보내는 방식이 있고 json으로 데이터를 전송하는 방식이 있다. 이 두가지를 코드로 살펴보자. 우선 form에 있는 데이터를 전부 보내는 방식을 살펴보자. html 카테고리명 활성화 html 코드이다. form에서 input 태그로 넘어갈 값들은 name(카테고리명), isDel(활성화여부) 이다. 등록 버튼을 클릭하면 createCategory() 함수가 실행된다. javascript function createCategory() { // 이름이 중복인지, 유효성 확인 $.ajax({ url: '/admin/category/create', d..
-
[Querydsl] Q Entity 생성시 javax.persistence.Entity 오류 해결 법JAVA/Spring(Springboot) 2023. 5. 30. 15:46
Querydsl 을 사용할 때 Q Entity 생성시 (compileQuerydsl 실행시) javax.persistence.Entity 오류가 발생하는 경우가 있다. 이때 build.gradle 에 querydsl 의존성을 수정해주면된다. querydsl 의존성 추가 코드에 ":jakarta" 를 추가해주면 된다. buildscript { ext { queryDslVersion = "5.0.0" } } dependencies { // querydsl 추가 implementation "com.querydsl:querydsl-jpa:${queryDslVersion}:jakarta" annotationProcessor "com.querydsl:querydsl-apt:${queryDslVersion}:jaka..
-
[Spring] QueryDsl 페이징 쿼리 사용법JAVA/Spring(Springboot) 2023. 4. 8. 23:13
QueryDsl로 페이징 쿼리를 작성할 때 count 쿼리를 분리해서 사용하면 성능이 개선되는 경우가 있다. count 쿼리를 분리해서 작성하는 법을 알아보자. 우선 return 타입으로 PageImpl 방식과 PageableExecutionUtils.getPage 방식이 있다. PageImpl 방식은 totalCount를 구하는 쿼리를 무조건 실행하게 되지만, PageableExecutionUtils.getPage 방식은 필요한 경우에만 totalCount를 실행하게 할 수 있다. totalCount 쿼리 실행이 필요없는 경우는 생략해서 처리한다. - 페이지 시작이면서 컨텐츠 사이즈가 페이지 사이즈보다 작을 때 - 마지막 페이지일 때 (offset + 컨텐츠 사이즈를 더해서 전체 사이즈 구함) 코드를 ..
-
[SpringBootTest] AssertJ List의 원하는 컬럼만 가져와서 포함됐는지 안됐는지 확인하기 - extracting/containsExactlyJAVA/Spring(Springboot) 2023. 4. 5. 14:47
이름(username)과 나이(age) 필드를 가진 Member라는 클래스가 있다. List result = new ArrayList(); result.add(new Member("memberA", 10)); result.add(new Member("memberB", 20)); 이때 resullt에 username에 "memberA"가 포함되었는지 확인할 수 있다. Assertions.assertThat( 리스트 ).extracting( 원하는 컬럼 ).containsExactly( 값 ); 문법을 사용하면 된다. Assertions.assertThat(result) .extracting("username") .containsExactly("memberA") .doesNotContain("teamC");..
-
[Spring] JPA Repository 커스텀하기 - Repository 분리, 쿼리 방식 다르게 등에 사용JAVA/Spring(Springboot) 2023. 4. 4. 10:58
JPA Repository 한 파일에 메서드를 정의하게 되면 중요한 핵심 로직과 단순한 조회 로직 등 여러 메서드가 섞여서 저장되기 때문에 유지보수하기에 어려운 점이 있다. 또한 한 Repository 안에서 Spring data jpa 를 사용할 수도 있고 기본 myBatis를 사용할 수도 있고, jdbc template를 사용할 수도 있다면 Repository 분리가 필요하게 된다. Repository 는 하나로 사용하되, custom된 interface를 상속받으면 된다. 코드로 살펴보자. 아래를 보면 MemberRepository 에 여러 메서드가 조회되어 있다. public interface MemberRepository extends JpaRepository { List findUser(Str..
-
[Spring] MessageResolver 의 메시지 코드JAVA/Spring(Springboot) 2023. 2. 25. 11:07
Spring에서 제공하는 BindingResult를 통해서 rejectValue() 함수 내에서 에러 코드를 가져올 때 내부적으로 MessageResolver를 사용한다. MessageResolver를 통해서 에러 코드를 어떤걸 생성하는지 살펴보자. Object Error public class MessageCodesResolverTest { MessageCodesResolver codesResolver = new DefaultMessageCodesResolver(); @Test void messageCodesResolverObject() { String[] messageCodes = codesResolver.resolveMessageCodes("required", "item"); for (String ..
-
[Spring] PRG(Post-Redirect-Get) 할 때 PathVariable, Url Encoding 신경쓰기 - RedirectAttributeJAVA/Spring(Springboot) 2023. 2. 21. 23:40
PRG 할 때 RedirectAttribute를 사용하면 편리하다. 예를 들어 상품을 등록한 후 상품 상세 페이지로 가고 싶을 때 코드를 살펴보자. @PostMapping("/add") public String addItem(Item item, RedirectAttributes redirectAttributes) { Item savedItem = itemRepository.save(item); redirectAttributes.addAttribute("itemId", savedItem.getId()); redirectAttributes.addAttribute("status", true); return "redirect:/basic/items/{itemId}"; } RedirectAttribute를 사용하..
-
Query dsl 프로젝트 설정 시 unable to load class 'com.mysema.codegen.model.type'. gradle 오류 해결JAVA/Spring(Springboot) 2022. 2. 8. 20:30
Query dsl 을 프로젝트로 설정할 때 gradle이 5.0 이상이면 추가로 작업해줘야하는 부분이있다. 추가하지 않을 시 compileQuery를 실행할 때 unable to load class 'com.mysema.codegen.model.type'. gradle 오류가 난다. build.gradle 파일 코드 buildscript { ext { queryDslVersion = "5.0.0" } } plugins { id 'org.springframework.boot' version '2.6.3' id 'io.spring.dependency-management' version '1.0.11.RELEASE' //querydsl 추가 id "com.ewerk.gradle.plugins.querydsl"..