ImageVerifierCode 换一换
格式:DOCX , 页数:33 ,大小:430.63KB ,
资源ID:10755331      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/10755331.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(20SSHStruts2.docx)为本站会员(b****7)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

20SSHStruts2.docx

1、20SSHStruts2SSHMyBatis 关联映射MyBatis可以处理赋值关系映射:业务描述:数据库SQL:create table p_person( id int not null AUTO_INCREMENT, name varchar(100), primary key(id) ); insert into p_person (name) values (李老师); insert into p_person (id, name) values (null,李老师); create table p_comment( id int not null AUTO_INCREMENT, t

2、itle varchar(100), post_id int, primary key(id) ); create table p_post( id int not null AUTO_INCREMENT, title varchar(100), person_id int, primary key(id) ); insert into p_post (id, title, person_id) values (null, 今天天气不错, 1); insert into p_post (id, title, person_id) values (null, 高考又来了, 1); insert

3、into p_comment(id, title, post_id) values ( null, 少穿了哪一件呀?, 1); insert into p_comment(id, title, post_id) values (null, 冻成狗, 1); insert into p_comment(id, title, post_id) values (null, 喜欢下雪, 1); 关联映射步骤1. 声明实体类:public class Person implements Serializable private static final long serialVersionUID = -

4、2365398342302306276L; private Integer id; private String name; public Person() public Person(Integer id, String name) super(); this.id = id; this.name = name; public Integer getId() return id; public void setId(Integer id) this.id = id; public String getName() return name; public void setName(String

5、 name) this.name = name; Override public String toString() return Person id= + id + , name= + name + ; Override public int hashCode() final int prime = 31; int result = 1; result = prime * result + (id = null) ? 0 : id.hashCode(); return result; Override public boolean equals(Object obj) if (this =

6、obj) return true; if (obj = null) return false; if (getClass() != obj.getClass() return false; Person other = (Person) obj; if (id = null) if (other.id != null) return false; else if (!id.equals(other.id) return false; return true; public class Comment implements Serializable private static final lo

7、ng serialVersionUID = -5881249634665160256L; private Integer id; private String title; public Comment() public Comment(String title) super(); this.title = title; public Integer getId() return id; public void setId(Integer id) this.id = id; public String getTitle() return title; public void setTitle(

8、String title) this.title = title; Override public int hashCode() final int prime = 31; int result = 1; result = prime * result + (id = null) ? 0 : id.hashCode(); return result; Override public boolean equals(Object obj) if (this = obj) return true; if (obj = null) return false; if (getClass() != obj

9、.getClass() return false; Comment other = (Comment) obj; if (id = null) if (other.id != null) return false; else if (!id.equals(other.id) return false; return true; Override public String toString() return Comment id= + id + , title= + title + ; public class Post implements Serializable private stat

10、ic final long serialVersionUID = -4722438109030592372L; private Integer id; private String title; /* 发帖人 */ private Person person; /* 当前帖子收到的回复 */ private List comments = new ArrayList(); public Post() public Integer getId() return id; public void setId(Integer id) this.id = id; public String getTit

11、le() return title; public void setTitle(String title) this.title = title; public Person getPerson() return person; public void setPerson(Person person) this.person = person; public List getComments() return comments; public void setComments(List comments) ments = comments; Override public String toS

12、tring() return Post id= + id + , title= + title + , person= + person + , comments= + comments + ; Override public int hashCode() final int prime = 31; int result = 1; result = prime * result + (id = null) ? 0 : id.hashCode(); return result; Override public boolean equals(Object obj) if (this = obj)

13、return true; if (obj = null) return false; if (getClass() != obj.getClass() return false; Post other = (Post) obj; if (id = null) if (other.id != null) return false; else if (!id.equals(other.id) return false; return true; 2. 添加映射接口:public interface PostDao Post findPostById(Integer id); 3. 添加映射基本属性

14、映射: NoteMapper.xml select id, title, person_id from p_post where id=#id 提示: MyBatis不自动支持复杂的映射关联, 复杂的关联关系必须使用resultMap进行手动映射.4. 测试:public class PostDaoTest extends BaseTest PostDao dao; Before public void initDao() dao = ctx.getBean( postDao, PostDao.class); Test public void testFindPostById() Post p

15、ost = dao.findPostById(1); System.out.println(post); 测试结果说明, 可以映射基本属性 id和name, 但是无法映射person属性和commons属性.5. 映射 person 属性, 使用关联映射: 重构PersonMapper.xml select p_post.id, title, person_id, p.name from p_post left outer join p_person p on p.id=person_id where p_post.id=#id 首先使用关联SQL查询, 查询出person对象的属性值, 再利

16、用association标签将查询结果映射到Person对象的属性.6. 测试:经过测试可用发现已经成功映射了person属性.7. 映射 comments 属性, 使用collection标签: 重构PostMapper.xml select id, title from p_comment where post_id=#id select p_post.id, title, person_id, p.name from p_post left outer join p_person p on p.id=person_id where p_post.id=#id 首先利用collection

17、表示将comments属性委托到SQL查询 findCommentsByPostId , 再定义SQL查询findCommentsByPostId, 将comment属性的数据查询并且映射到Comment对象. 8. 测试测试结果中将出现comments属性的值.Struts2MVC1. MVC模式是用户界面的经典设计模式! 无论是手机还是桌面以及WEB应用界面都采用了MVC模式.2. 在Java WEB编程中, 有很多的框架采用了MVC模式.Spring MVCStruts2 简介1. 来自Apache基金会的软件: http:/struts.apache.org2. Struts2 与 S

18、truts1 完全没有关系3. Struts2 的前身是 WebWorks 4. Spring MVCStruts2Struts1 都是 MVC 模式的Web框架 o MVC是非常流行的 用户界面设计模式。o MVC是3层架构中的表现层。配置Struts2配置步骤:1. 创建项目, 导入Struts2: org.apache.struts struts2-core 2.3.8 2. 配置Struts2 主控制器 web.xml StrutsPrepareAndExecuteFilter StrutsPrepareAndExecuteFilter org.apache.struts2.dispa

19、tcher.ng.filter.StrutsPrepareAndExecuteFilter StrutsPrepareAndExecuteFilter /* 3. 添加Struts2的配置文件 struts.xml: 提示: DTD 信息可以从 struts2-core-2.3.8.jar 包的struts-2.3.dtd 文件中找到.4. 部署到tomcat 进行测试.Hello World工作原理为:1. 编写控制器类:public class DemoAction /* * Struts2 控制器中的默认处理方法名为execute * return 返回视图的名称 */ public S

20、tring execute() System.out.println(Hello World!); return success; 默认的控制器方法为execute!2. 编写jsp: /WEB-INF/jsp/ok.jsp Struts2 Hello World! 3. 编写配置文件 /WEB-INF/jsp/ok.jsp package 的 name 属性任意定义, 不重复即可4. 部署测试:http:/localhost:8080/ssh1/demo/hello.action 接收用户提交的数据利用 控制器 的 Bean属性 接收用户提交的数据Bean属性: 是指类上声明的getXXX和

21、setXXX方法原理为: 案例:1. 声明控制器 /* * 利用Bean属性接收用户提交的参数 */ public class ParamAction private String name; private int age; public void setName(String name) this.name = name; public void setAge(int age) this.age = age; public String execute() System.out.println(name:+name); System.out.println(age:+age); return success; 2. 编写JSP: param.jsp 控制器接收参数 Struts2 接收参数 3. 配置控制器: /WEB-INF/jsp/param.j

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

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