1. pom.xml 추가
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
2. Entity 파일 생성
- @data 어노테이션은 lombok 을 사용해서 나온것
- int -> Integer 형으로 변경. primitive type 은 사용불가!!
package com.edentns.rest.gateway.Entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import lombok.Data;
@Data
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Integer user_seq;
@Column(length = 50, nullable = true)
private String user_name;
@Column(length = 50, nullable = true)
private String user_type;
}
3. Repository 생성
- Interface 로 생성하되 Jparepository 를 상속 받으면 자동으로 CRUD 메소드 사용가능.
package com.edentns.rest.gateway.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.edentns.rest.gateway.Entity.User;
@Repository
public interface UserRepository extends JpaRepository<User, Integer>{
}
4. Test 코드 생성
package com.edentns.rest.gateway;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.edentns.rest.gateway.Entity.User;
import com.edentns.rest.gateway.repository.UserRepository;
@RunWith(SpringRunner.class)
@SpringBootTest
public class RepositoryTests {
private final Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());
@Autowired
UserRepository userRepository;
@Test
public void repositoryTests() throws Exception {
List<User> userList = userRepository.findAll();
logger.info("userList : {}", userList);
}
}
테스트완료.
'PROJECT > TOOL' 카테고리의 다른 글
이클립스 Line width 변경 (0) | 2019.11.08 |
---|---|
Spring Boot index 페이지 설정 (0) | 2019.11.07 |
Jpa vs Mybatis (0) | 2019.11.06 |
Boot Spring 기본 WAS PORT 변경 (0) | 2019.11.04 |
[크롬] API 테스트 - Talend API Tester (0) | 2019.10.28 |