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