hibernate4.2.4之环境搭建与测试实例
2013-08-14 15:03
417 查看
1.下载hibernate-release-4.2.4.Final.zip从点击打开链接(http://www.hibernate.org/downloads)下载
2.在eclipse中新建一个javaproject工程,名为hibernate_first,在hibernate_first中新建一个ilb文件夹,
解压下载的hibernate-release-4.2.4.Final.zip,打开之后有几个文件夹lib---jar库documentation--文档说明projetc---关于hibernate的配置/例子等等之类的
进入到hibernate-release-4.2.4.Final\lib\required\目录下将8个必须的jar包拷贝到hibernate_first\lib\下面并且将8个jar包单击右键选中--BuildPath--AddtoBuildPath(即添加为工程引用)
同时将mysql-connector-java-5.1.25-bin.jar添加lib包下
3.编写配置文件进入到hibernate-release-4.2.4.Final\project\etc\目录下将hibernate.cfg.xml文件拷贝到项目hibernate_first\src\目录下
将hibernate.cfg.xml文件修改为如下;
<!DOCTYPEhibernate-configurationPUBLIC
"-//Hibernate/HibernateConfigurationDTD3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<propertyname="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<propertyname="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<propertyname="hibernate.connection.username">root</property>
<propertyname="hibernate.connection.password">root</property>
<propertyname="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<propertyname="hbm2ddl.auto">create</property>
<propertyname="hibernate.show_sql">true</property>
<mappingresource="com/undergrowth/hibernate/domain/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
简单解释一下上面的hibernate.cfg.xml文件
connection.driver_class/connection.url/connection.username/connection.password是用于mysql连接的驱动/资源定位符/登陆账号/登陆密码
hibernate.dialect是hibernate用于兼容多种数据库而做的,告诉hibernate使用的是哪一种数据库
hibernate.show_sql是在进行相应的hibernate操作时,会在控制台输出相应的sql语句
hbm2ddl.auto是在hibernate第一次进行操作时都会自己创建一个表
mappingresource用于指明对象与表的映射关系这里指的是Student类和学生表的一个映射文件下面也会介绍到
上面的这些属性其实不用记在hibernate-release-4.2.4.Final\project\etc\hibernate.properties文件中都可以找到
packagecom.undergrowth;
importjava.util.Date;
publicclassStudent{
privateintid;
publicintgetId(){
returnid;
}
publicvoidsetId(intid){
this.id=id;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicDategetBirthday(){
returnbirthday;
}
publicvoidsetBirthday(Datebirthday){
this.birthday=birthday;
}
publicStringgetMajor(){
returnmajor;
}
publicvoidsetMajor(Stringmajor){
this.major=major;
}
privateStringname;
privateDatebirthday;
privateStringmajor;
publicStudent(Stringname,Datebirthday,Stringmajor){
this.name=name;
this.birthday=birthday;
this.major=major;
}
publicStudent(){}//用于给hibernate的反射使用
@Override
publicStringtoString(){
return"Student[id="+id+",name="+name+",birthday="
+birthday+",major="+major+"]";
}
}
5.测试
packagecom.undergrowth.inter;
importcom.undergrowth.Student;
publicinterfaceStudentDaoInter{
publicvoidsaveStudent(Studentstudent);
publicvoidupdateStudent(Studentstudent);
publicvoiddeleteStudent(Studentstudent);
publicStudentfindIdStudent(intid);
publicStudentfindNameStudent(Stringname);
}
packagecom.undergrowth.imple;
importcom.undergrowth.Student;
importcom.undergrowth.hibernate.utils.HibernateUtils;
importcom.undergrowth.inter.StudentDaoInter;
publicclassStudentDaoImpimplementsStudentDaoInter{
@Override
publicvoidsaveStudent(Studentstudent){
//TODOAuto-generatedmethodstub
HibernateUtils.add(student);
}
@Override
publicvoidupdateStudent(Studentstudent){
//TODOAuto-generatedmethodstub
HibernateUtils.update(student);
}
@Override
publicvoiddeleteStudent(Studentstudent){
//TODOAuto-generatedmethodstub
HibernateUtils.delete(student);
}
@Override
publicStudentfindIdStudent(intid){
//TODOAuto-generatedmethodstub
return(Student)HibernateUtils.get(Student.class,id);
}
@Override
publicStudentfindNameStudent(Stringname){
//TODOAuto-generatedmethodstub
return(Student)HibernateUtils.get(name);
}
}
packagecom.undergrowth.hibernate.utils;
importjava.io.Serializable;
importorg.hibernate.HibernateException;
importorg.hibernate.Query;
importorg.hibernate.Session;
importorg.hibernate.SessionFactory;
importorg.hibernate.Transaction;
importorg.hibernate.cfg.Configuration;
publicclassHibernateUtils{
privatestaticSessionFactorysf;
static{
Configurationcfg=newConfiguration();
cfg.configure();
sf= cfg.buildSessionFactory();
}
publicstaticSessiongetSession()
{
returnsf.openSession();
}
publicstaticvoidadd(Objectentity)
{
Sessionsession=null;
Transactiontx=null;
try{
session=HibernateUtils.getSession();
tx=session.beginTransaction();
session.save(entity);
tx.commit();
}catch(HibernateExceptione){
throwe;
}finally{
if(session!=null)
session.close();
}
}
publicstaticvoiddelete(Objectentity)
{
Sessionsession=null;
Transactiontx=null;
try{
session=HibernateUtils.getSession();
tx=session.beginTransaction();
session.delete(entity);
tx.commit();
}catch(HibernateExceptione){
throwe;
}finally{
if(session!=null)
session.close();
}
}
publicstaticvoidupdate(Objectentity)
{
Sessionsession=null;
Transactiontx=null;
try{
session=HibernateUtils.getSession();
tx=session.beginTransaction();
session.update(entity);
tx.commit();
}catch(HibernateExceptione){
throwe;
}finally{
if(session!=null)
session.close();
}
}
publicstaticObjectget(Classclazz,Serializableid)
{
Sessionsession=null;
try{
session=HibernateUtils.getSession();
Objectobj=session.get(clazz,id);
returnobj;
}catch(HibernateExceptione){
throwe;
}finally{
if(session!=null)
session.close();
}
}
publicstaticObjectget(Stringname)
{
Sessionsession=null;
try{
session=HibernateUtils.getSession();
Queryquery=session.createQuery("fromStudentasstudentwherename=:name");
query.setParameter("name",name);
Objectobj=query.uniqueResult();
returnobj;
}catch(HibernateExceptione){
throwe;
}finally{
if(session!=null)
session.close();
}
}
}
packagecom;
importjava.util.Date;
importcom.undergrowth.Student;
importcom.undergrowth.imple.StudentDaoImp;
publicclassHibernateTest{
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
StudentDaoImpimp=newStudentDaoImp();
//添加
Studentstudent=newStudent("under",newDate(),"计算机");
System.out.println(student);
imp.saveStudent(student);
//查询
student=imp.findNameStudent("under");
System.out.println(student);
//修改
student.setMajor("电信");
imp.updateStudent(student);
//查询
student=imp.findNameStudent("under");
System.out.println(student);
//删除
imp.deleteStudent(student);
}
}
2.在eclipse中新建一个javaproject工程,名为hibernate_first,在hibernate_first中新建一个ilb文件夹,
解压下载的hibernate-release-4.2.4.Final.zip,打开之后有几个文件夹lib---jar库documentation--文档说明projetc---关于hibernate的配置/例子等等之类的
进入到hibernate-release-4.2.4.Final\lib\required\目录下将8个必须的jar包拷贝到hibernate_first\lib\下面并且将8个jar包单击右键选中--BuildPath--AddtoBuildPath(即添加为工程引用)
同时将mysql-connector-java-5.1.25-bin.jar添加lib包下
3.编写配置文件进入到hibernate-release-4.2.4.Final\project\etc\目录下将hibernate.cfg.xml文件拷贝到项目hibernate_first\src\目录下
将hibernate.cfg.xml文件修改为如下;
简单解释一下上面的hibernate.cfg.xml文件
connection.driver_class/connection.url/connection.username/connection.password是用于mysql连接的驱动/资源定位符/登陆账号/登陆密码
hibernate.dialect是hibernate用于兼容多种数据库而做的,告诉hibernate使用的是哪一种数据库
hibernate.show_sql是在进行相应的hibernate操作时,会在控制台输出相应的sql语句
hbm2ddl.auto是在hibernate第一次进行操作时都会自己创建一个表
mappingresource用于指明对象与表的映射关系这里指的是Student类和学生表的一个映射文件下面也会介绍到
上面的这些属性其实不用记在hibernate-release-4.2.4.Final\project\etc\hibernate.properties文件中都可以找到
4.编写对象与对象的映射文件引用上面的配置建立Student.java类如下
编写Student的ORM映射文件Student.hbm.xmlhibernate自带的模板很多这里引用hibernate-release-4.2.4.Final\project\documentation\src\main\docbook\quickstart\tutorials\basic\src\test\java\org\hibernate\tutorial\hbm\目录下的Event.hbm.xml
Student.hbm.xml
<?xmlversion="1.0"?>
<!DOCTYPEhibernate-mappingPUBLIC
"-//Hibernate/HibernateMappingDTD3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mappingpackage="com.undergrowth">
<classname="Student"table="student">
<idname="id">
<generatorclass="native"/>
</id>
<propertyname="name"/>
<propertyname="birthday"/>
<propertyname="major"/>
</class>
</hibernate-mapping>
5.测试
StudentDaoInter.java
importcom.undergrowth.Student;
publicinterfaceStudentDaoInter{
publicvoidsaveStudent(Studentstudent);
publicvoidupdateStudent(Studentstudent);
publicvoiddeleteStudent(Studentstudent);
publicStudentfindIdStudent(intid);
publicStudentfindNameStudent(Stringname);
}
StudentDaoImp.java
importcom.undergrowth.Student;
importcom.undergrowth.hibernate.utils.HibernateUtils;
importcom.undergrowth.inter.StudentDaoInter;
publicclassStudentDaoImpimplementsStudentDaoInter{
@Override
publicvoidsaveStudent(Studentstudent){
//TODOAuto-generatedmethodstub
HibernateUtils.add(student);
}
@Override
publicvoidupdateStudent(Studentstudent){
//TODOAuto-generatedmethodstub
HibernateUtils.update(student);
}
@Override
publicvoiddeleteStudent(Studentstudent){
//TODOAuto-generatedmethodstub
HibernateUtils.delete(student);
}
@Override
publicStudentfindIdStudent(intid){
//TODOAuto-generatedmethodstub
return(Student)HibernateUtils.get(Student.class,id);
}
@Override
publicStudentfindNameStudent(Stringname){
//TODOAuto-generatedmethodstub
return(Student)HibernateUtils.get(name);
}
}
HibernateUtils.java
importjava.io.Serializable;
importorg.hibernate.HibernateException;
importorg.hibernate.Query;
importorg.hibernate.Session;
importorg.hibernate.SessionFactory;
importorg.hibernate.Transaction;
importorg.hibernate.cfg.Configuration;
publicclassHibernateUtils{
privatestaticSessionFactorysf;
static{
Configurationcfg=newConfiguration();
cfg.configure();
sf= cfg.buildSessionFactory();
}
publicstaticSessiongetSession()
{
returnsf.openSession();
}
publicstaticvoidadd(Objectentity)
{
Sessionsession=null;
Transactiontx=null;
try{
session=HibernateUtils.getSession();
tx=session.beginTransaction();
session.save(entity);
tx.commit();
}catch(HibernateExceptione){
throwe;
}finally{
if(session!=null)
session.close();
}
}
publicstaticvoiddelete(Objectentity)
{
Sessionsession=null;
Transactiontx=null;
try{
session=HibernateUtils.getSession();
tx=session.beginTransaction();
session.delete(entity);
tx.commit();
}catch(HibernateExceptione){
throwe;
}finally{
if(session!=null)
session.close();
}
}
publicstaticvoidupdate(Objectentity)
{
Sessionsession=null;
Transactiontx=null;
try{
session=HibernateUtils.getSession();
tx=session.beginTransaction();
session.update(entity);
tx.commit();
}catch(HibernateExceptione){
throwe;
}finally{
if(session!=null)
session.close();
}
}
publicstaticObjectget(Classclazz,Serializableid)
{
Sessionsession=null;
try{
session=HibernateUtils.getSession();
Objectobj=session.get(clazz,id);
returnobj;
}catch(HibernateExceptione){
throwe;
}finally{
if(session!=null)
session.close();
}
}
publicstaticObjectget(Stringname)
{
Sessionsession=null;
try{
session=HibernateUtils.getSession();
Queryquery=session.createQuery("fromStudentasstudentwherename=:name");
query.setParameter("name",name);
Objectobj=query.uniqueResult();
returnobj;
}catch(HibernateExceptione){
throwe;
}finally{
if(session!=null)
session.close();
}
}
}
HibernateTest.java
importjava.util.Date;
importcom.undergrowth.Student;
importcom.undergrowth.imple.StudentDaoImp;
publicclassHibernateTest{
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
StudentDaoImpimp=newStudentDaoImp();
//添加
Studentstudent=newStudent("under",newDate(),"计算机");
System.out.println(student);
imp.saveStudent(student);
//查询
student=imp.findNameStudent("under");
System.out.println(student);
//修改
student.setMajor("电信");
imp.updateStudent(student);
//查询
student=imp.findNameStudent("under");
System.out.println(student);
//删除
imp.deleteStudent(student);
}
}
打印的控制台信息
相关文章推荐
- hibernate4.2.4之环境搭建与测试实例
- 【Hibernate】—Hibernate+mysql 环境搭建+入门实例
- JPA(hibernate)开发环境的搭建和一个基本的实例
- activiti 入门实例与测试环境搭建
- 【SSH进阶之路】Hibernate搭建开发环境+简单实例(二)
- 【SSH进阶之路】【二】Hibernate搭建开发环境+简单实例
- 【hibernate进阶】hibernate搭建开发环境+实例讲解
- Hibernate搭建开发环境+简单实例(二)
- CVX 安装、环境搭建及实例测试
- Hibernate入门BLOG [一、搭建hibernate的开发环境和测试案例]
- Android开发实战一之搭建开发环境-附测试实例(已亲测)
- CVX 安装、环境搭建及实例测试
- Hibernate环境搭建以及测试(maven)
- 【SSH进阶之路】Hibernate搭建开发环境+简单实例(二)
- Hibernate(二):Hibernate搭建开发环境+简单实例
- (Hibernate进阶)Hibernate搭建开发环境+简单实例(二)
- Hibernate下搭建JUNIT的测试环境,使用beforeclass和afterclass实现sessionFactory建立一次
- 【SSH进阶之路】Hibernate搭建开发环境+简单实例(二)
- 【Hibernate】从入门到精通(二)环境搭建实例
- 新人入门linux环境搭建及shell文件运行测试实例