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的下面的提示。