기록해! 정리해!

JPAJarThymeleaf-2 (Board, QLogin) 본문

SpringBoot

JPAJarThymeleaf-2 (Board, QLogin)

zsuling 2022. 10. 19. 10:14

1. 프로젝트 생성

2. application.properties

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

# 오라클 컨넥션 정보
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=false

3. Entity추가 (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="board1019")
	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;

	}

4. 동적DSL설정 pom.xml

: 엔터티가 없으면 실행되지않음 

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.7.4</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.rubypaper</groupId>
	<artifactId>JPAJarThymeleaf-2</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>JPAJarThymeleaf-2</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>11</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>com.oracle.database.jdbc</groupId>
			<artifactId>ojdbc8</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		
				<!--  querydsl Setting 추가(1) -->
		<dependency>
			<groupId>com.querydsl</groupId>
			<artifactId>querydsl-jpa</artifactId>
		</dependency>
		
		<!--  querydsl Setting 추가(2) -->
		<dependency>
			<groupId>com.querydsl</groupId>
			<artifactId>querydsl-apt</artifactId>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			
			<!-- 동적 DSL을 위한  plugin 추가 -->
			<plugin>
				<groupId>com.mysema.maven</groupId>
				<artifactId>apt-maven-plugin</artifactId>
				<version>1.1.3</version>
				<executions>
					<execution>
						<goals>
							<goal>process</goal>
						</goals>
					    <configuration>
					    	<outputDirectory>src/main/querydsl</outputDirectory>
					    	<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
					    </configuration>
					</execution>				
				</executions>					
			</plugin>			
		</plugins>
	</build>

</project>

4. Maven Update

5. Login

: QLogin이 자동으로 생성됨

package com.rubypaper.board;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import lombok.Data;

@Data
@Entity
@Table(name="login1019")
public class Login {

	@Id
	private String id;
	private String pwd;
	
}

6. Junit5

-BoardTest

package com.rubypaper.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class BoardTest {
    
	@Test
	public void test() {
		System.out.println("Test 영역");

	}
}

>> Junit4에서 5로 업그레이드해주고

BoardTest JunitTest 하기

>> 에러없음

7. 인터페이스 생성

- BoardRepository

<엔터티이름, P.K 형>

package com.rubypaper.board;

import org.springframework.data.repository.CrudRepository;

public interface BoardRepository
            extends CrudRepository<Board, Long> {

}

- BoardService

package com.rubypaper.board;

import java.util.List;

public interface BoardService {

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

- BoardServiceImpl

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
	BoardRepository boardRepo;
	
	@Override
	public void insert(Board vo) {

		boardRepo.save(vo);
	}

	@Override
	public List<Board> selectAll(Board vo) {

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

}

 

Comments