본문 바로가기
SPRING

JPA 설정방법 Spring Data 사용

by brilliant-growth 2023. 7. 5.

jpa.zip
0.03MB

 

깃 : brilliant-growth/spring_data_jpa at main · kdh11112/brilliant-growth (github.com)

 

기본 틀은 기존에 있던 JPA 설정방법과 거의 비슷함JPA 설정방법 (tistory.com)

다만 기존 방식은 dao에서 바로 갔다면 이번방식은 dao에서 service로 갔다 감

 

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

<context:component-scan base-package="kr.co.jhta.*"></context:component-scan>

<mvc:annotation-driven></mvc:annotation-driven>
<mvc:default-servlet-handler/>
<mvc:view-resolvers>
	<mvc:jsp prefix="/views/" suffix=".jsp"/>
</mvc:view-resolvers>



<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
	<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
	<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
	<property name="username" value="scott"/>
	<property name="password" value="tiger"/>
</bean>

<!-- jpa를 사용하려면 데이터소스 빈을 넣어야 됨 -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
	<property name="dataSource" ref="dataSource"></property>
	<property name="persistenceXmlLocation" value="/META-INF/persistence.xml"></property><!-- 설정파일 -->
	<property name="jpaVendorAdapter"><!-- 구현하고 있는 회사 -->
		<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"><!-- 그중 하이버네이트 쓰겟다 -->
			<property name="generateDdl" value="true"></property><!-- ddl이 없으면 생성 !!테이블이 없으면 생성해라 있으면 생성하지말고-->
			<property name="showSql" value="true"></property> <!-- sql문 어떻게 썻는지 보여주는거 공부할때 쓸만한거 -->
		</bean>
	</property>
</bean>
<!-- 트랜잭션 활성화 트랜잭션 안하면 오류남-->
<tx:annotation-driven transaction-manager="transactionManager" /> 

<!-- 트랜잭션 관련해서 처리하는거 네임스페이스에서 tx클릭 밑에서 2번쨰 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
	<!-- <property name="entityManagerFactory" ref="entityManagerFactory" /> --> 
</bean> 


</beans>

 

package kr.co.jhta.web.repository;

import java.util.List;

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

import kr.co.jhta.web.dto.DeptDTO;

@Repository
public interface DeptRepository extends JpaRepository<DeptDTO, Integer>{
	List<DeptDTO> findByDname(String dname); //findOne인거 잘은 모르겠지만 findOne은 사용불가로 
	DeptDTO findByDeptno(int deptno);//          인하여 findBy~~형식으로 구현?? 하는게 편할듯함
	
	//save(S) : 새로운 엔터티를 저장하고 ,이미 잇는 엔터티는 수정
	//delete(T) : 엔터티는 하나 삭제 , 내부적으로 entityManager.remove() 호출
	//findOne(ID) : 엔터티는 하나 조회
	//findAll : 모든 엔터티 조회
}

package kr.co.jhta.web.service;

import java.util.List;

import kr.co.jhta.web.dto.DeptDTO;

public interface DService {

	public List<DeptDTO> getDeptList();

	public void insertData(DeptDTO dto);

	public DeptDTO getDeptData(int no);

	public void updateData(DeptDTO dto);

	public void deleteData(int no);
}

 

package kr.co.jhta.web.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import kr.co.jhta.web.dto.DeptDTO;
import kr.co.jhta.web.repository.DeptRepository;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
@Transactional
@EnableJpaRepositories("kr.co.jhta.web.repository")
public class DeptService implements DService{

	@Autowired
	DeptRepository repository;
	
	@Override
	public List<DeptDTO> getDeptList() {
		log.info("getDeptList ==========>");
		return repository.findAll();
	}

	@Override
	public void insertData(DeptDTO dto) {
		repository.save(dto); //새로운 엔터티를 저장하고 , 이미 있는 엔터티는 수정함
		
	}

	@Override
	public DeptDTO getDeptData(int no) {
		
		return repository.findByDeptno(no); //no로 셀렉트

	}

	@Override
	public void updateData(DeptDTO dto) {
		repository.save(dto); //수정 입력 둘다됨
	}

	@Override
	public void deleteData(int no) {
		repository.deleteById(no);
		
	}

}

 

기존과 같이 하이버네티스를 사용하여서 이부분도 설정을 꼭해야함

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
        version="2.0">
    <persistence-unit name="persistence-unit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hiberbate.ejb.HibernatePersistence</provider>
        <properties>
        	<property name="hibernate.diarect" value="${hibernate.diarect}"/>
        	<property name="hibernate.hbm2ddl.auto" value="${hibernate.hbm2ddl.auto}"/>
        </properties>
    </persistence-unit>
</persistence>

'SPRING' 카테고리의 다른 글

SpringBoot에서 JSP 사용  (0) 2023.07.10
간단한 싱글톤  (0) 2023.07.05
REST API 구현방법  (0) 2023.07.05
JPA 설정방법  (0) 2023.07.05
스프링에서 dispatcherservlet 설정하는 방법  (0) 2023.07.05