Hibernate学习笔记之在Eclipse上手动配置Hibernate文件


对于初学者来说,Eclipse手工配置Hibernate文件可能会花费不少时间,现在记下来以免以后忘记,另外希望能给其他人提供些帮助!

1.下载JBoss-Tools
注意事项:我觉得Eclipse有一点不好的地方是版本太多太杂,我自己用的是 Mars Release (4.5.0),本来想用下载包离线安装的,但是一直找不到对应的JBoss-Tools版本,于是不得不在线安装了:help–>EclipseMarketplace 如图:
Hibernate学习笔记之在Eclipse上手动配置Hibernate文件_第1张图片
2.下载好后查看windows–>open perspective–>other 如图:
Hibernate学习笔记之在Eclipse上手动配置Hibernate文件_第2张图片
3.点击new–>Hibernate Configuration File建立Hibernate配置文件hibernate.cfg.xml
Hibernate学习笔记之在Eclipse上手动配置Hibernate文件_第3张图片
4.相关属性配置
Hibernate学习笔记之在Eclipse上手动配置Hibernate文件_第4张图片
5.创建一个配置文件控制面板
Hibernate学习笔记之在Eclipse上手动配置Hibernate文件_第5张图片
6.classpath中是需要添加的jar包
Hibernate学习笔记之在Eclipse上手动配置Hibernate文件_第6张图片
7.mappings中加载O-R Mapping文件*.hbm.xml
Hibernate学习笔记之在Eclipse上手动配置Hibernate文件_第7张图片
8.点击finish
9.查看生成的hibernate.cfg.xml
9.1SessionFactory界面
Hibernate学习笔记之在Eclipse上手动配置Hibernate文件_第8张图片
9.2SourceCode,此段代码接下来还要完善



<hibernate-configuration>
    <session-factory name="mysql">
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driverproperty>
        <property name="hibernate.connection.password">qweproperty>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernateproperty>
        <property name="hibernate.connection.username">rootproperty>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialectproperty>
    session-factory>
hibernate-configuration>

10.创建持久化类Students.java

/*
 * 创建持久化类
 * */

public class Students {
    private int sid;
    private String sname;

    public int getSid() {
        return sid;
    }
    public void setSid(int sid) {
        this.sid = sid;
    }
    public String getSname() {
        return sname;
    }
    public void setSname(String sname) {
        this.sname = sname;
    }

}

11.在Students.java的包名右击:new–>Hibernate Mapping Xml file
创建映射文件Students.hbm.xml
代码如下:




<hibernate-mapping>
    <class name="HibernateDemo1.Students" table="STUDENTS">
        <id name="sid" type="int">   
            <column name="SID" />
            <generator class="assigned" />
        id>
        <property name="sname" type="java.lang.String">
            <column name="SNAME" />
        property>
    class>
hibernate-mapping>

12.将以下语句加入hibernate.cfg.xml

<property name="hibernate.show_sql">trueproperty>
  <property name="hbm2ddl.auto">updateproperty>

  <mapping resource="HibernateDemo1/Students.hbm.xml"/>

13.通过HibernateAPI编写访问数据库的代码
核心类和接口为:Configuration、SessionFactory、Session、Transaction,即本文中的HibernateTest.java

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import HibernateDemo1.Students;

public class HibernateTest {

    public static void main(String[] args) {

        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();

        Transaction tx = session.beginTransaction();//


        try {
            Students s = new Students();
            s.setSid(1);            
            s.setSname("zhangsan");
            session.save(s);
            tx.commit();

        } catch (Exception e) {

            e.printStackTrace();

        }finally{

            if(session!=null){
                session.close();
            }

        }

    }

}

14.运行HibernateTest.java即可成功连接数据库!

你可能感兴趣的:(Hibernate)