1 / 10
文档名称:

Struts框架和Hibernate框架的整合完整版介绍.doc

格式:doc   大小:1,806KB   页数:10页
下载后只包含 1 个 DOC 格式的文档,没有任何的图纸或源代码,查看文件列表

如果您已付费下载过本站文档,您可以点这里二次下载

分享

预览

Struts框架和Hibernate框架的整合完整版介绍.doc

上传人:HShess 2024/5/10 文件大小:1.76 MB

下载得到文件列表

Struts框架和Hibernate框架的整合完整版介绍.doc

相关文档

文档介绍

文档介绍:该【Struts框架和Hibernate框架的整合完整版介绍 】是由【HShess】上传分享,文档一共【10】页,该文档可以免费在线阅读,需要了解更多关于【Struts框架和Hibernate框架的整合完整版介绍 】的内容,可以使用淘豆网的站内搜索功能,选择自己适合的文档,以下文字是截取该文章内的部分文字,如需要获得完整电子版,请下载此文档到您的设备,方便您编辑和打印。Struts框架和Hibernate框架的整合完整版介绍Struts框架和Hibernate框架的整合1、首先写一个student的实体类,命名为:;/***数据库表的映射实体类文件****@authorCalasin*/lassStudentEntity{ privateStrings_id;//学号 privateStrings_name;//姓名 <classname=""table="student"> <!--name对应的是java,column对应的是数据库,但是一般两个都写成一样,这样可以避免出错 name属性:主键id,对应的是java中的类中的属性, column属性:对应数据库中的主键id--> <idname="s_id"column="s_id"> <!--主键生成策略:uuid32位随机的字符串--> <generatorclass="uuid"></generator> </id> <!--name属性:对应java中的实体类属性,column属性:对应的数据库中的字段名称,type属性:表示java中的数据类型,默认可以不添加--> <propertyname="s_name"column="s_name"type=""></property> <propertyname="s_age"column="s_age"></property> </class></hibernate-mapping>3、接下来写实体类的Action:;;.;.;.;.;/***与jsp页面交互,完成数据传递****@authorCalasin*/lassStudentActionextendsActionSupport{ privateListstudentList;//创建一个List类型的学生列表 privateStudentServicestudentService=newStudentServiceImpl(); privateStudentEntitystudentEntity; /** *查询学生表的列表信息 * ****@return */ publicStringstudentList(){ studentList=(); return"studentList"; } publicStringupdPage(){ studentEntity=studentService .getStudentEntity(()); return"updPage"; } publicStringupd(){ (studentEntity); return"upd"; } publicListgetStudentList(){ returnstudentList; } publicvoidsetStudentList(ListstudentList){ =studentList; } publicStudentEntitygetStudentEntity(){ returnstudentEntity; } publicvoidsetStudentEntity(StudentEntitystudentEntity){ =studentEntity; }}4、接下来写提供数据库的接口interface:;;/***提供数据库接口****@authorCalasin*:设计Student对象及相关实体配置文件,工具类(得到一个Session对象),*StudentDao接口(实现此接口即以操作数据库),下面代码用"Dao"代替,编写主配置文件,编写测试类。*,最初打算设计成通用Object的操作,*后来发现它的Session对象操作都要传递一个对象,就设计成如下形式。内容如下:*/publicinterfaceDao{ publicListgetList(Stringhql);//查看学生的列表信息 publicvoidadd(Objectobj);//增加 publicvoidupdate(Objectobj);//修改 publicvoiddelete(Objectobj);//删除 publicObjectgetObj(Classcls,Stringid);//获取单一实体类对象}5、接下来写接口实现类的具体操作方法DaoImpl,;;.;.;.;.;/***Dao接口实现类,具体的操作,主要工作由Hibernate来完成*以下几点需要注意:导包:Hibernate包,数据库包;改写配置文件;查询方法的设计;注意事务,特别是“增删改”要注意事务。****@authorCalasin*/lassDaoImplimplementsDao{ privateSessionsession; publicDaoImpl(){//提供一个空参构造 //首先构建一个Configuration对象,可以调用configure()方法 Configurationcfg=newConfiguration().configure(); //再用这个对象构建一个SessionFactory对象,才是真正意义上可操控的实例对象 SessionFactorysessionFactory=(); //然后在用这个对象来构建一个SessionFactory对象 session=(); } //增加 publicvoidadd(Objectobj){ //TODOAuto-generatedmethodstub } //删除 publicvoiddelete(Objectobj){ //TODOAuto-generatedmethodstub } /** *查询列表方法(通用查询) */ publicListgetList(Stringhql){ //TODOAuto-generatedmethodstub try{ ();//开启事务 Queryquery=(hql); Listlist=(); ().commit();//提交 returnlist; }catch(Exceptione){ //TODO:handleexception returnnull; }finally{ (); } } /** *获取单一实体类对象 */ publicObjectgetObj(Classcls,Stringid){ try{ (); Objectobject=(cls,id); ().commit(); returnobject; }catch(Exceptione){ //TODO:handleexception returnnull; }finally{ (); } } /** *修改单一实体类对象 */ publicvoidupdate(Objectobj){ //TODOAuto-generatedmethodstub try{ (); (obj); ().commit(); }catch(Exceptione){ //TODO:handleexception }finally{ (); } }}6、接下来写service层:;;.;publicinterfaceStudentService{ publicListgetStudentList();//查 publicvoidadd(StudentEntitystudentEntity);//增 publicvoidupdate(StudentEntitystudentEntity);//改 publicvoiddelete(StudentEntitystudentEntity);//删 publicStudentEntitygetStudentEntity(Strings_id);}7、接下来写service层:;;.;.;.;/***针对数据库中student表的操作*****@authorCalasin**/lassStudentServiceImplimplementsStudentService{ privateDaodao=newDaoImpl(); publicvoidadd(StudentEntitystudentEntity){ //TODOAuto-generatedmethodstub } publicvoiddelete(StudentEntitystudentEntity){ //TODOAuto-generatedmethodstub } publicStudentEntitygetStudentEntity(Strings_id){ //TODOAuto-generatedmethodstub StudentEntitystudentEntity=(StudentEntity)( ,s_id); returnstudentEntity; } publicListgetStudentList(){ //TODOAuto-generatedmethodstub Stringhql="fromStudentEntity"; Listlist=(hql); returnlist; } publicvoidupdate(StudentEntitystudentEntity){ //TODOAuto-generatedmethodstub (studentEntity); }}8、配置文件config介绍():<!DOCTYPEhibernate-configurationPUBLIC "-//Hibernate///EN" "http://hibernate./hibernate-configuration-"><hibernate-configuration> <session-factory> <propertyname="">jdbc:mysql://localhost:3306/test?characterEncoding=utf-8</property> <propertyname=""></property> <propertyname="">root</property> <propertyname="">123456</property> <!--数据库方言--> <propertyname=""></property> <propertyname="">true</property> <mappingresource="com/liu/student/entity/"/> </session-factory></hibernate-configuration>:<?xmlversion=""encoding="UTF-8"?><!DOCTYPEstrutsPUBLIC"-//ApacheSoftwareFoundation////EN""http://struts./dtds/struts-"><struts> <constantname=""value="true"/> <constantname=""value="true"/> <packagename="stu"extends="struts-default"> <actionname="stu"class=""> <resultname="studentList">/jsp/student/</result> <resultname="updPage">/jsp/student/</result> <resultname="upd"type="redirect">stu!</result> </action> </package> </struts>