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
/*
-
* User.java
-
*
-
* Created on 2006年4月26日, 下午12:06
-
*
-
* Copyright (c) 2006 Wuxi MDC Co. Ltd.
-
* All right reserved.
-
*/
-
-
package com.mdc;
-
-
/**
-
*
-
* @author Chen QingHui
-
*/
-
public class User
-
{
-
private int id;
-
private String username;
-
private String password;
-
-
/** Creates a new instance of User */
-
public User()
-
{
-
}
-
-
public int getId()
-
{
-
return id;
-
}
-
-
public String getName()
-
{
-
return username;
-
}
-
-
public String getPass()
-
{
-
return password;
-
}
-
-
public void setId(int id)
-
{
-
this.id = id;
-
}
-
-
public void setName(String username)
-
{
-
this.username = username;
-
}
-
-
public void setPass(String password)
-
{
-
this.password = password;
-
}
-
-
}
User.hbm.xml
- <?xml version='1.0' encoding='UTF-8'?>
-
<!DOCTYPE hibernate-mapping PUBLIC
-
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
-
"">
-
-
<hibernate-mapping>
-
-
<class name="com.mdc.User" table="UserTable">
-
<id name="id" type="int">
-
<column name="id" />
-
<generator class="sequence">
-
<param name="sequence">seq_usertable_id</param>
-
</generator>
-
</id>
-
<property name="name">
-
<column name="username" />
-
</property>
-
<property name="pass">
-
<column name="password" />
-
</property>
-
</class>
-
-
</hibernate-mapping>
4.在
中,完成QueryTest.java,并在hibernate的发行包中拷贝ehcache.xml,hibernate.cfg.xml,log4j.properties
QueryTest.java
- /*
-
* QueryTest.java
-
*
-
* Created on 2006年4月26日, 下午12:22
-
*
-
* Copyright (c) 2006 Wuxi MDC Co. Ltd.
-
* All right reserved.
-
*/
-
-
import com.mdc.*;
-
import org.hibernate.cfg.*;
-
import org.hibernate.*;
-
import java.util.*;
-
-
/**
-
*
-
* @author Chen QingHui
-
*/
-
public class QueryTest
-
{
-
-
/** Creates a new instance of QueryTest */
-
public QueryTest()
-
{
-
}
-
-
public static void main(String[] args)
-
{
-
try
-
{
-
SessionFactory sf = new Configuration().configure().buildSessionFactory();
-
Session session = sf.openSession();
-
List users = session.createQuery("from User".list();
-
session.close();
-
sf.close();
-
-
for (ListIterator iterator = users.listIterator(); iterator.hasNext(); )
-
{
-
User user = (User) iterator.next();
-
System.out.println("Id:" + user.getId() +
-
"nt名子: " + user.getName() +
-
"nt密碼: " + user.getPass());
-
}
-
}
-
catch (HibernateException he)
-
{
-
he.printStackTrace();
-
}
-
}
-
-
}
ehcache.xml,hibernate.cfg.xml,log4j.properties只要拷贝到src下,就会自动出现在中了,需要修改hibernate.cfg.xml如下
hibernate.cfg.xml
- <?xml version='1.0' encoding='UTF-8'?>
-
<!DOCTYPE hibernate-configuration PUBLIC
-
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
-
"">
-
-
<hibernate-configuration>
-
-
<session-factory>
-
<!-- properties -->
-
<property name="connection.username">ff</property>
-
<property name="connection.url">jdbc:oracle:thin:@192.168.150.200:1521:yxtnj</property>
-
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
-
<property name="connection.password">chen</property>
-
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
-
-
<!-- mapping files -->
-
<mapping resource="com/mdc/User.hbm.xml"/>
-
-
</session-factory>
-
-
</hibernate-configuration>
5.项目结构如下
最后Build Project,然后run就行了。但事先要准备好数据哦
附:表结构
- CREATE TABLE USERTABLE (
-
ID NUMBER NOT NULL,
-
USERNAME VARCHAR2(24),
-
PASSWORD VARCHAR2(24));
序列
- CREATE SEQUENCE SEQ_USERTABLE_ID MAXVALUE 1000000000 START WITH 1;
阅读(518) | 评论(0) | 转发(0) |