hibernate第一课Word下载.docx
《hibernate第一课Word下载.docx》由会员分享,可在线阅读,更多相关《hibernate第一课Word下载.docx(22页珍藏版)》请在冰豆网上搜索。
创建数据库之间的会话对象Session,以单态方式出现
(3)Session
数据库会话接口,主要用于操作实体对象
(4)、事务
由Session对象获取,用于保持操作的事务特性(ACID)
(5)、查询接口(Query)
主要用于对数据库中的数据通过实体对象进行查询。
由Session对象创建.
四、Hibernate的操作步骤
库和表
createdatabasemydb
go
usemydb
createtableadmin(
aidintidentityprimarykey,
anamevarchar(50),
passwdvarchar(50)
)
select*fromadmin
1、在Myeclipse中建立数据库访问环境
2、在当前工程中引入Hibernate组件包
3、生成实体类和Hibernate映射文件
生成的实体类对象
packagecom.po;
/**
*Adminentity.@authorMyEclipsePersistenceTools
*/
publicclassAdminimplementsjava.io.Serializable{
//Fields
privateIntegeraid;
privateStringaname;
privateStringpasswd;
//Constructors
/**defaultconstructor*/
publicAdmin(){
}
/**fullconstructor*/
publicAdmin(Stringaname,Stringpasswd){
this.aname=aname;
this.passwd=passwd;
//Propertyaccessors
publicIntegergetAid(){
returnthis.aid;
publicvoidsetAid(Integeraid){
this.aid=aid;
publicStringgetAname(){
returnthis.aname;
publicvoidsetAname(Stringaname){
publicStringgetPasswd(){
returnthis.passwd;
publicvoidsetPasswd(Stringpasswd){
}
4、在测试类中创建Session对象
5、使用Session对象创建事务对象
6、给实体对象赋值
7、使用session对象操作实体对象
8、提交事务
9、关闭Session对象
TestSave.java
packagecom.test;
importjava.util.*;
importcom.po.*;
importorg.hibernate.*;
publicclassTestSave{
publicstaticvoidmain(String[]args){
//创建数据库会话对象
Sessionsession=HibernateSessionFactory.getSession();
//创建事务对象
Transactiontx=session.beginTransaction();
//创建实体对象
Adminadmin=newAdmin();
admin.setAname("
张三"
);
admin.setPasswd("
123456"
try{
//保存数据
session.save(admin);
//提交数据
mit();
System.out.println("
保存成功!
"
}catch(HibernateExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}finally{
HibernateSessionFactory.closeSession();
}
五、生成的Admin.hbm.xml文件
<
?
xmlversion="
1.0"
encoding="
utf-8"
>
!
DOCTYPEhibernate-mappingPUBLIC"
-//Hibernate/HibernateMappingDTD3.0//EN"
--
MappingfileautogeneratedbyMyEclipsePersistenceTools
-->
hibernate-mapping>
<
classname="
com.po.Admin"
table="
admin"
schema="
dbo"
catalog="
mydb"
idname="
aid"
type="
java.lang.Integer"
columnname="
/>
generatorclass="
identity"
/id>
propertyname="
aname"
java.lang.String"
length="
50"
/property>
passwd"
/class>
/hibernate-mapping>
六、生成的HibernateSessionFactory.java类
importorg.hibernate.HibernateException;
importorg.hibernate.Session;
importorg.hibernate.cfg.Configuration;
importorg.hibernate.cfg.AnnotationConfiguration;
*ConfiguresandprovidesaccesstoHibernatesessions,tiedtothe
*currentthreadofexecution.FollowstheThreadLocalSession
*pattern,see{@linkhttp:
//hibernate.org/42.html}.
publicclassHibernateSessionFactory{
/**
*Locationofhibernate.cfg.xmlfile.
*LocationshouldbeontheclasspathasHibernateuses
*#resourceAsStreamstylelookupforitsconfigurationfile.
*Thedefaultclasspathlocationofthehibernateconfigfileis
*inthedefaultpackage.Use#setConfigFile()toupdate
*thelocationoftheconfigurationfileforthecurrentsession.
privatestaticStringCONFIG_FILE_LOCATION="
/hibernate.cfg.xml"
;
privatestaticfinalThreadLocal<
Session>
threadLocal=newThreadLocal<
();
privatestaticConfigurationconfiguration=newAnnotationConfiguration();
privatestaticorg.hibernate.SessionFactorysessionFactory;
privatestaticStringconfigFile=CONFIG_FILE_LOCATION;
static{
configuration.configure(configFile);
sessionFactory=configuration.buildSessionFactory();
}catch(Exceptione){
System.err
.println("
%%%%ErrorCreatingSessionFactory%%%%"
privateHibernateSessionFactory(){
/**
*ReturnstheThreadLocalSessioninstance.Lazyinitialize
*the<
code>
SessionFactory<
/code>
ifneeded.
*
*@returnSession
*@throwsHibernateException
publicstaticSessiongetSession()throwsHibernateException{
Sessionsession=(Session)threadLocal.get();
if(session==null||!
session.isOpen()){
if(sessionFactory==null){
rebuildSessionFactory();
}
session=(sessionFactory!
=null)?
sessionFactory.openSession()
:
null;
threadLocal.set(session);
returnsession;
*Rebuildhibernatesessionfactory
publicstaticvoidrebuildSessionFactory(){
*Closethesinglehibernatesessioninstance.
publicstaticvoidcloseSession()throwsHibernateException{
threadLocal.set(null);
if(session!
=null){
session.close();
*returnsessionfactory
publicstaticorg.hibernate.SessionFactorygetSessionFactory(){
returnsessionFactory;
*sessionfactorywillberebuildedinthenextcall
publicstaticvoidsetConfigFile(StringconfigFile){
HibernateSessionFactory.configFile=configFile;
sessionFactory=null;
*returnhibernateconfiguration
publicstaticConfigurationgetConfiguration(){
returnconfiguration;