初见Hibernate(一)
基于XML配置文件实现的第一个hibernate小程序。
1.首先新建一个普通的java工程。
2.将oracle数据库驱动包和实现hibernate的最小包导入到该工程中。
Oracle数据库驱动包为:
Oracle 10g 10.2.0.4 JDBC_ojdbc14.jar
Hibernate的最小包为:
antlr-2.7.6.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
hibernate3.jar
javassist-3.4.GA.jar
jta-1.1.jar
slf4j-api-1.5.2.jar
slf4j-nop-1.5.2.jar
3.在src文件夹下建一个包名为com.neusoft.bean包,在该包下建立一个名为Person.java的文件,内容如下:
package com.neusoft.bean;
public class Person {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
再在该包下建立名为Person.hbm.xml的Person.java映射文件,内容如下:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.neusoft.bean.Person" table="person">
<id name="id" column="person_ID">
<generator class="native" />
</id>
<property name="name" column="person_name" />
</class>
</hibernate-mapping>
4.在src包下建立名为com.neusoft.test的包,然后在该包下建立名为Test.java的测试类。内容为:
package com.neusoft.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.neusoft.bean.Person;
public class Test {
public static void main(String[] args) {
Person p = new Person();
p.setId(1);
p.setName("wjl");
Configuration cfg = new Configuration();
SessionFactory sf = cfg.configure().buildSessionFactory();
Session s = sf.openSession();
s.beginTransaction();
s.save(p);
s.getTransaction().commit();
s.close();
sf.close();
}
}
5. 在src文件夹下建立名为hibernate.cfg.xml的hibernate的配置文件。内容如下:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@192.168.1.102:1521:oracle</property>
<property name="connection.username">hr</property>
<property name="connection.password">hr</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<mapping resource="com/neusoft/bean/Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>
6. 运行Test.java,控制台将显示如下内容:
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into person (person_name, person_ID) values (?, ?)
7.查看数据库,将看到生成了person表,并插入了一条记录。
×××××××××××××××××××××××××××××××
×××××××××××××××××××××××××××××××
作者“极致书生”
补充:软件开发 , Java ,