2015년 10월 5일 월요일

spring jpa 설정 및 테스트 (maven 설정)

거의 대부분 mybatis 를 이용하여 개발을 하는데..
JPA가 대세라고 해서 가벼운 프로젝트에 연동을 해봤습니다.

1. 라이브러리 import....
maven pom.xml


   org.springframework.data
   spring-data-jpa
   1.9.0.RELEASE


   org.hibernate
   hibernate-entitymanager
   4.3.8.Final



2. Entity class를 만들어 줍니다.
참고로 SerializedName, Expose는 jpa와 직접 관련은 없습니다.. (개체를 그대로 JsonView 할때 사용)

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

import javax.persistence.*;

@Entity
@Table(name="tb_notice")
public class Notice {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "notice_id")
    @SerializedName(value = "notice_id")
    @Expose
    private Integer noticeId;

    @Column(name="title", nullable = false)
    @Expose
    private String title;

    @Column(name="content", nullable = false)
    @Expose
    private String content;

    @Column(name="reg_date", nullable = false)
    @SerializedName(value = "reg_date")
    @Expose
    private String regDate;

    @Column(name="del_yn", nullable = false)
    @Expose(serialize = false, deserialize = false)
    private String delYn;

    public Integer getNoticeId() {
        return noticeId;
    }

    public void setNoticeId(Integer noticeId) {
        this.noticeId = noticeId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getRegDate() {
        return regDate;
    }

    public void setRegDate(String regDate) {
        this.regDate = regDate;
    }

    public String getDelYn() {
        return delYn;
    }

    public void setDelYn(String delYn) {
        this.delYn = delYn;
    }
}



3. repository 를 만들어줍니다. (아무것도 없는게.. 인상적)

public interface NoticeRepository extends JpaRepository<Notice, Integer> {

}



4. context-jpa.xml 설정합니다. (txManager2인 이유는 기존에 mybatis에 영향을 주지 않기 위해서입니다. , mybatis를 한번에 다 걷어낼 자신이 없...)




    
    
        
        
    

    
        
            
            
        
    

    
        
            
            
        
    

    
        
        
    

    
    
        
            
                
                
            
        
    

    
    





5. 사용 예제

@Service
public class NoticeService extends ServiceBase  {
    private static final Logger logger = LoggerFactory.getLogger(NoticeService.class);

    @Autowired
    private NoticeRepository noticeRepository;

    public void srXX(RequestData req, ResponseData res) throws Exception {
        List<Notice> list = noticeRepository.findAll();
        res.put("notice_list", list);
    }
}


인터넷상에 자료가 많아서 설정은 어렵지 않았습니다.
하지만 실제로 사용에 요령이 필요하다고 하네요.. (제대로 이해를 하지 않고 사용하면 성능에도 영향을 준다고 함)

댓글 없음: