经过一天的摸索,终于调通了第一个Hibernate程序,非常的激动,赶快记录下来我的实践过程。
开发环境:eclipse3.21 + myeclipse(Hibernate3.0) + jdk1.5.0 + sql server2000
第一步: 建立数据库和表。
表名:T_account
Fid int 主键 自增量
Fname varchar 50
Fonhand int
Ftype varchar 30
Fcreatetime datetime
第二步:下载sql server 2000 jar 包 下载地址http://download.microsoft.com/download/SQLSVR2000/Install/2.2.0022/UNIX/EN-US/mssqlserver.tar解压后取出msbase.jar / mssqlserver.jar / msutil.jar 存放到一个目录下
第三步:启动eclipse 打开菜单: window --> preferences --> java --> Build path --> user Libraries 点new ,输入jdbc名称:jdbc_sqlserver, 选中刚建好的jdbc,点 Add JARs 选中第二步准备好的三jar文件。点OK关闭。
第四步:新建一个java工程。右键点工程,选preferences ,点java Build path ,在右边选Libraries ,点Add Libraries ,选 User Libraries , 点next ,选第三步建好的jdbc名称。点OK
第五步:在工程中建立SRC 源程序目录,在目录下建立T_account表对应的实体accountInfo.java文件. (本例是用cownewstudio1.0插件完成这步)如下:
package com.nettwn.finance;
public class accountInfo
{
private int id;
private String name;
private int onhand;
private String type;
private java.sql.Date createtime;
public void setid(int value)
{
this.id=value;
}
public void setname(String value)
{
this.name=value;
}
public void setonhand(int value)
{
this.onhand=value;
}
public void settype(String value)
{
this.type=value;
}
public void setcreatetime(java.sql.Date value)
{
this.createtime=value;
}
public int getid()
{
return id;
}
public String getname()
{
return name;
}
public int getonhand()
{
return onhand;
}
public String gettype()
{
return type;
}
public java.sql.Date getcreatetime()
{
return createtime;
}
}
第六步,创建实体类文件的accountInfo.hdm.xml文件。类与表的映射就是这里进行的。(本例是通过cownewstudio1.1建立并稍加修改)
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="com.nettwn.finance.accountInfo" table="T_account">
<id name="id" type="int" column="Fid" >
<generator class="identity"/>
</id>
<property name="name" type="string" column="Fname" length="50"/>
<property name="onhand" type="int" column="Fonhand" />
<property name="type" type="string" column="Ftype" length="50"/>
<property name="createtime" type="date" column="Fcreatetime"/>
</class>
</hibernate-mapping>
第七步:创建本工程的配置文件hibernate.cfg.xml 这一步比较重要,数据库的配置就是在这里完成。实现的方法为:右键点工程,选myEclipse,选Add web 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.username">sa</property>
<property name="connection.url">
jdbc:microsoft:sqlserver://192.168.0.45:1433;DatabaseName=asdfg
</property>
<property name="dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<property name="connection.password">
*************** </property>
<property name="connection.driver_class">
com.microsoft.jdbc.sqlserver.SQLServerDriver
</property>
<mapping resource="com/nettwn/finance/accountInfo.hbm.xml" />
</session-factory>
</hibernate-configuration>
第八步:建一个java程序测试是否可以通过hibernate进行插入一条数据。文件名为insertAccount.java,以上所做的所有工作都是为了在这里使用方便,看到了吗?不用写一句SQL就完成了一条插入操作。很神奇!
package com.nettwn.finance;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.*;
import org.hibernate.classic.Session;
public class insertAccount
{
public static void main(String[] args) {
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
accountInfo acc = new accountInfo();
//acc.setid(10);
acc.setname("gongshang");
acc.setonhand(45000);
acc.settype("A");
acc.setcreatetime(java.sql.Date.valueOf("2007-1-22"));
System.out.println(acc + "_____________10");
try
{
session.save(acc);
tx.commit();
session.close();
System.out.println(acc + "_____________9999");
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}