Hibernate快速入门

快速入门Hibernate
(1)下载Hibernate
下载地址:
http://sourceforge.net/projects/hibernate/files/hibernate4/4.3.8.Final/hibernate-release-4.3.8.Final.zip/download
(2)解压下载的Hibernate,我们可以看到如下的目录结构:
技术分享
–documentation:存放相关的文档
–lib:存放项目的开发包
–project:存放项目的源代码

(3)新建一个java项目
打开eclipse,新建一个普通的java项目即可。
(4)从lib目录中导入相应的开发包,需要的包如下:
技术分享

(5)写配置文件
在项目的src目录下,新建一个xml文件,文件名为hibernate.cfg.xml
配置文件如下:

<?xml version=‘1.0‘ encoding=‘UTF-8‘?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.url">jdbc:mysql://121.40.142.73:3306/study</property>
        <property name="connection.username">root</property>
        <property name="connection.password">thwl</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="myeclipse.connection.profile">com.mysql.jdbc.Driver</property>

    </session-factory>

</hibernate-configuration>

(6)写一个测试的实体bean

package com.zhang.dao;

public class News {
    private int id;
    private String tiltle;
    private String content;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTiltle() {
        return tiltle;
    }

    public void setTiltle(String tiltle) {
        this.tiltle = tiltle;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

}

(7)编写实体类映射文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC

        "-//Hibernate/Hibernate Mapping DTD 4.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-4.0.dtd">

  <hibernate-mapping>
     <class name="com.zhang.dao.News" table="NEWS">
         <id name="id">
            <generator class="native"/>
         </id>
         <property name="title"></property>
          <property name="content"></property>
     </class>


  </hibernate-mapping>

(8)写一个主方法进行测试

package com.zhang.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class Main {
     private static ServiceRegistry serviceRegistry; 

    public static void main(String[] args) {
        Configuration configuration = new Configuration();
        configuration.configure();
        serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
        SessionFactory sfc = configuration.buildSessionFactory(serviceRegistry);
        Session session = sfc.openSession();

        News news = new News();
        news.setTiltle("测试");
        news.setContent("这是一个测试");
        session.save(news);

        System.out.println("保存成功");

    }

}

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