Hibernate入门
1.登录http://www.hibernate.org/downloads.html站点,下载HibernateCore,目前最新版本是3.3.1.GA,Windows平台下载.zip包(hibernate-distribution-
3.3.1.GA-dist.zip).将下载的.zip包解压.
2.鼠标右击工程项目,选择properties-->JavaBuildPath-->Libraries,点击AddExternalJars,把10个jar包导入进来.10个jar包如下:
antlr-2.7.6.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
ehcache-1.2.3.jar
hibernate3.jar
hibernate-cglib-repack-2.1_3.jar
javassist-3.4.GA.jar
jta-1.1.jar
slf4j-api-1.5.2.jar
slf4j-nop-1.5.2.jar(注:需要单独下载slf4j-1.5.2.zip,解压后就有slf4j-nop-1.5.2.jar)
3.编写hibernate.cfg.xml文件,将其放在项目的src目录下.
<?xmlversion='1.0'encoding='utf-8'?>
<!DOCTYPEhibernate-configurationPUBLIC
"-//Hibernate/HibernateConfigurationDTD3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--Databaseconnectionsettings(MicroSoftSQLSERVER2000)-->
<propertyname="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>
<propertyname="connection.url">jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=cdh</property>
<propertyname="connection.username">sa</property>
<propertyname="connection.password">1234</property>
<!--SQLdialect-->
<propertyname="dialect">org.hibernate.dialect.SQLServerDialect</property>
<propertyname="show_sql">true</property>
<!--mypack/User.hbm.xml是我接下来要创建的配置文件-->
<mappingresource="mypack/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4.编写POJO.
packagemypack;
importjava.util.Date;
classUser{
privateintid;
privateStringname;
privateDatebirthday;
publicDategetBirthday(){
returnbirthday;
}
publicvoidsetBirthday(Datebirthday){
this.birthday=birthday;
}
publicintgetId(){
returnid;
}
publicvoidsetId(intid){
this.id=id;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
}
5.编写POJO的配置文件(User.hbm.xml),将其放在POJO同一个包下.
<?xmlversion="1.0"?>
<!DOCTYPEhibernate-mappingPUBLIC
"-//Hibernate/HibernateMappingDTD3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mappingpackage="mypack">
<classname="User"table="USERS">
<comment>Usersmaybidfororsellauctionitems.</comment>
<idname="id">
<generatorclass="increment"/>
</id>
<propertyname="name"/>
<propertyname="birthday"/>
</class>
</hibernate-mapping>
6.在数据库中创建USERS表.
usecdh
CREATETABLEusers(
idintNOTNULLPRIMARYKEY,
namevarchar(20),
birthdaydatetime
)
7.编写测试代码.
packagemypack;
importjava.util.Date;
importorg.hibernate.Session;
importorg.hibernate.SessionFactory;
importorg.hibernate.Transaction;
importorg.hibernate.cfg.Configuration;
publicclassUserAppliction{
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
Configurationconfig=newConfiguration();
config.configure();
SessionFactorysf=config.buildSessionFactory();
Sessionsession=sf.openSession();
Transactiontx=null;
Useruser=newUser();
user.setName("Phil");
user.setBirthday(newDate());
try{
tx=session.beginTransaction();
session.save(user);
tx.commit();
}catch(Exceptione){
if(tx!=null){
tx.rollback();
}
throwe;
}finally{
session.close();
}
System.out.println("TestSuccess!");
}
}
运行结果:
Hibernate:selectmax(id)fromUSERS
Hibernate:insertintoUSERS(name,birthday,id)values(?,?,?)
TestSuccess!
现在可以在查询分析器中写以下查询语句查看刚插入的数据.
usecdh
select*fromusers