Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1013924
  • 博文数量: 584
  • 博客积分: 2293
  • 博客等级: 大尉
  • 技术积分: 3045
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-28 11:15
文章分类

全部博文(584)

文章存档

2012年(532)

2011年(47)

2009年(5)

我的朋友

分类:

2012-08-04 23:05:57

1.启动netbean--->new project,选择general类型-->Java Application
   Next>       项目名为HiberUserQuery;
                  project localtion为:e:hiber_test;
                  project Folder为:e:hiber_testHiberUserQuery
把Main Class改为QueryTest(不打在包里)
   Finish.

建好后project的目录结构应如下:
|HiberUserQuery
|--build
|--dist
|--nbproject
|--src
    |--com
        |--mdc
|--test
|build.xml
|manifest.mf

2.把Hibernate要用到的jar都导入到项目中来
   我是把Hibernate要用的到jar都加入到了netbean的library管理器中并命名为Hibernate-3.1,以下为jar文件的列表

ant-antlr-1.6.5.jar
antlr-2.7.6rc1.jar
asm-attrs.jar
asm.jar
cglib-2.1.3.jar
commons-collections-2.1.1.jar
commons-logging-1.0.4.jar
dom4j-1.6.1.jar
ehcache-1.1.jar
hibernate3.jar
jaxen-1.1-beta-7.jar
jdbc2_0-stdext.jar
jta.jar
log4j-1.2.11.jar
xml-apis.jar

这样只要右击项目HiberUserQuery中的Libraries-->Add Library,再在列表中选择Hibernate-3.1就可以了。
另:JDBC驱动ojdbc14.jar我放在了%JAVA_HOME%jrelibext下。

3.创建User类和映射
  右击项目HiberUserQuery中的Source Packages,选择new-->java package
  package Name:com.mdc
右击新建的package,选择new-->Java Class
  Class Name:User
  User.java

/*
  1. * User.java
  2. *
  3. * Created on 2006年4月26日, 下午12:06
  4. *
  5. * Copyright (c) 2006 Wuxi MDC Co. Ltd.
  6. * All right reserved.
  7. */

  8. package com.mdc;

  9. /**
  10. *
  11. * @author Chen QingHui
  12. */
  13. public class User
  14. {
  15.     private int id;
  16.     private String username;
  17.     private String password;
  18.    
  19.     /** Creates a new instance of User */
  20.     public User()
  21.     {
  22.     }

  23.     public int getId()
  24.     {
  25.         return id;
  26.     }

  27.     public String getName()
  28.     {
  29.         return username;
  30.     }

  31.     public String getPass()
  32.     {
  33.         return password;
  34.     }
  35.    
  36.     public void setId(int id)
  37.     {
  38.         this.id = id;
  39.     }

  40.     public void setName(String username)
  41.     {
  42.         this.username = username;
  43.     }

  44.     public void setPass(String password)
  45.     {
  46.         this.password = password;
  47.     }
  48.    
  49. }

User.hbm.xml

  1. <?xml version='1.0' encoding='UTF-8'?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4. "">

  5. <hibernate-mapping>

  6.     <class name="com.mdc.User" table="UserTable">
  7.         <id name="id" type="int">
  8.             <column name="id" />
  9.             <generator class="sequence">
  10.                 <param name="sequence">seq_usertable_id</param>
  11.             </generator>
  12.         </id>
  13.         <property name="name">
  14.             <column name="username" />
  15.         </property>
  16.         <property name="pass">
  17.             <column name="password" />
  18.         </property>
  19.     </class>

  20. </hibernate-mapping>


4.在中,完成QueryTest.java,并在hibernate的发行包中拷贝ehcache.xml,hibernate.cfg.xml,log4j.properties

QueryTest.java

  1. /*
  2. * QueryTest.java
  3. *
  4. * Created on 2006年4月26日, 下午12:22
  5. *
  6. * Copyright (c) 2006 Wuxi MDC Co. Ltd.
  7. * All right reserved.
  8. */

  9. import com.mdc.*;
  10. import org.hibernate.cfg.*;
  11. import org.hibernate.*;
  12. import java.util.*;

  13. /**
  14. *
  15. * @author Chen QingHui
  16. */
  17. public class QueryTest
  18. {
  19.    
  20.     /** Creates a new instance of QueryTest */
  21.     public QueryTest()
  22.     {
  23.     }

  24.     public static void main(String[] args)
  25.     {
  26.         try
  27.         {
  28.             SessionFactory sf = new Configuration().configure().buildSessionFactory();
  29.             Session session = sf.openSession();
  30.             List users = session.createQuery("from User".list();
  31.             session.close();
  32.             sf.close();

  33.             for (ListIterator iterator = users.listIterator(); iterator.hasNext(); )
  34.             {
  35.                 User user = (User) iterator.next();
  36.                 System.out.println("Id:" + user.getId() +
  37.                                    "nt名子: " + user.getName() +
  38.                                    "nt密碼: " + user.getPass());
  39.             }
  40.         }
  41.         catch (HibernateException he)
  42.         {
  43.             he.printStackTrace();
  44.         }
  45.     }
  46.    
  47. }

ehcache.xml,hibernate.cfg.xml,log4j.properties只要拷贝到src下,就会自动出现在中了,需要修改hibernate.cfg.xml如下
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. "">

  5. <hibernate-configuration>

  6.     <session-factory>
  7.         <!-- properties -->
  8.         <property name="connection.username">ff</property>
  9.         <property name="connection.url">jdbc:oracle:thin:@192.168.150.200:1521:yxtnj</property>
  10.         <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
  11.         <property name="connection.password">chen</property>
  12.         <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>

  13.         <!-- mapping files -->
  14.         <mapping resource="com/mdc/User.hbm.xml"/>

  15.     </session-factory>

  16. </hibernate-configuration>


5.项目结构如下



最后Build Project,然后run就行了。但事先要准备好数据哦

附:表结构

  1. CREATE TABLE USERTABLE (
  2. ID NUMBER NOT NULL,
  3. USERNAME VARCHAR2(24),
  4. PASSWORD VARCHAR2(24));
序列
  1. CREATE SEQUENCE SEQ_USERTABLE_ID MAXVALUE 1000000000 START WITH 1;
阅读(519) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~