gwooden_코린이

스프링 부트 DAO Respository 본문

스프링 부트

스프링 부트 DAO Respository

gwooden22 2023. 2. 13. 16:12
728x90
package com.example.sb;

import java.util.Optional;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SbApplicationTests {
	
	@Autowired
	private QuestionRepository questionRepository;

	@Test
	void contextLoads() {
		
		Optional<Question> o = questionRepository.findById(1);
		Question q = null;
		
		if(o.isPresent()) { //값이 있으면 true, 없으면 false
			q = o.get();
			
			System.out.println(q.getSubject());
			System.out.println(q.getContent());
		} else {
			System.out.println("조회한 결과가 없습니다.");
		}
		
		
	}

}
package com.example.sb;


import java.time.LocalDateTime;
import java.util.List;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import lombok.Getter;
import lombok.Setter;


@Entity
@Getter
@Setter
public class Question {

	@Id 
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer id; //질문코드
	
	@Column(length = 200) //varchar(200)
	private String subject;
	
	@Column(columnDefinition = "TEXT") //varchar
	private String content;
	
	private LocalDateTime createDate;
	
	//질문이 지워지면 답변들도 같이 지워지게
	@OneToMany(mappedBy = "question", cascade = CascadeType.REMOVE)
	private List<Answer> answersList;
}

 

findById() 메서드는 id라는 컬럼이 아닌 기본키 컬럼의 검색문을 작성해주는 메서드

select * from 테이블 where 기본키컬럼 = ?

 

questionRepository.findById(1)

select * from question where id=1

 


findByXXXX -> 리턴타입 알아서 정함

결과물이 여러개 나올경우

List<Question> findByXXX();

findByBetween(x, y)

findByIdLessThen(x) x 보다 작은거 < x

                   GreaterThen(x)

                   GreaterThenEqual(x)이상

findBySubjectln(배열)

findDistinctBySubject

 

- 조회

package com.example.sb;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;

public interface QuestionRepository extends JpaRepository<Question, Integer> {

	Question findBySubject(String sub);
	Question findBySubjectAndContent(String sub, String con);
	List<Question> findBySubjectLike(String sub);
	
}
		for(Question l : list) {
			System.out.println("id : " + l.getId());
			System.out.println("제목 : " + l.getSubject());
			System.out.println("내용 : " + l.getContent());
			
		}
			
		
		
//		Question q = questionRepository.findBySubjectAndContent("테스트 제목입니다." , "테스트 내용 블라블라");
//		
//		System.out.println("id : " + q.getId());
//		System.out.println("작성일자 : " + q.getCreateDate());
		
		
		
//		Question q = questionRepository.findBySubject("제목");
//		
//		System.out.println(q.getSubject());
//		System.out.println(q.getContent());
		
		
		
//		Optional<Question> o = questionRepository.findById(1);
//		Question q = null;
//		
//		if(o.isPresent()) { //값이 있으면 true, 없으면 false
//			q = o.get();
//			
//			System.out.println(q.getSubject());
//			System.out.println(q.getContent());
//		} else {
//			System.out.println("조회한 결과가 없습니다.");
//		}
		
		
		
		
//		List<Question> list = questionRepository.findAll();
//		
//		System.out.println(list.size());
//		
//		for(Question q : list) {
//			System.out.println("제목 : " + q.getSubject());
//		}

 

- 업데이트

package com.example.sb;

import java.util.Optional;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SbApplicationTests {
	
	@Autowired
	private QuestionRepository questionRepository;

	@Test
	void contextLoads() {
		
	Optional<Question> o = questionRepository.findById(3);
	
	Question q = o.get();
	q.setContent("수정함");
	
	questionRepository.save(q);

		
	}

}

 

- 삭제

package com.example.sb;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SbApplicationTests {
	
	@Autowired
	private QuestionRepository questionRepository;

	@Test
	void contextLoads() {
		
	System.out.println(questionRepository.count());
	
	questionRepository.deleteById(2);
		
	}

}

 

- 추가

package com.example.sb;

import java.time.LocalDateTime;
import java.util.Optional;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SbApplicationTests {
	
	@Autowired
	private QuestionRepository questionRepository;
	
	@Autowired
	private AnswerRepository answerRepository;

	@Test
	void contextLoads() {
		
		Optional<Question> o = questionRepository.findById(1);
		
		Question q = o.get();
		
		Answer a = new Answer();
		
		a.setContent("1번 답변 내용");
		a.setQuestion(q);
		a.setCreateDate(LocalDateTime.now());
		
		answerRepository.save(a);
		
	}

}
728x90
Comments