`

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics