Hibernate入门

Hibernate入门

1.      Hibernate是一种数据持久化工具,是一个开放源代码的对象关系映射框架,内部封装了通过jdbc访问数据库的操作,向上层应用提供面向对象的数据访问api

2.      什么是ORMORM(Object/Relational Mapping)即对象关系映射,他在对象模型和关系型数据库之间建立的对应关系,让表和javabean对象对应

3.      Hibernate环境搭建

1)        下载架包,地址http://sourceforge.net/projects/hibernate/files/hibernate-annotations/本人使用hibernate-distribution-3.3.2.GA-dist.zip

2)        需要的架包,根目录下的hibernate3.jarlib\required下的必须jar是第三方的

3)        src目录下添加Hibernate配置文件(可在project/etc目录下找到实例文件)

4)        在对应的实体类下面添加hbm.xml文件

5)        如图

创建表

create table demo(
tid number(4) primary key,
tnum nvarchar2(20) not null,
tprice number(7,2),
tdate date
);
create sequence tid_seq
start with 1
increment by 1;
实体类
package cn.jbit.entity;
import java.util.Date;

public class Demo {
      private int tid;
      public int getTid() {
		return tid;
	}
	public void setTid(int tid) {
		this.tid = tid;
	}
	public String getTnum() {
		return tnum;
	}
	public void setTnum(String tnum) {
		this.tnum = tnum;
	}
	public double getTprice() {
		return tprice;
	}
	public void setTprice(double tprice) {
		this.tprice = tprice;
	}
	public Date getTdate() {
		return tdate;
	}
	public void setTdate(Date tdate) {
		this.tdate = tdate;
	}
	private String tnum;
      private double tprice;
      private Date tdate;
}<span style="font-family:Calibri;font-size:14px;"></span>

配置文件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 name="foo">
	<property name="dialect">org.hibernate.dialect.OracleDialect</property>
	<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
	<property name="connection.username">scott</property>
	<property name="connection.password">scott</property>
	<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
		<property name="show_sql">true</property>
		<mapping resource="cn/jbit/entity/Demo.hbm.xml"/>
	</session-factory>
</hibernate-configuration>

配置文件Demo.hbm.xml

<?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
	package="cn.jbit.entity">

	<class name="Demo" table="Demo">
		<id name="Tid" column="TID" type="java.lang.Integer">
			<generator class="sequence">
			<param name="sequence">tid_seq</param>
			</generator>
		</id>
		<property name="Tnum" not-null="true" column="TNUM" type="java.lang.String"/>
		<property name="Tprice" not-null="true" column="TPRICE" type="java.lang.Double"/>
		<property name="Tdate" not-null="true" column="TDate" type="java.util.Date"/>
	</class>

	

</hibernate-mapping>

测试类

package cn.jbit.test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import cn.jbit.entity.Demo;

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
       Configuration conf=new Configuration().configure();
       SessionFactory sf=conf.buildSessionFactory();
       Session session=sf.openSession();
       Demo demo=new Demo();
       Date date=new Date();
		try {
			date = new SimpleDateFormat("yyyy-MM-dd").parse("2008-5-4");
		} catch (ParseException e) {
			e.printStackTrace();
		}
       demo.setTdate(date);
       demo.setTnum("CDMA");
       demo.setTprice(650);
       Transaction tx=session.beginTransaction();
       //增加
       session.save(demo);
       //更新
       Demo demo2=(Demo)session.load(Demo.class,2);
       demo2.setTprice(800);
       session.update(demo2);
       //删除
      
          
       tx.commit();
       session.close();
	}

}


 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值