[Spring3.x, Hibernate4연동하기]
1. 하이버네이트 다운로드
다운로드
www.hibernate.org (hibernate-4.2.3.Final.zip 사용)
www.hibernate.org (hibernate-4.2.3.Final.zip 사용)
lib : 하이버네이트를 실행 시 필요한 JAR 파일
project : 각종 소스 코드
documentation : 문서들
project : 각종 소스 코드
documentation : 문서들
하이버네이트를 실행하기 위해서는 hibernate4.2.3.jar 파일 뿐 아니라 lib 폴더의 각종 jar 파일이
필요하다.
물론 이클립스 하이버네이트 플러그인을 설치해도 된다. 현재 Eclipse INDIGO 까지 나와 있다.
물론 이클립스 하이버네이트 플러그인을 설치해도 된다. 현재 Eclipse INDIGO 까지 나와 있다.
2. 준비
이클립스 ? Spring Project
hibernate4 및 spring 관련 라이브러리 추가
(하이버네이트4 의 경우 스프링 3,1 이상이 필요)
(하이버네이트4 의 경우 스프링 3,1 이상이 필요)
주) 스프링의 Template Project에서 hhibernate Template을 이용하여 하이버네이트 APP 작성가능하나
라이브러리 버전이 맞지 않아 hibernate4 예제와 연동 어려움
예제 테이블 작성
예제 테이블 작성
create table myemp (
empno number ,
ename varchar2(10)
)
empno number ,
ename varchar2(10)
)
3. MyEmp.java
package edu.onj.hibernate;
public class MyEmp {
private int empno;
private String ename;
public MyEmp() {}
public MyEmp(int empno, String ename) {
this.empno = empno; this.ename = ename;
}
public int getEmpno() { return empno; }
public void setEmpno(int empno) {this.empno = empno;}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
}
private int empno;
private String ename;
public MyEmp() {}
public MyEmp(int empno, String ename) {
this.empno = empno; this.ename = ename;
}
public int getEmpno() { return empno; }
public void setEmpno(int empno) {this.empno = empno;}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
}
4. MyempDao.java
package edu.onj.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Repository
@Transactional(propagation=Propagation.REQUIRED)
public class MyEmpDao {
@Transactional(propagation=Propagation.REQUIRED)
public class MyEmpDao {
private SessionFactory sessionFactory;
private Session session;
private Session session;
public MyEmpDao() {}
//constructor injection(생성자 주입)
@Autowired
public MyEmpDao(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Autowired
public MyEmpDao(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public Session getSession() {
return sessionFactory.getCurrentSession();
}
return sessionFactory.getCurrentSession();
}
public void insertMyEmp(MyEmp myemp)
{
getSession().save(myemp);
}
getSession().save(myemp);
}
public void deleteMyEmp(int empno)
{
getSession().delete(getMyEmpByEmpno(empno));
}
getSession().delete(getMyEmpByEmpno(empno));
}
public MyEmp getMyEmpByEmpno(int empno) {
return (MyEmp)getSession().get(MyEmp.class, empno);
}
return (MyEmp)getSession().get(MyEmp.class, empno);
}
public void saveMyEmp(MyEmp myemp)
{
getSession().update(myemp);
}
}
getSession().update(myemp);
}
}
5. myemp.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="edu.onj.hibernate.MyEmp" table="MyEmp" lazy="false">
<id name="empno" type="java.lang.Integer" column="empno" />
<property name="ename" type="java.lang.String" column="ENAME" length="10"/>
</class>
</hibernate-mapping>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="edu.onj.hibernate.MyEmp" table="MyEmp" lazy="false">
<id name="empno" type="java.lang.Integer" column="empno" />
<property name="ename" type="java.lang.String" column="ENAME" length="10"/>
</class>
</hibernate-mapping>
6. spring-hibernate.xml
<?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:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd" >
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd" >
<!-- Annotation 쓰기 위해서 -->
<context:annotation-config />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
>
<property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value></property>
<property name="url"><value>jdbc:oracle:thin:@localhost:1521:onj</value></property>
<property name="username"><value>scott</value></property>
<property name="password"><value>tiger</value></property>
</bean>
<property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value></property>
<property name="url"><value>jdbc:oracle:thin:@localhost:1521:onj</value></property>
<property name="username"><value>scott</value></property>
<property name="password"><value>tiger</value></property>
</bean>
<!-- Hibernate Session Factory 설정 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource"><ref bean="dataSource"/></property>
<property name="mappingResources">
<list>
<value>myemp.hbm.xml</value>
</list>
</property>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource"><ref bean="dataSource"/></property>
<property name="mappingResources">
<list>
<value>myemp.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- 트랜잭션 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- MyEmpDAO autowiring -->
<bean id="myEmpDao" class="edu.onj.hibernate.MyEmpDao"> </bean>
</beans>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- MyEmpDAO autowiring -->
<bean id="myEmpDao" class="edu.onj.hibernate.MyEmpDao"> </bean>
</beans>
7. SpringHibernateExam.java
public class SpringHibernateExam {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-hibernate.xml");
MyEmpDao myEmpDao = (MyEmpDao)ctx.getBean("myEmpDao");
try {
MyEmp myemp1 = new MyEmp(1, "1길동");
MyEmp myemp2 = new MyEmp(2, "2길동");
MyEmp myemp3 = new MyEmp(3, "3길동");
MyEmp myemp1 = new MyEmp(1, "1길동");
MyEmp myemp2 = new MyEmp(2, "2길동");
MyEmp myemp3 = new MyEmp(3, "3길동");
myEmpDao.insertMyEmp(myemp1);
myEmpDao.insertMyEmp(myemp2);
myEmpDao.insertMyEmp(myemp2);
myEmpDao.deleteMyEmp(1);
myEmpDao.insertMyEmp(myemp3);
MyEmp emp1 =
(MyEmp)myEmpDao.getMyEmpByEmpno(3);
System.out.println(emp1.getEmpno() + "::" + emp1.getEname());
System.out.println(emp1.getEmpno() + "::" + emp1.getEname());
myemp2.setEname("2가아니고4");
myEmpDao.saveMyEmp(myemp2);
myEmpDao.saveMyEmp(myemp2);
MyEmp emp2 =
(MyEmp)myEmpDao.getMyEmpByEmpno(2);
System.out.println(emp2.getEmpno() + "::" + emp2.getEname());
System.out.println(emp2.getEmpno() + "::" + emp2.getEname());
}
catch (HibernateException e)
{ e.printStackTrace(); }
finally { }
}
}
catch (HibernateException e)
{ e.printStackTrace(); }
finally { }
}
}
8. 결과
/*
SQL> select * from myemp;
SQL> select * from myemp;
EMPNO ENAME
---------- ----------
2 2가아니고4
3 3길동
*/
---------- ----------
2 2가아니고4
3 3길동
*/
[개강확정강좌]오라클자바커뮤니티에서 운영하는 개발자 전문교육 ,개인80%환급(www.onjprogramming.co.kr)
[주간]
[11/4]Spring3.X, MyBatis, Hibernate실무과정
[11/6]SQL초보에서실전전문가까지
[평일야간]
[11/1]C#,ASP.NET마스터
[11/5]iPhone 하이브리드 앱 개발 실무과정
[11/7]JAVA&WEB프레임워크실무과정
[11/8]Spring3.X, MyBatis, Hibernate실무과정
[주말]
[11/2]C#,ASP.NET마스터
[11/2]Spring3.X, MyBatis, Hibernate실무과정
[11/2]JAVA&WEB프레임워크실무과정
[11/9]안드로이드개발자과정
[주간]
[11/4]Spring3.X, MyBatis, Hibernate실무과정
[11/6]SQL초보에서실전전문가까지
[평일야간]
[11/1]C#,ASP.NET마스터
[11/5]iPhone 하이브리드 앱 개발 실무과정
[11/7]JAVA&WEB프레임워크실무과정
[11/8]Spring3.X, MyBatis, Hibernate실무과정
[주말]
[11/2]C#,ASP.NET마스터
[11/2]Spring3.X, MyBatis, Hibernate실무과정
[11/2]JAVA&WEB프레임워크실무과정
[11/9]안드로이드개발자과정
댓글 없음:
댓글 쓰기