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();
}
}
分享到:
相关推荐
一个简单的hibernate helloworld 下载下来就能用
用Spring MVC+hibernate 写的一个HelloWorld小实例,加入了一个简单验证。
hibernate的helloworld版。下载下来可以直接运行。
Hibernate教程01_Hibernate的HelloWorld及基本配置 http://blog.csdn.net/e421083458/article/details/8794127 该源码为Hibernate教程配套源码
Hibernate 4——Hello World
hibernate-helloworld-native
Hibernate HelloWorld 6 1 建立新java 项目,名为hibernate_0100_HelloWorld 6 2 学习建User-library-hibernate,并加入相应的jar包 6 3 引入mysql的JDBC驱动包 6 4 在mysql中建立对应的数据库以及表 6 5 建立...
第6课 第一个示例Hibernate HelloWorld 7 第7课 建立Annotation版本的HellWorld 9 第8课 什么是O/R Mapping 11 一、 定义: 11 二、 Hibernate的创始人: 11 三、 Hibernate做什么: 12 四、 Hibernate存在的原因: ...
如果你没学过hibernate,但是又想看一下它的最简单形式。这个应该适合你。
建立新的java项目 名为:hibernate_0100_HelloWorld 2. 学习建立User-hibernate 并加入相应的包 a)项目右键build path-configure build path-add library b)选择User-library,在其中新建library 命名为hibernate...
Hibernate 4.0版helloworld,项目需要Maven管理库依赖,项目包含Oracle建表语句
NULL 博文链接:https://huangminwen.iteye.com/blog/1312068
版本差异太难搞了,找半天才找到错误,主要是包冲突 NoSuchMethodError: org.jboss....NoClassDefFoundError: org/hibernate/cache/spi/RegionFactory org.hibernate.cache.spi.RegionFactory 记录下来,以备不时
Hibernate01-简介和HelloWorld 例子代码,实现了简单新增,测试类需要引进junit4
Hibernate_HelloWorld 使用Netbeans和SQL Server的HelloWorld Hibernate
Hibernate_annotation_学习笔记 第1课 课程内容 6 第2课 Hibernate UML图 6 第3课 风格 7 ...第6课 第一个示例Hibernate HelloWorld 7 第7课 建立Annotation版本的HellWorld 9 第8课 什么是O/R Mapping 11 ....
ssh_helloworld(初学参考),初学者学习的例子,供学习参考
hibernate 使用及与oracle连接的方式
这是hibernate 的超级简单的例子,只有一个持久化对象和一个辅助类,还有一个测试类,对于初学者很有参考价值
主要介绍了Hibernate的Annotation版Hello world实现方法,详细分析了Annotation的具体使用步骤与Hello world实现方法,需要的朋友可以参考下