Hibernate入门体验
来源:互联网 发布:app制作编程教学 编辑:程序博客网 时间:2024/05/21 16:54
在java项目开发中,对于DAO层的持久化框架非常的多,流行的Hibernate,Tooplink,OJB等等。Hibernate是一个全自动化的持久行框架,功能非常强大。要想用Hibernate进行开发,首先必须下载其jar包,这里用的是hibernate-3.2.5版本的。
开发步骤如下:
1.新建一个java项目,项目名称为hibernateTest,在项目下新建一个lib文件夹,并将下载的jar包中lib文件夹中的所以jar包拷贝到新建的lib文件夹中,然后将拷贝jar包添加到项目中,同时不要忘记需要数据库驱动的jar包。
2.在src目录下新建一个hibernate.cfg.xml的xml文件。该配置文件配置了很多Hibernate框架工作必须的信息。包括数库连接信息,连接池,映射文件等等
<!--hibernate.hbm2ddl.auto 当数据库中没有表时,创建表 取值除了create之外,还有create-drop,update,validate等-->
<!--dialect 配置方言 不同的数据库都存在一定的差异 通过该配置项 Hibernate会针对不同的数据库多相应操作 这里的数据库为oracle数据库-->
<!--show_sql 该配置用来控制是否显示Hibernate执行的sql语句 true为显示sql语句-->
<!--mapping 指定关系映射文件的位置-->
<?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">
<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">scott</property>
<property name="connection.password">tiger</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="show_sql">true</property>
<mapping resource="com/hotmail/silence/domain/Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>
2.建立实体,在src下新建一个com.hotmail.silence.domain的包 ,在该包中建立一个Person类。
注意:(1). Person类就是一个标准的javaBean。也就是说,要有字段和相应的的getter和setter方法。
(2).有一个id属性。
(3).必须要用一个无参数构造方法。
代码如下:
package com.hotmail.silence.domain;public class Person {
//实体类中一般都有一个id属性 用来做主键
private int id; private String name; private int age; private IdCard idCard;public int getId() {return id;}public void setId(int id) {this.id = id;}public IdCard getIdCard() {return idCard;}public void setIdCard(IdCard idCard) {this.idCard = idCard;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;} }
3.建立映射文件,在Person类的同一目录下,新建一个Person.hbm.xml文件
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC '-//Hibernate/Hibernate Mapping DTD 3.0//EN' 'http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd'>
<!--package属性用来指定包名,-->
<hibernate-mapping package="com.hotmail.silence.domain">
<!--name属性用来指定类名称 要和类名一致。table属性用来指定映射生成的表名称 默认和类名称一致-->
<class name="Person" table="person">
<!--对应Person类的id属性,在表中生成id字段 Hibernate框架中有一个native类(采用自增长)会自动给id赋值-->
<id name="id"> <generator class="native"/> </id>
<!--对应Person类的name属性 在表中会生成name字段-->
<property name="name"></property><property name="age"></property>
</class></hibernate-mapping>
在hibernate.cfg.xml文件中加入 <mapping resource="com/hotmail/silence/domain/Person.hbm.xml"/> 参见hibernate.cfg.xml文件
4.新建一个含有main方法的测试类。进行测试。
在src目录下新建一个com.hotmail.silence.test包并新建一个Test1类。
代码如下:
package com.hotmail.silence.test;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import com.hotmail.silence.domain.Person;public class Test1 {/** * @param args */public static void main(String[] args) {
try{
SessionFactory sf = new Configuration().configure().buildSessionFactory(); Session session = sf.openSession(); org.hibernate.Transaction transaction = session.beginTransaction(); Person p = new Person();p.setName("name");p.setAge(10);session.save(p); transaction.commit();} catch (Exception e) {transaction.rollback();
e.printStackTrace();}finally{
if(session != null)
session.close();}}}
运行Test1 会输出:
Hibernate: create sequence hibernate_sequenceHibernate: select hibernate_sequence.nextval from dualHibernate: insert into tbl_person (name, age, id) values (?, ?, ?)
进入数据库 执行select * from person
即可查询出数据。
Hibernate: create sequence hibernate_sequence2014-10-14 18:12:48 org.hibernate.tool.hbm2ddl.SchemaExport executeINFO: HHH000230: Schema export completeHibernate: select hibernate_sequence.nextval from dualHibernate: insert into tbl_person (name, age, id) values (?, ?, ?)
Hibernate: create sequence hibernate_sequence
2014-10-14 18:12:48 org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into tbl_person (name, age, id) values (?, ?, ?)Hibernate: create sequence hibernate_sequence
2014-10-14 18:12:48 org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into tbl_person (name, age, id) values (?, ?, ?)
- Hibernate入门体验
- Hibernate --Hibernate的入门体验
- 体验Hibernate
- 体验hibernate
- hibernate学习笔记-1入门初体验对象持久化
- hibernate学习笔记-2入门初体验关联映射
- 10分钟Hibernate体验
- Webwork2+Spring+Hibernate体验
- 10分钟Hibernate体验
- Hibernate的初体验
- 初步体验Hibernate
- Hibernate整体体验
- Hibernate的初步体验。
- hibernate初体验
- Hibernate初体验
- Hibernate Search 初体验
- Hibernate初体验
- Hibernate之初体验
- iOS绘图机制2
- maven 教程一 入门
- Linux驱动面试的几个问题
- memached介绍_java
- 谈谈Winform程序的界面设计
- Hibernate入门体验
- Could not reload resource bundles java.lang.NoSuchFieldException: cacheList
- Linux的文本编辑器
- js setTimeOut() 方法传参
- UI_本地存储
- css 实现自定义下拉框样式
- jQuery(三) jQuery Html
- RNN 2:反馈神经网络的解决OXR问题
- NSNotification 通知传值