20SSHStruts2.docx

上传人:b****7 文档编号:10755331 上传时间:2023-02-22 格式:DOCX 页数:33 大小:430.63KB
下载 相关 举报
20SSHStruts2.docx_第1页
第1页 / 共33页
20SSHStruts2.docx_第2页
第2页 / 共33页
20SSHStruts2.docx_第3页
第3页 / 共33页
20SSHStruts2.docx_第4页
第4页 / 共33页
20SSHStruts2.docx_第5页
第5页 / 共33页
点击查看更多>>
下载资源
资源描述

20SSHStruts2.docx

《20SSHStruts2.docx》由会员分享,可在线阅读,更多相关《20SSHStruts2.docx(33页珍藏版)》请在冰豆网上搜索。

20SSHStruts2.docx

20SSHStruts2

SSH

MyBatis关联映射

MyBatis可以处理赋值关系映射:

业务描述:

数据库SQL:

createtablep_person(idintnotnullAUTO_INCREMENT,namevarchar(100),primarykey(id));insertintop_person(name)values('李老师');insertintop_person(id,name)values(null,'李老师');createtablep_comment(idintnotnullAUTO_INCREMENT,titlevarchar(100),post_idint,primarykey(id));createtablep_post(idintnotnullAUTO_INCREMENT,titlevarchar(100),person_idint,primarykey(id));insertintop_post(id,title,person_id)values(null,'今天天气不错',1);insertintop_post(id,title,person_id)values(null,'高考又来了',1);insertintop_comment(id,title,post_id)values(null,'少穿了哪一件呀?

',1);insertintop_comment(id,title,post_id)values(null,'冻成狗',1);insertintop_comment(id,title,post_id)values(null,'喜欢下雪',1);

关联映射步骤

1.声明实体类:

publicclassPersonimplementsSerializable{privatestaticfinallongserialVersionUID=-2365398342302306276L;privateIntegerid;privateStringname;publicPerson(){}publicPerson(Integerid,Stringname){super();this.id=id;this.name=name;}publicIntegergetId(){returnid;}publicvoidsetId(Integerid){this.id=id;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}@OverridepublicStringtoString(){return"Person[id="+id+",name="+name+"]";}@OverridepublicinthashCode(){finalintprime=31;intresult=1;result=prime*result+((id==null)?

0:

id.hashCode());returnresult;}@Overridepublicbooleanequals(Objectobj){if(this==obj)returntrue;if(obj==null)returnfalse;if(getClass()!

=obj.getClass())returnfalse;Personother=(Person)obj;if(id==null){if(other.id!

=null)returnfalse;}elseif(!

id.equals(other.id))returnfalse;returntrue;}}publicclassCommentimplementsSerializable{privatestaticfinallongserialVersionUID=-5881249634665160256L;privateIntegerid;privateStringtitle;publicComment(){}publicComment(Stringtitle){super();this.title=title;}publicIntegergetId(){returnid;}publicvoidsetId(Integerid){this.id=id;}publicStringgetTitle(){returntitle;}publicvoidsetTitle(Stringtitle){this.title=title;}@OverridepublicinthashCode(){finalintprime=31;intresult=1;result=prime*result+((id==null)?

0:

id.hashCode());returnresult;}@Overridepublicbooleanequals(Objectobj){if(this==obj)returntrue;if(obj==null)returnfalse;if(getClass()!

=obj.getClass())returnfalse;Commentother=(Comment)obj;if(id==null){if(other.id!

=null)returnfalse;}elseif(!

id.equals(other.id))returnfalse;returntrue;}@OverridepublicStringtoString(){return"Comment[id="+id+",title="+title+"]";}}publicclassPostimplementsSerializable{privatestaticfinallongserialVersionUID=-4722438109030592372L;privateIntegerid;privateStringtitle;/**发帖人*/privatePersonperson;/**当前帖子收到的回复*/privateListcomments=newArrayList();publicPost(){}publicIntegergetId(){returnid;}publicvoidsetId(Integerid){this.id=id;}publicStringgetTitle(){returntitle;}publicvoidsetTitle(Stringtitle){this.title=title;}publicPersongetPerson(){returnperson;}publicvoidsetPerson(Personperson){this.person=person;}publicListgetComments(){returncomments;}publicvoidsetComments(Listcomments){ments=comments;}@OverridepublicStringtoString(){return"Post[id="+id+",title="+title+",person="+person+",comments="+comments+"]";}@OverridepublicinthashCode(){finalintprime=31;intresult=1;result=prime*result+((id==null)?

0:

id.hashCode());returnresult;}@Overridepublicbooleanequals(Objectobj){if(this==obj)returntrue;if(obj==null)returnfalse;if(getClass()!

=obj.getClass())returnfalse;Postother=(Post)obj;if(id==null){if(other.id!

=null)returnfalse;}elseif(!

id.equals(other.id))returnfalse;returntrue;}}

2.添加映射接口:

publicinterfacePostDao{PostfindPostById(Integerid);}

3.添加映射基本属性映射:

NoteMapper.xml

--复杂映射必须使用resultMap进行处理-->

--逐一映射每个属性-->

--数据库主键,使用id映射-->

--非主键,使用result映射-->selectid,title,person_idfromp_postwhereid=#{id}

提示:

MyBatis不自动支持复杂的映射关联,复杂的关联关系必须使用resultMap进行手动映射.

4.测试:

publicclassPostDaoTestextendsBaseTest{PostDaodao;@BeforepublicvoidinitDao(){dao=ctx.getBean("postDao",PostDao.class);}@TestpublicvoidtestFindPostById(){Postpost=dao.findPostById

(1);System.out.println(post);}}

测试结果说明,可以映射基本属性id和name,但是无法映射person属性和commons属性.

5.映射person属性,使用关联映射:

重构PersonMapper.xml

--复杂映射必须使用resultMap进行处理-->

--逐一映射每个属性-->

--数据库主键,使用id映射-->

--非主键,使用result映射-->

--映射person属性使用association(关联)映射-->selectp_post.id,title,person_id,p.namefromp_postleftouterjoinp_personponp.id=person_idwherep_post.id=#{id}

首先使用关联SQL查询,查询出person对象的属性值,再利用association标签将查询结果映射到Person对象的属性.

6.测试:

经过测试可用发现已经成功映射了person属性.

7.映射comments属性,使用collection标签:

重构PostMapper.xml

--复杂映射必须使用resultMap进行处理-->

--逐一映射每个属性-->

--数据库主键,使用id映射-->

--非主键,使用result映射-->

--映射person属性使用association(关联)映射-->

--属性是一个集合,使用collection进行映射处理,其中column="id"是查询参数-->selectid,titlefromp_commentwherepost_id=#{id}selectp_post.id,title,person_id,p.namefromp_postleftouterjoinp_personponp.id=person_idwherep_post.id=#{id}

首先利用collection表示将comments属性委托到SQL查询findCommentsByPostId,再定义SQL查询findCommentsByPostId,将comment属性的数据查询并且映射到Comment对象.

8.测试

测试结果中将出现comments属性的值.

Struts2

MVC

1.MVC模式是用户界面的经典设计模式!

无论是手机还是桌面以及WEB应用界面都采用了MVC模式.

2.在JavaWEB编程中,有很多的框架采用了MVC模式.

SpringMVC

Struts2简介

1.来自Apache基金会的软件:

http:

//struts.apache.org

2.Struts2与Struts1完全没有关系

3.Struts2的前身是WebWorks

4.SpringMVC\Struts2\Struts1都是MVC模式的Web框架

oMVC是非常流行的用户界面设计模式。

oMVC是3层架构中的表现层。

配置Struts2

配置步骤:

1.创建项目,导入Struts2:

org.apache.strutsstruts2-core2.3.8

2.配置Struts2主控制器web.xml

StrutsPrepareAndExecuteFilterStrutsPrepareAndExecuteFilterorg.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterStrutsPrepareAndExecuteFilter/*

3.添加Struts2的配置文件struts.xml:

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

>

DOCTYPEstrutsPUBLIC"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.3//EN""http:

//struts.apache.org/dtds/struts-2.3.dtd">

提示:

DTD信息可以从struts2-core-2.3.8.jar包的struts-2.3.dtd文件中找到.

4.部署到tomcat进行测试.

HelloWorld

工作原理为:

1.编写控制器类:

publicclassDemoAction{/***Struts2控制器中的默认处理方法名为execute*@return返回视图的名称*/publicStringexecute(){System.out.println("HelloWorld!

");return"success";}}

默认的控制器方法为execute!

2.编写jsp:

/WEB-INF/jsp/ok.jsp

<%@pagelanguage="java"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%>

DOCTYPEhtml>

Struts2

HelloWorld!

3.编写配置文件

--定义WEB请求的第一层路径namespace="/demo"-->/WEB-INF/jsp/ok.jsp

--请求/demo/hello.action时候,执行DemoAction的execute方法,当方法返回值是success时候,转发到ok.jsp-->

package的name属性任意定义,不重复即可

4.部署测试:

http:

//localhost:

8080/ssh1/demo/hello.action

接收用户提交的数据

利用控制器的"Bean属性"接收用户提交的数据

Bean属性:

是指类上声明的getXXX和setXXX方法

原理为:

案例:

1.声明控制器

/***利用Bean属性接收用户提交的参数*/publicclassParamAction{privateStringname;privateintage;publicvoidsetName(Stringname){this.name=name;}publicvoidsetAge(intage){this.age=age;}publicStringexecute(){System.out.println("name:

"+name);System.out.println("age:

"+age);return"success";}}

2.编写JSP:

param.jsp

<%@pagelanguage="java"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%>

DOCTYPEhtml>控制器接收参数

Struts2

接收参数

3.配置控制器:

--请求路径:

/demo/param.action?

name=Tom&age=5-->/WEB-INF/jsp/param.j

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

当前位置:首页 > 高中教育 > 数学

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

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