惯例:先生成实体类.
岗位实体类:
package cn.xxx.entity; import java.io.Serializable; import java.util.ArrayList; import java.util.List; public class Post implements Serializable{ private static final long serialVersionUID = 8958411428421018244L; private Long postId; private String postContent; private Long postTime; //private Long personId; private Person person; private List<Comment> comments = new ArrayList<Comment>(); public Post() { } public Post(Long postId, String postContent, Long postTime, Person person, List<Comment> comments) { super(); this.postId = postId; this.postContent = postContent; this.postTime = postTime; this.person = person; this.comments = comments; } public Long getPostId() { return postId; } public void setPostId(Long postId) { this.postId = postId; } public String getPostContent() { return postContent; } public void setPostContent(String postContent) { this.postContent = postContent; } public Long getPostTime() { return postTime; } public void setPostTime(Long postTime) { this.postTime = postTime; } public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } public List<Comment> getComments() { return comments; } public void setComments(List<Comment> comments) { this.comments = comments; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((comments == null) ? 0 : comments.hashCode()); result = prime * result + ((person == null) ? 0 : person.hashCode()); result = prime * result + ((postContent == null) ? 0 : postContent.hashCode()); result = prime * result + ((postId == null) ? 0 : postId.hashCode()); result = prime * result + ((postTime == null) ? 0 : postTime.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Post other = (Post) obj; if (comments == null) { if (other.comments != null) return false; } else if (!comments.equals(other.comments)) return false; if (person == null) { if (other.person != null) return false; } else if (!person.equals(other.person)) return false; if (postContent == null) { if (other.postContent != null) return false; } else if (!postContent.equals(other.postContent)) return false; if (postId == null) { if (other.postId != null) return false; } else if (!postId.equals(other.postId)) return false; if (postTime == null) { if (other.postTime != null) return false; } else if (!postTime.equals(other.postTime)) return false; return true; } @Override public String toString() { return "Post [postId=" + postId + ", postContent=" + postContent + ", postTime=" + postTime + ", person=" + person + ", comments=" + comments + "]"; } }
人员实体类:
package cn.xxx.entity; import java.io.Serializable; public class Person implements Serializable{ private static final long serialVersionUID = 2125601210807098412L; private Long personId; private String personName; private int personAge; public Person() { } public Person(Long personId, String personName, int personAge) { super(); this.personId = personId; this.personName = personName; this.personAge = personAge; } public Long getPersonId() { return personId; } public void setPersonId(Long personId) { this.personId = personId; } public String getPersonName() { return personName; } public void setPersonName(String personName) { this.personName = personName; } public int getPersonAge() { return personAge; } public void setPersonAge(int personAge) { this.personAge = personAge; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + personAge; result = prime * result + ((personId == null) ? 0 : personId.hashCode()); result = prime * result + ((personName == null) ? 0 : personName.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Person other = (Person) obj; if (personAge != other.personAge) return false; if (personId == null) { if (other.personId != null) return false; } else if (!personId.equals(other.personId)) return false; if (personName == null) { if (other.personName != null) return false; } else if (!personName.equals(other.personName)) return false; return true; } @Override public String toString() { return "Person [personId=" + personId + ", personName=" + personName + ", personAge=" + personAge + "]"; } }
评价实体类:
package cn.xxx.entity; import java.io.Serializable; public class Comment implements Serializable{ private static final long serialVersionUID = 9005740179566619541L; private Long commentId; private String content; public Comment() { } public Comment(Long commentId, String content) { super(); this.commentId = commentId; this.content = content; } public Long getCommentId() { return commentId; } public void setCommentId(Long commentId) { this.commentId = commentId; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((commentId == null) ? 0 : commentId.hashCode()); result = prime * result + ((content == null) ? 0 : content.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Comment other = (Comment) obj; if (commentId == null) { if (other.commentId != null) return false; } else if (!commentId.equals(other.commentId)) return false; if (content == null) { if (other.content != null) return false; } else if (!content.equals(other.content)) return false; return true; } @Override public String toString() { return "Comment [commentId=" + commentId + ", content=" + content + "]"; } }
mapper接口:
package cn.xxx.mapper; import cn.xxx.entity.Post; public interface PostMapper { Post findPostByPostId(Long postId); }
mapper实现:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> <mapper namespace="cn.xxx.mapper.PostMapper"> <select id="findPostByPostId" parameterType="long" resultMap="rMap"> select po.post_id as postId, po.post_content as postContent, po.post_time as postTime, pe.person_id as personId, pe.person_name as personName, pe.person_age as personAge from post po left join person pe on po.person_id=pe.person_id where po.post_id=#{postId} </select> <resultMap id="rMap" type="cn.xxx.entity.Post"> <!-- id代表实体主键 --> <!-- property对应实体类属性, column是DB字段,如果改名了必须取改名后的 --> <id property="postId" column="postId"/> <result property="postContent" column="postContent"/> <result property="postTime" column="postTime"/> <!-- association 映射关联查询(单例) --> <!-- association property是本实体类中的字段属性名, javaType声明对应什么类型 --> <association property="person" javaType="cn.xxx.entity.Person"> <!-- id代表实体主键 --> <!-- property对应实体类属性, column是DB字段,如果改名了必须取改名后的 --> <id property="personId" column="personId"/> <result property="personName" column="personName"/> <result property="personAge" column="personAge"/> </association> <!-- collection 映射集合(多例), 需要传入column DB字段,如果改名了必须取改名后的 --> <collection property="comments" select="findCommentsByPostId" column="postId"/> </resultMap> <select id="findCommentsByPostId" resultType="cn.xxx.entity.Comment"> select comment_id as commentId, content as content from comment where post_id=#{postId} </select> </mapper>
进行测试:
测试方法:
@Test public void testFindPostByPostId(){ PostMapper postMapper = ctx.getBean("postMapper", PostMapper.class); Post post = postMapper.findPostByPostId(1L); System.out.println(post); }
打印结果:
Post [postId=1, postContent=开发, postTime=9, person=Person [personId=2, personName=jack, personAge=23], comments=[Comment [commentId=1, content=gfhgfjhgj], Comment [commentId=2, content=fgdgdfg], Comment [commentId=3, content=fdgdfgdfg]]]
转发请注明: 转自http://www.cnblogs.com/gscq073240/articles/6905306.html