Hibernate4 使用入门

1. 到官网:http://hibernate.org/orm/ 下载最新的Hibernate

2. 在eclipse中安装Hibernate tool插件

最新的luna插件下载地址在:http://download.jboss.org/jbosstools/updates/stable/luna/
安装好有,在新建菜单里应该有:
技术分享

3. 新建一个新的java工程

将下载好的hibernate中的/lib/required包里的所有jar包都添加进去,连接数据库的驱动也要添加进去.

4. 在src目录下新建一个hibernate.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.新建 News 类 , 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.用插件生成News的.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

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。