기록해! 정리해!

JPAJarTymeleaf-1 (board) 본문

SpringBoot

JPAJarTymeleaf-1 (board)

zsuling 2022. 10. 18. 12:17

1. 프로젝트 생성

2. application.properties

# 주석 ( none , servlet )
spring.main.web-application-type=servlet
server.port=8888

# 오라클 컨넥션 정보
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url =jdbc:oracle:thin:@localhost:1521:xe
spring.datasource.username=system
spring.datasource.password=1234

## JPA  Setting (create, update)
spring.jpa.hibernate.ddl-auto=update
spring.jpa.generate-ddl=false
spring.jpa.show-sql=true
spring.jpa.database-platform=org.hibernate.dialect.OracleDialect
spring.jpa.properties.hibernate.format_sql=true


#thymeleaf cache setting
spring.thymeleaf.cache=fals

3. Board

package com.rubypaper.board;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import lombok.Data;

@Data
@Entity
@Table(name="board1018")
public class Board {

	@Id
	@GeneratedValue
	private Long seq;
	
	@Column(nullable=false, length=30) //not null, varchar(30)
	private String title;
	
	@Column(unique=true) //유일키 제약조건
	private String writer;
	private String content;
	
	//디폴트값을 부여할때 insertable=false은 필수
	@Column(insertable=false, updatable=false, columnDefinition="date default sysdate")
	private Date createDate;
	
	@Column(insertable=false, columnDefinition="number default 1")
	private int cnt;
	
	private String password;
	
	@Transient // 컬럼을 만들 필요가 없는 변수 정의 
	private  String  searchCondition;
	
	@Transient
	private  String  searchKeyword;

}

+ 서치를 추가하였음 

import javax.persistence.Transient;

seq int로 잡았다가 후에 Long으로 바꿈 ㅎㅎ

4. 인터페이스 생성 (추상메소드 정의)

- BoardRepository

package com.rubypaper.board;

import org.springframework.data.repository.CrudRepository;

public interface BoardRepository
            extends CrudRepository<Board, Long> {

}

*CrudRepository (컨트롤+마우스) 하면 메소드 볼 수 있음

Iterable<T> findAllById(Iterable<ID> ids); : 아이디를 가지고 서치하기

void deleteById(ID id); 의 리턴형은 void다 id를 가지고만 삭제할 수 있다 name이런거 안됨

>> JPA는 인터페이스를 만들어서 상속받고 여기서 추상메소드들을 또 만들어서 사용할 수 있다

메소드들 공부해아함 ㅜ 엄청 많음

 

-BoardSerive

package com.rubypaper.board;

import java.util.List;

public interface BoardService {

	void insert(Board vo);
	List<Board> getBoardList();
}

-BoardSeriveImpl

: boardRepo. save, findAll 은 CrudRepository 여기 안에 있는 메소드들..

package com.rubypaper.board;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BoardServiceImpl implements BoardService{

	@Autowired
	private BoardRepository boardRepo;
	
	@Override
	public void insert(Board vo) {
		boardRepo.save(vo);
	}

	@Override
	public List<Board> getBoardList() {

		return (List<Board>)boardRepo.findAll();
	}

}

5. 테스트해보기 (UI를 만들지않고 테스트하는 것)

JUnit 5 추가

package com.rubypaper;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.rubypaper.board.Board;
import com.rubypaper.board.BoardRepository;

@RunWith(SpringRunner.class) //스프링을 실행하겠다
@SpringBootTest
public class BoardTest {

	@Autowired
	private BoardRepository BoardRepo;
	
	@Test
	public void testInsert() {
		for (Long i = 1L; i<= 10; i++) {
			Board board= new Board();
			board.setTitle("게시판 제목" + i);
			board.setPassword("1234");
			board.setWriter("소금빵"+i);
			board.setContent("게시판 내용" + i);
			BoardRepo.save(board);
		}
	}
}

'SpringBoot' 카테고리의 다른 글

JPAJarTymeleaf-1 UI (getBoardList, form)  (0) 2022.10.18
JPAJarTymeleaf-1 Test(Crudrepository 메소드사용)  (0) 2022.10.18
JPA - Board(Junit -2)  (0) 2022.10.17
JPA - Board(Junit-1)  (0) 2022.10.17
JPAJarTymeleaf - Board  (0) 2022.10.17
Comments