Hibernate HelloWorld

Hibernate是一个数据持久化的开源框架,主要是负责将对象映射为二维表中的数据。
简单的HelloWorld大体步骤如下:
1、建立表
2、建立与表对应的实体类
3、写映射文件
4、写hibernate配置文件
5、测试类引入接口进行学习和测试。

具体步骤及代码如下:
1、建立表
create table T_MESSAGE
(
ID NUMBER(10) not null primary key,
NAME VARCHAR2(255)
)
2、建立与表对应的实体类
package helloworld;

public class Message {
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;
}

}

3、写映射文件

<?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="helloworld.Message" table="t_message">
<id name="id" column="id">
<generator class="sequence">
<param name="id_seq"></param>
</generator>
</id>
<property name="name" type="string" column="name"/>
</class>


</hibernate-mapping>

4、写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">

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>

<session-factory>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:orcl
</property>
<property name="connection.username">tysp</property>
<property name="connection.password">12345678</property>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="dialect">
org.hibernate.dialect.OracleDialect
</property>

<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="helloworld/Message.hbm.xml"/>

</session-factory>

</hibernate-configuration>

5、测试类引入接口进行学习和测试。

package helloworld;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

public class HelloWorld {

@Test
public void testSave(){
System.out.println("Let us begin!");
Configuration config = new Configuration().configure("/hibernate.cfg.xml");
//Configuration config = new Configuration().configure();
SessionFactory sf = config.buildSessionFactory();
System.out.println(sf);
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
tx.begin();
Message msg = new Message();
msg.setName("sss");
session.save(msg);
tx.commit();
}

public static void main(String[] args){
new HelloWorld().testSave();
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值