您的位置:首页 > 编程语言 > Java开发

Hibernate入门教程2--Hibernate3在Eclipse3.2下的简单开发

2007-03-30 10:46 686 查看
 

 

所需的环境:Eclipse3.2  Hibernate3.2  MySQL  Hibernate Synchronizer3.1.9

先后Synchronizer3.1.9(eclispe中开发hibernate的插件)的文件放到eclipse中的plugin中。然后在Eclipse3.2建一个Java Project,然后建2 个Folder,一个src,另外一个lib(存放jar包)。把hibernate3中的所有jar包放进去,另外我用的是MySQL。把MySQL的jar包(mysql-connector-java-3.1.7-bin.jar)也放到项目中的lib下。现在准备工作已经OK了。

mysql:

use test;
CREATE TABLE CUSTOMER
(
    CID INT NOT NULL PRIMARY KEY,
    USERNAME VARCHAR(12) NOT NULL,
    PASSWORD VARCHAR(12)
);

在src上右键新建一个Other,进去后Hibernate->Hibernate Configuration File。

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration
    PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
 <session-factory>
  <!-- local connection properties -->
  <property name="hibernate.connection.url">
   jdbc:mysql://localhost:3306/test
  </property>
  <property name="hibernate.connection.driver_class">
   org.gjt.mm.mysql.Driver
  </property>
  <property name="hibernate.connection.username">root</property>
  <property name="hibernate.connection.password">1234</property>
  <!-- property name="hibernate.connection.pool_size"></property -->
  <!-- dialect for MySQL -->
  <property name="dialect">
   org.hibernate.dialect.MySQLDialect
  </property>
  <property name="hibernate.show_sql">false</property>
  <property name="hibernate.transaction.factory_class">
   org.hibernate.transaction.JDBCTransactionFactory
  </property>
  <mapping resource="mapping/Customer.hbm.xml" />
 </session-factory>
</hibernate-configuration>

在src下新建一下mapping文件夹。在mapping右键新建other,进去后Hibernate->Hibernate Mapping File,将会生成一个Custom.hbm.xml。<generator class="increment"/>,一定要为increment。在此文件右键Hibernate Synchronizer->Synchronizer Files。会自动生成一个mapping.base包。再次此文件Custom.hbm.xml右键Hibernate Synchronizer->Add Mapping References,在Hibernate3中,会自动把hibernate.cfg.xml中的

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration
    PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

去掉,你需手动加上去。

Custom.hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
 "-//Hibernate/Hibernate Mapping DTD//EN"
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping package="mapping">
 <class
  name="Customer"
  table="customer"
 >
  <meta attribute="sync-DAO">false</meta>
  <id
   name="Id"
   type="integer"
   column="CID"
  >
   <generator class="increment"/>
  </id>

  <property
   name="Username"
   column="USERNAME"
   type="string"
   not-null="true"
   length="12"
  />
  <property
   name="Password"
   column="PASSWORD"
   type="string"
   not-null="false"
   length="12"
  />

 </class> 
</hibernate-mapping>

我们再在mapping.base下建一个新类吧。

Test.java:

package mapping.base;

import mapping.Customer;

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;

public class Test {
 public static void main(String[] args) {
  try {
   SessionFactory sf = new Configuration().configure()
     .buildSessionFactory();
   Session session = sf.openSession();
   Transaction tx = session.beginTransaction();

   for (int i = 0; i < 200; i++) {
    Customer customer = new Customer();
    customer.setUsername("customer" + i);
    customer.setPassword("customer");
    session.save(customer);
   }

   tx.commit();
   session.close();
  } catch (HibernateException e) {
   e.printStackTrace();
  }
 }

}

OK,over.你去你的数据库里去看下,是不是插进去了很多数据呀?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息