hibernate之Hello world

step 1)工具下载

1.1)下载hibernate-3.0

1.2)下载ojdbc14_g.jar(oracle 9i的驱动)

1.3)在eclipse环境下建立两个库(windows-->preferences-->java-->buildpath-->user library) hibernate(hibernate-3.0/lib下所有jar),jdbc(包括ojdbc14_g.jar)

step2)具体开发

建立一个 项目 ,引入刚才建立的两个库

2.1)

建一个src源文件目录,输出路径设为classes,在src下建立hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<!-- 是否将运行期生成的SQL输出到日志以供调试 -->
<property name="show_sql">true</property>

<!-- SQL方言,这里设定的是MySQL -->
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>

<!-- JDBC驱动程序 -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>

<!-- JDBC URL, "?useUnicode=true&amp;characterEncoding=GBK" 表示使用GBK进行编码 -->
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:oracle9i
</property>

<!-- 数据库用户名 -->
<property name="connection.username">pwlazy</property>

<!-- 数据库密码 -->
<property name="connection.password">770216</property>
<!-- 指定User的映射文件 -->
<mapping resource="hello/message.hbm.xml"/>
</session-factory>
</hibernate-configuration>

注意dtd一定要写对,否则会抛错!

2.2)

在数据库建立一个messages表

-- Create table
create table MESSAGES
(
ID NUMBER not null,
NEXT_ID NUMBER,
TEXT VARCHAR2(20)
)
tablespace OEM_REPOSITORY
pctfree 10
pctused 40
initrans 1
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);
-- Create/Recreate primary, unique and foreign key constraints
alter table MESSAGES
add constraint KEY primary key (ID)
using index
tablespace OEM_REPOSITORY
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);

2.3)写POJO,与该表相关

package hello;

/**
* @author Administrator
*
*
*/
public class Message {
private Long id;
private String text;
private Message nextMessage;
private Message() {}
public Message(String text) {
this.text = text;
}
public Long getId() {
return id;
}
private void setId(Long id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Message getNextMessage() {
return nextMessage;
}
public void setNextMessage(Message nextMessage) {
this.nextMessage = nextMessage;
}
}

2.4)写POJO的映射文件

<?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="hello.Message" table="MESSAGES">
<id name="id"
column="ID">
<generator class="increment"/>
</id>
<property
name="text"
column="TEXT"/>
<many-to-one
name="nextMessage"
cascade="all"
column="NEXT_ID"/>
</class>
</hibernate-mapping>

注意dtd一定要写对,否则会抛错!

2.5)写客户代码

package hello;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

/**
* @author Administrator
*
*
*/

public class Test {
public static void main(String[] args) {
try {
SessionFactory sf = new Configuration().configure()
.buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
Message message = new Message("Hello");
session.save(message);

tx.commit();
session.close();
} catch (HibernateException e) {
e.printStackTrace();
}

}
}

即望数据库中插入一条记录

你可能感兴趣的:(oracle,sql,mysql,Hibernate,jdbc)