hibernate3 eclipse3.2配置

例子是<精通hibernate3.0>的例子,权当给初学者一个教程吧.

需要自己下载hibernate3.3和mysql的jdbc驱动包.eclipse3.2

1:新建一个Java项目

 2010-10-11 11-09-01

2:在项目下面新建2个包:BasicCar和bean如图所示:

2010-10-11 11-12-08

3:导入需要导入的包:

2010-10-11 11-15-33

4:在MysqL数据库中建立一个数据库car,然后新建一个表basiccar;

SQL语句为:

create database car;
create table basiccar(id bigint not null primary key,name varchar(20),factory varchar(20),date date);
select * from basiccar;

5:在BasicCar包底下建立一个配置文件hibernate.cfg.xml

 1:  <?xml version='1.0' encoding='UTF-8'?>
 2:  <!DOCTYPE hibernate-configuration PUBLIC
 3:  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4:  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 5:  
 6:  <hibernate-configuration>
 7:  
 8:  <session-factory>
 9:      <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
10:      <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/car</property>
11:      <property name="hibernate.connection.username">root</property>
12:      <property name="hibernate.connection.password">0</property>
13:      <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
14:  <mapping resource ="basicCar/bean/BasicCar.hbm.xml"/>
15:  </session-factory>
16:  </hibernate-configuration>

 

6:在bean包底下新建一个java文件BasicCar.java,内容为:

 1:  package BasicCar.bean;
 2:  import java.util.*;
 3:  public class BasicCar implements java.io.Serializable  {
 4:   
 5:          private String name;
 6:   
 7:          private long id;
 8:   
 9:          private String factory;
10:   
11:          private Date date;
12:   
13:   
14:   
15:   
16:  public BasicCar()
17:  {
18:  }
19:      public BasicCar(String name,  String factory, Date date) {
20:   
21:          this.name = name;
22:          this.factory = factory;
23:          this.date = date;
24:      }
25:      public String getName() {
26:          return name;
27:      }
28:      public void setName(String name) {
29:          this.name = name;
30:      }
31:      public long getId() {
32:          return id;
33:      }
34:      public void setId(long id) {
35:          this.id = id;
36:      }
37:      public String getFactory() {
38:          return factory;
39:      }
40:      public void setFactory(String factory) {
41:          this.factory = factory;
42:      }
43:      public Date getDate() {
44:          return date;
45:      }
46:      public void setDate(Date date) {
47:          this.date = date;
48:      }
49:   
50:   
51:  }
52:   

7:在bean包底下建立映射文件,BasicCar.hbm.xml数据表映射文件.

 1:  <?xml version="1.0" encoding="utf-8"?>
 2:  <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 3:  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 4:  
 5:  <hibernate-mapping>
 6:      <class name="BasicCar.bean.BasicCar" table="basiccar" >
 7:          <id name="id" type="long">
 8:              <column name="id" />
 9:              <generator class="increment" ></generator>
10:          </id>
11:          <property name="name" type="java.lang.String">
12:              <column name="name" />
13:          </property>
14:           <property name="factory" type="string">
15:              <column name="factory" />
16:          </property>
17:           <property name="date" type="date">
18:              <column name="date" />
19:          </property>
20:      </class>
21:  </hibernate-mapping>
22:  

 

8:在BasicCar包底下,建立一个java文件Test.java来测试hibernate的情况:里面的内容供调试用.

  1:  package BasicCar;
  2:   
  3:  import java.sql.Date;
  4:   
  5:   
  6:  import org.hibernate.Query;
  7:  import org.hibernate.Session;
  8:  import org.hibernate.SessionFactory;
  9:  import org.hibernate.Transaction;
 10:  import org.hibernate.transaction.*;
 11:  import org.hibernate.cfg.Configuration;
 12:   
 13:  import BasicCar.bean.*;
 14:  public class Test {
 15:      Configuration cfg;
 16:   
 17:      SessionFactory sf;
 18:   
 19:      Session session;
 20:   
 21:      public static void main(String []args)
 22:      {
 23:          Test test = new Test();
 24:          test.doConfiguration();
 25:          test.openSession();    
 26:          test.saveEntity();
 27:          test.updateEntity();
 28:      //    test.deleteEntity();
 29:  
 30:  test.closeSession();
 31:          System.out.print("end");
 32:   
 33:      }
 34:   
 35:      void doConfiguration() {
 36:          cfg = new Configuration();
 37:          cfg.configure("/BasicCar/hibernate.cfg.xml");
 38:          sf = cfg.buildSessionFactory();
 39:   
 40:      }
 41:   
 42:      void openSession()
 43:      {
 44:          session = sf.openSession();
 45:   
 46:      }
 47:   
 48:      void closeSession()
 49:      {
 50:          session.close();
 51:   
 52:      }
 53:      void saveEntity()
 54:      {
 55:          Transaction tx1 =session.beginTransaction();
 56:          BasicCar bc1 = new BasicCar();
 57:          bc1.setFactory("BEIJING");
 58:          bc1.setName("sunliming");
 59:          bc1.setDate(new Date(1));
 60:          session.save(bc1);
 61:          tx1.commit();
 62:      }
 63:   
 64:      /*void deleteEntity()
 65:      {
 66:          Transaction tx3 = session.beginTransaction();
 67:  
 68:  
 69:          try{
 70:              BasicCar bc4 =(BasicCar)session.load(BasicCar.class, new Long(1));
 71:              session.delete(bc4);
 72:  
 73:  
 74:          }catch(Exception e){
 75:              System.out.print("id= 1 car are not existed , cannot be deleted;");
 76:  
 77:          }
 78:          tx3.commit();
 79:      }*/
 80:  void updateEntity()
 81:      {
 82:          Transaction tx2 =session.beginTransaction();
 83:          BasicCar bc2 = (BasicCar)session.load(BasicCar.class, new Long(1));
 84:          bc2.setFactory("LuoYang");
 85:          bc2.setName("liulu");
 86:          session.update(bc2);
 87:          tx2.commit();
 88:   
 89:   
 90:   
 91:      }
 92:   
 93:      void queryEntity()
 94:      {
 95:          try{
 96:              Query query = session.createQuery("from BasicCar");
 97:   
 98:              BasicCar bc3 =(BasicCar)query.list().get(0);
 99:              System.out.println("name for the first selected car is:" + bc3.getName());
100:          }catch(Exception e){
101:              System.out.println("no car in database");
102:   
103:          }
104:   
105:      }
106:  }
107:   

 

完成了 ,这样一个简单的hibernate就完成了,你可以在mysql数据库里面检查一下运行 情况,结果如下:

 

2010-10-11 11-31-09

posted on 2010-10-11 11:32  sunliming  阅读(4219)  评论(0编辑  收藏  举报