如何应用MyEclipse开发工具实现基于Hibenate框架的J2EE Web应用系统的开发第2部分.docx

上传人:b****8 文档编号:29368715 上传时间:2023-07-22 格式:DOCX 页数:17 大小:232.35KB
下载 相关 举报
如何应用MyEclipse开发工具实现基于Hibenate框架的J2EE Web应用系统的开发第2部分.docx_第1页
第1页 / 共17页
如何应用MyEclipse开发工具实现基于Hibenate框架的J2EE Web应用系统的开发第2部分.docx_第2页
第2页 / 共17页
如何应用MyEclipse开发工具实现基于Hibenate框架的J2EE Web应用系统的开发第2部分.docx_第3页
第3页 / 共17页
如何应用MyEclipse开发工具实现基于Hibenate框架的J2EE Web应用系统的开发第2部分.docx_第4页
第4页 / 共17页
如何应用MyEclipse开发工具实现基于Hibenate框架的J2EE Web应用系统的开发第2部分.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

如何应用MyEclipse开发工具实现基于Hibenate框架的J2EE Web应用系统的开发第2部分.docx

《如何应用MyEclipse开发工具实现基于Hibenate框架的J2EE Web应用系统的开发第2部分.docx》由会员分享,可在线阅读,更多相关《如何应用MyEclipse开发工具实现基于Hibenate框架的J2EE Web应用系统的开发第2部分.docx(17页珍藏版)》请在冰豆网上搜索。

如何应用MyEclipse开发工具实现基于Hibenate框架的J2EE Web应用系统的开发第2部分.docx

如何应用MyEclipse开发工具实现基于Hibenate框架的J2EEWeb应用系统的开发第2部分

如何应用MyEclipse开发工具实现基于Hibenate框架的J2EEWeb应用系统的开发(第2/2部分)

6、在示例项目中连接目标数据库系统

(1)右击所产生的profile名称

(2)在弹出的对话框中进行设置,并进行与数据库连接----但要保证数据库服务器已经启动了

(3)如果连接成功,将出现下面的内容

(4)产生Hibenate的数据库表的mapping文件----右击我们的数据库表

(5)将产生出下面的对话框,并在对话框中进行设置,包名称为com.px1987.struts.hibernate,同时取消“CreateAbstractClass”的选项。

(6)点击“next”按钮,将出现下面的内容,然后对反向工程的内容进行设置---选择主键id的产生方式为increment方式。

(7)点击“next”按钮,将出现下面的内容,然后对PO类进行设置

输入类名称为com.px1987.struts.hibernate.UserInfoPOJO,同时主键的产生方式为increment(因为我们的数据库表中主键为int类型)。

(8)然后再分别对PO类中的各个属性与数据库表中的字段的对应关系进行设置

userName、userPassWord、userDepartment、userAdminLevel、departAdminLevel、userImage、registerTime、id。

(9)最后点击“Finish”按钮,将创建出UserInfoPOJO.hbm.xml文件和对应的持久类UserInfoPOJO.java。

同时出现警告提示---不用管它!

7、最终的配置文件的代码示例

xmlversion="1.0"?

>

DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN"

"

--

MappingfileautogeneratedbyMyEclipse-HibernateTools

-->

catalog="WebStudyDB">

注意:

对上面的id标签添加一个unsaved-value="null"属性

8、创建出PO类com.px1987.struts.hibernate.UserInfoPOJO

下面为其代码,并对代码加以修改---请见黑体部分

packagecom.px1987.struts.hibernate;

publicclassUserInfoPOJOimplementsjava.io.Serializable{

privateIntegerid;

privateStringuserName;

privateStringuserPassWord;

privateStringuserDepartment;

privateIntegeruserAdminLevel;

privateIntegerdepartAdminLevel;

privateStringuserImage;

privateStringregisterTime;

publicUserInfoPOJO(){

}

publicUserInfoPOJO(StringuserName,StringuserPassWord,StringuserDepartment,IntegeruserAdminLevel,IntegerdepartAdminLevel,StringuserImage,StringregisterTime){

this.userName=userName;

this.userPassWord=userPassWord;

this.userDepartment=userDepartment;

this.userAdminLevel=userAdminLevel;

this.departAdminLevel=departAdminLevel;

this.userImage=userImage;

this.registerTime=registerTime;

}

publicIntegergetId(){

returnthis.id;

}

privatevoidsetId(Integerid){

this.id=id;

}

publicStringgetUserName(){

returnthis.userName;

}

publicvoidsetUserName(StringuserName){

this.userName=userName;

}

publicStringgetUserPassWord(){

returnthis.userPassWord;

}

publicvoidsetUserPassWord(StringuserPassWord){

this.userPassWord=userPassWord;

}

publicStringgetUserDepartment(){

returnthis.userDepartment;

}

publicvoidsetUserDepartment(StringuserDepartment){

this.userDepartment=userDepartment;

}

publicIntegergetUserAdminLevel(){

returnthis.userAdminLevel;

}

publicvoidsetUserAdminLevel(IntegeruserAdminLevel){

this.userAdminLevel=userAdminLevel;

}

publicIntegergetDepartAdminLevel(){

returnthis.departAdminLevel;

}

publicvoidsetDepartAdminLevel(IntegerdepartAdminLevel){

this.departAdminLevel=departAdminLevel;

}

publicStringgetUserImage(){

returnthis.userImage;

}

publicvoidsetUserImage(StringuserImage){

this.userImage=userImage;

}

publicStringgetRegisterTime(){

returnthis.registerTime;

}

publicvoidsetRegisterTime(StringregisterTime){

this.registerTime=registerTime;

}

publicbooleanequals(Objectother)

{

//该方法为对对象进行比较判断用

if(this==other)

{

returntrue;

}

if(!

(otherinstanceofUserInfoPOJO))

{

returnfalse;

}

returntrue;

}

}

注意:

上面的黑体是我们修改的部分

9、MyEclipser工具也会对hibernate.cfg.xml文件进行修改,内容为的下面的内容---请见黑体部分

(1)配置文件的代码示例

xmlversion='1.0'encoding='UTF-8'?

>

DOCTYPEhibernate-configurationPUBLIC

"-//Hibernate/HibernateConfigurationDTD3.0//EN"

"

--GeneratedbyMyEclipseHibernateTools.-->

jdbc:

microsoft:

sqlserver:

//localhost:

1433;DatabaseName=WebStudyDB

org.hibernate.dialect.SQLServerDialect

com.microsoft.jdbc.sqlserver.SQLServerDriver

sa

1234

true

(2)也可以通过其可视化界面来看到。

(3)注意编译后,将自动将前面的各个*.xml拷贝到WEB-INF/classes所在的目录下。

10、在项目的DAO组件类中通过Hibernate来进行数据库访问操作——但首先要在项目中添加数据库的JDBC驱动程序。

(1)编程DAOOperatorDBBean组件类中的HibernateSelectDBData方法加以功能实现

packagecom.px1987.struts.dao;

importjava.util.*;

importjava.sql.*;

importjavax.sql.*;

importorg.hibernate.*;

importcom.px1987.struts.hibernate.*;

publicclassDAOOperatorDBBeanimplementsDAOInterface

{

//。

其它代码

publicListHibernateSelectDBData(StringHQLStatement)

{

Sessionsession=null;

Listresult=null;

try

{

session=HibernateSessionFactory.currentSession();

result=session.createQuery(HQLStatement).list();

}

catch(HibernateExceptionhe)

{

System.out.println("在HibernateSelectDBData方法中出现了错误");

}

finally

{

HibernateSessionFactory.closeSession();

}

returnresult;

}

//。

其它代码

}

(2)修改UserLoginBean业务功能组件以对前面的基于Hibernate的数据库访问功能方法的调用

packagecom.px1987.struts.model;

importjavax.sql.*;

importjava.sql.*;

importcom.px1987.struts.dao.*;

publicclassUserLoginBeanimplementsUserLoginInterface

{

DataSourcedataSource=null;

publicUserLoginBean(DataSourcedataSource)

{

super();

this.dataSource=dataSource;

}

/*

该方法为采用Hibernate的API类访问的

*/

publicbooleandoUserLogin(UserInfoEntityBeanuserInfo)

{

booleanokOrNot=false;

StringuserName=userInfo.getUserName();

StringuserPassword=userInfo.getUserPassWord();

//不再需要dataSource,因为数据库的连接我们直接来自Hibernate的实现

DAOOperatorDBBeandaoOperatorDBBean=newDAOOperatorDBBean();

StringselectHQL="fromUserInfoPOJOuserInfowhereuserInfo.userName='"+

userName+"'anduserInfo.userPassWord='"+userPassword+"'";

java.util.Listresult=daoOperatorDBBean.HibernateSelectDBData(selectHQL);

if((result==null)||(result.size()==0))

{

okOrNot=false;

}

else

{

okOrNot=true;

}

returnokOrNot;

}

}

11、再将本项目部署和重新测试

(1)启动测试页面

(2)将出现下面成功的提示

(3)同时在控制台中也出现Hibernate的下面的提示。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 农林牧渔 > 林学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1