깃 : brilliant-growth/spring_jpa at main · kdh11112/brilliant-growth (github.com)
JPA는 인터페이스 이고 Hibernate는 JPA의 구현체중 하나이다
<?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이 없으면 생성 !!테이블이 없으면 생성해라 있으면 생성하지말고-->
</bean>
</property>
</bean>
<!-- 트랜잭션 관련해서 처리하는거 네임스페이스에서 tx클릭 밑에서 2번쨰 -->
<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"></property>
</bean>
<!-- 트랜잭션 활성화 트랜잭션 안하면 오류남-->
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
위 name "generateDdl"에서 테이블이 없으면 생성하고
DTO에서 테이블에 관한 설정들을 해준다
package kr.co.jhta.web.dto;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Entity
@Table(name="department1")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class DeptDTO {
@Id//pk
@GeneratedValue(strategy= GenerationType.SEQUENCE)//시퀀스생성
int deptno;
String dname;
@Column(length=50, nullable=false) //null 사용 허용할꺼니? 기본이 false
String loc;
}
DAO 에서 데이터를 가져와 처리한다
package kr.co.jhta.web.repository;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;
import org.springframework.stereotype.Repository;
import kr.co.jhta.web.dto.DeptDTO;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Repository
@Transactional
public class DeptDao implements Dao{
@PersistenceContext
private EntityManager em;
@Override
public List<DeptDTO> getDeptList() {
log.info("em : {}",em);
return em.createQuery("select b from DeptDTO b order by b.deptno desc").getResultList(); //리스트 출력 list
}
@Override
public void insertData(DeptDTO dto) {
log.info("insertData() ====>");
em.persist(dto); //입력 form
}
@Override
public DeptDTO getDeptData(int no) {
return em.find(DeptDTO.class, no); //하나만 출력 detail
//클래스를 써야함
}
@Override
public void updateData(DeptDTO dto) {
em.merge(dto);//수정 modify
}
@Override
public void deleteData(int no) {
em.remove(em.find(DeptDTO.class, no));//삭제
}
}
'SPRING' 카테고리의 다른 글
JPA 설정방법 Spring Data 사용 (0) | 2023.07.05 |
---|---|
REST API 구현방법 (0) | 2023.07.05 |
스프링에서 dispatcherservlet 설정하는 방법 (0) | 2023.07.05 |
UTF-8 필터 xml 처리 (0) | 2023.07.05 |
시큐리티 간단 설정 (0) | 2023.07.05 |