Hibernate 4 사용 입문

1.홈 페이지 방문:http://hibernate.org/orm/ 최신 Hibernate 다운로드
2.eclipse 에 Hibernate tool 플러그 인 설치
최신 luna 플러그 인 다운로드 주소:http://download.jboss.org/jbosstools/updates/stable/luna/ 설치 되 어 있 습 니 다.새 메뉴 에 있 을 것 입 니 다.
3.새로운 자바 프로젝트 새로 만 들 기
     hibernate  /lib/required     jar      ,              .

4.src 디 렉 터 리 아래 hibenate.cfg.xml 설정 파일 을 새로 만 듭 니 다(플러그 인 으로 새로 만 들 면 됩 니 다)
xml 에 데이터베이스 정보 와 설정 을 적 습 니 다.
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
    <session-factory>
        <!--           -->
        <property name="connection.username">javaTest</property>
        <property name="connection.password">123456</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/sample</property>

        <!--   hibernate      -->
        <!--    -->
        <property name="dialetc">org.hibernate.dialect.MySQLInnoDBDialect</property>

        <!--   sql -->
        <property name="show_sql">true</property>

        <!-- sql    -->
        <property name="format_sql">true</property>

        <!--         -->
        <property name="hbm2ddl.auto">update</property>

        <!--    hbm.xml   -->
        <mapping resource="com/qhn/News.hbm.xml"/> 
    </session-factory>
    </hibernate-configuration>

5.새 뉴스 클래스,get set,구조 함수 등...
package com.qhn;

import java.sql.Date;

public class News {
    private Integer id;
    private String title;
    private String author;
    private Date date;

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public Date getDate() {
        return date;
    }
    public void setDate(Date date) {
        this.date = date;
    }

    @Override
    public String toString() {
        return "News [id=" + id + ", title=" + title + ", author=" + author
                + ", date=" + date + "]";
    }
    public News(String title, String author, Date date) {
        super();
        this.title = title;
        this.author = author;
        this.date = date;
    }
    public News() {
    }
}

6.뉴스.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">
<!-- Generated May 14, 2015 8:33:47 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="com.qhn.News" table="NEWS">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="native" />
        </id>

        <property name="title" type="java.lang.String">
            <column name="TITLE" />
        </property>

        <property name="author" type="java.lang.String">
            <column name="AUTHOR" />
        </property>

        <property name="date" type="java.sql.Date">
            <column name="DATE" />
        </property>

    </class>
</hibernate-mapping>

7.테스트 클래스 테스트 구축
package com.qhn;

import static org.junit.Assert.*;

import java.sql.Date;

import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.Test;

@SuppressWarnings("deprecation")
public class HibernateTest {

    @Test
    public void test() {

        //1. SessionFectory

        SessionFactory sessionFactory = null;

        Configuration configuration = new Configuration().configure();

        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
            .applySettings(configuration.getProperties())
            .buildServiceRegistry();

        sessionFactory = configuration.buildSessionFactory(serviceRegistry);

        //2. Sesson

        Session session = sessionFactory.openSession();

        //3.     

        Transaction transaction = session.beginTransaction();

        //4.       

        News news = new News("java","HI",new Date(new java.util.Date().getTime()));

        session.save(news);

        //5.     

        transaction.commit();

        //6.   Session

        session.close();

        //7.   SessionFectory

        sessionFactory.close();
    }

}

실행 후 대응 하 는 데이터베이스 에서 새로 만 든 시 계 를 찾 을 수 있 고 데 이 터 를 삽입 할 수 있 습 니 다.
마지막 으로 전체 프로젝트 의 파일 구 조 는 다음 과 같다.
├── bin
│   ├── com
│   │   └── qhn
│   │       ├── HibernateTest.class
│   │       ├── News.class
│   │       └── News.hbm.xml
│   └── hibernate.cfg.xml
├── lib
│   ├── antlr-2.7.7.jar
│   ├── dom4j-1.6.1.jar
│   ├── hibernate-commons-annotations-4.0.5.Final.jar
│   ├── hibernate-core-4.3.9.Final.jar
│   ├── hibernate-jpa-2.1-api-1.0.0.Final.jar
│   ├── jandex-1.1.0.Final.jar
│   ├── javassist-3.18.1-GA.jar
│   ├── jboss-logging-3.1.3.GA.jar
│   ├── jboss-logging-annotations-1.2.0.Beta1.jar
│   ├── jboss-transaction-api_1.2_spec-1.0.0.Final.jar
│   └── mysql-connector-java-5.1.34-bin.jar
└── src
    ├── com
    │   └── qhn
    │       ├── HibernateTest.java
    │       ├── News.hbm.xml
    │       └── News.java
    └── hibernate.cfg.xml

좋은 웹페이지 즐겨찾기