初试Hibernate

    技术2022-05-11  127

        Hibernate是现在炙手可热的ORM工具,最近终于成功使用它做了一个小小的例子,算是入了门。下面就谈谈这个例子吧。

        该范例运行的环境是:JBuilder9 + SQL Server 2000 + Hibernate3.1。 首先下载hibernate3.1压缩包,解压到某个文件夹下,称为%hibernate%。 用SQL Server2000建立一个表,命名为CUSTOMER,表的结构如下: 

                                 字段    |          类型         |                            ----------+-----------------------+-------                              CID    |            int        | 主键                             USERNAME |     varchar(12)       | not null                             PASSWORD |     varchar(12)       |

        用JBuilder9新建一个project,命名为example,我们用%example%表示该项目的文件夹。在菜单中选择:project -> project properties -> Required Libraries,加入%hibernate%/lib下的jar文件以及%hibernate%下的hibernate3.jar。     根据我们建立的CUSTOMER表,建立一个和它对应的类Customer,代码如下:         import java.lang.String;         public class Customer         {               private int id;               private String username;               private String password;               public int getId()               {                    return id;               }               public String getPassword()               {                    return password;               }               public String getUsername()               {                     return username;               }               public void setId(int id)               {                     this.id = id;               }              public void setPassword(String password)              {                     this.password = password;              }              public void setUsername(String username)              {                    this.username = username;              }        }     这个类就是CUSTOMER表的映射,我们用一个XML文件定义这种映射,命名为Customer.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>         <class name="Customer" table="CUSTOMER">               <id name="id" column="CID"> //定义主键的映射               </id>               <property name="username" column="USERNAME" /> //定义字段的映射               <property name="password" column="PASSWORD" />         </class>         </hibernate-mapping>     此外,还需要一个XML文件定义hibernate的相关参数,命名为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">         <hibernate-configuration>         <session-factory name="java:/hibernate/HibernateFactory">             <property name="show_sql">true</property>             <property name="connection.driver_class">                      sun.jdbc.odbc.JdbcOdbcDriver //定义数据驱动             </property>             <property name="connection.url">                      jdbc:odbc:SQL Server //定义数据库URL             </property>             <property name="connection.username">                     sa //定义数据库密码             </property>             <property name="connection.password">             </property>             <property name="dialect">                    org.hibernate.dialect.SQLServerDialect             </property>             <mapping resource="Customer.hbm.xml" />         </session-factory>         </hibernate-configuration>     选择make project编译源代码,然后把上面的两个xml文件放到%example%/classes下面,同时把%hibernate%/etc下面的文件log4j.properties一并复制到%example%/classes下面。     为了测试hibernate的效果,我们再编写一个test.java文件加入该项目,代码如下:         import org.hibernate.*;         import org.hibernate.cfg.*;         import java.lang.String;         public class Test         {              public static void main(String[] args) throws Exception              {                  try                  {                       SessionFactory sf = new Configuration().configure().buildSessionFactory();                       Session session = sf.openSession();                       Transaction tx = session.beginTransaction();                       for (int i = 0; i < 20; i++)                       {                            Customer customer = new Customer();                            customer.setId(i+1000);                            customer.setUsername("customer" + String.valueOf(i));                            customer.setPassword("good");                            session.save(customer);                       }                       tx.commit();                       session.close();                  } catch (HibernateException e)                   {                        e.printStackTrace();                   }              }         }     在该项目中运行test,可以看到在CUSTOMER表中插入了20条记录。但是我们在test.java中并没有直接调用JDBC,也没有直接使用SQL语句。


    最新回复(0)