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();
    }

   }
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值