基于NetBeans7.0的Hibernate编程入门



 

 

 基于
NetBeans7.0 Hebernate入门

1.       建立一个java项目JavaHibernat如图:


基于NetBeans7.0的Hibernate编程入门_第1张图片
 

 

2.       新建一个数据连接:

基于NetBeans7.0的Hibernate编程入门_第2张图片

 

3.       添加Hibernate库,如图:

基于NetBeans7.0的Hibernate编程入门_第3张图片

 

 

4.       使用向导新建Hibernate配置文件:

基于NetBeans7.0的Hibernate编程入门_第4张图片

 

 

 

 


基于NetBeans7.0的Hibernate编程入门_第5张图片
 

 


基于NetBeans7.0的Hibernate编程入门_第6张图片
 

 

结果自动生成如下文件:

hibernate.cfg.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="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/book</property>

    <property name="hibernate.connection.username">root</property>

    <property name="hibernate.connection.password">123456</property>

  </session-factory>

</hibernate-configuration>

5.       使用Hibernate逆向工程向导生成hibernate.reveng.xml文件:如图所示添加选中的数据表。


基于NetBeans7.0的Hibernate编程入门_第7张图片
 

 

自动生成的文件:

hibernate.reveng.xml

 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd " >

<hibernate-reverse-engineering>

  <schema-selection match-catalog="book"/>

  <table-filter match-name="admin"/>

</hibernate-reverse-engineering>

 

6.       使用Hibernate向导生成Hibernate映射文件和POJOPlain Ordinary Java Object)文件:

 

基于NetBeans7.0的Hibernate编程入门_第8张图片

 

 

生成的POJO文件是:
 

Admin.java

package javahibernate;

// Generated 2011-11-19 10:57:36 by Hibernate Tools 3.2.1.GA

 

/**

 * Admin generated by hbm2java

 */

public class Admin  implements java.io.Serializable {

     private int UId;

     private String UName;

     private String UPass;

   

public Admin() {

    }

    public Admin(int UId) {

        this.UId = UId;

    }

    public Admin(int UId, String UName, String UPass) {

       this.UId = UId;

       this.UName = UName;

       this.UPass = UPass;

    }

  

    public int getUId() {

        return this.UId;

    }

   

    public void setUId(int UId) {

        this.UId = UId;

    }

    public String getUName() {

        return this.UName;

    }

   

    public void setUName(String UName) {

        this.UName = UName;

    }

    public String getUPass() {

        return this.UPass;

    }

   

    public void setUPass(String UPass) {

        this.UPass = UPass;

    }

}

 

生成的Hibernate映射文件是:

Admin.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 2011-11-19 10:57:36 by Hibernate Tools 3.2.1.GA -->

<hibernate-mapping>

    <class name="javahibernate.Admin" table="admin" catalog="book">

        <id name="UId" type="int">

            <column name="u_id" />

            <generator class="assigned" />

        </id>

        <property name="UName" type="string">

            <column name="u_Name" length="20" />

        </property>

        <property name="UPass" type="string">

            <column name="u_Pass" length="20" />

        </property>

    </class>

</hibernate-mapping>

 

此时Hibernate配置文件hibernate.cfg.xml中,在<session-factory>标记内增加了映射信息:

<mapping resource="javahibernate/Admin.hbm.xml"/>

到此为止,基于Hibernate的持久化工作已准备就绪。所有的工作基本上是自动完成的。

7.       将下面的代码添加到主类中,运行项目即可。

 

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;

public class JavaHibernate {

 

    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) {

        // TODO code application logic here

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

        SessionFactory sessionFactory=config.buildSessionFactory();

        Admin user=new Admin();

        user.setUId(3);

        user.setUName("aaa");

        user.setUPass("123456");

        Session session=sessionFactory.openSession();

        Transaction tx=session.beginTransaction();

        session.save(user);

        tx.commit();

        session.close();

        sessionFactory.close();

    }

}

 

然后打开数据表可以验证运行是否成功。

你可能感兴趣的:(java,Hibernate,框架,Netbeans)