Mybatis连接查询返回类型问题
一对一映射
public class Card { private Integer id; private String num; private Student student; //重要 public Card(){} public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getNum() { return num; } public void setNum(String num) { this.num = num; } public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } } public class Student { private Integer id; private String name; private Card card; //重要 public Student(){} public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Card getCard() { return card; } public void setCard(Card card) { this.card = card; } }
映射文件
CardMapper.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="cardNamespace"> <resultMap type="cn.itcast.javaee.mybatis.one2one.Card" id="cardMap"> <id property="id" column="id" /> <result property="num" column="num" /> </resultMap> </mapper>
StudentMapper.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="studentNamespace"> <resultMap type="cn.itcast.javaee.mybatis.one2one.Student" id="studentMap"> <id property="id" column="id" /> <result property="name" column="name"/> <association property="card" resultMap="cardNamespace.cardMap"/> </resultMap> <select id="findById" parameterType="int" resultMap="studentMap"> select s.id,s.name,c.id,c.num from students s inner join cards c on s.cid = c.id and s.id = #{id} </select> </mapper>
一对多映射
/** * 班级(单方) * @author AdminTC */ public class Grade { private Integer id; private String name; private List<Student> studentList = new ArrayList<Student>(); public Grade(){} public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Student> getStudentList() { return studentList; } public void setStudentList(List<Student> studentList) { this.studentList = studentList; } } /** * 学生(多方) * @author AdminTC */ public class Student { private Integer id; private String name; private Grade grade; public Student(){} public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Grade getGrade() { return grade; } public void setGrade(Grade grade) { this.grade = grade; } }
映射文件
GradeMapper.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="gradeNamespace"> <resultMap type="cn.itcast.javaee.mybatis.one2many.Grade" id="gradeMap"> <id property="id" column="gid" /> <result property="name" column="gname"/> <collection property="studentList" resultMap="studentNamespace.studentMap"/> </resultMap> <select id="findGradeByName" parameterType="string" resultMap="gradeMap"> select g.gid,g.gname,s.sid,s.sname from grades g,students s where g.gid = s.sgid and s.sname = #{name} </select> </mapper>
StudentMapper.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="studentNamespace"> <resultMap type="cn.itcast.javaee.mybatis.one2many.Student" id="studentMap"> <id property="id" column="sid" /> <result property="name" column="sname"/> <association property="grade" resultMap="gradeNamespace.gradeMap"/> </resultMap> <select id="findAllByName" parameterType="string" resultMap="studentMap"> select s.sid,s.sname,g.gid,g.gname from grades g,students s where g.gid = s.sgid and g.gname = #{name} </select> </mapper>
多对多映射
/** * 学生(多方) * @author AdminTC */ public class Student { private Integer id; private String name; private List<Course> courseList = new ArrayList<Course>(); public Student(){} public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Course> getCourseList() { return courseList; } public void setCourseList(List<Course> courseList) { this.courseList = courseList; } } /** * 课程(多方) * @author AdminTC */ public class Course { private Integer id; private String name; private List<Student> studentList = new ArrayList<Student>(); public Course(){} public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Student> getStudentList() { return studentList; } public void setStudentList(List<Student> studentList) { this.studentList = studentList; } }
映射文件
StudentMapper.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="studentNamespace"> <resultMap type="cn.itcast.javaee.mybatis.many2many.Student" id="studentMap"> <id property="id" column="sid" /> <result property="name" column="sname"/> </resultMap> <select id="findStudentByName" parameterType="string" resultMap="studentMap"> select s.sid,s.sname from students s,middles m,courses c where s.sid = m.sid and m.cid = c.cid and c.cname = #{name} </select> </mapper> CourseMapper.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="courseNamespace"> <resultMap type="cn.itcast.javaee.mybatis.many2many.Course" id="courseMap"> <id property="id" column="cid" /> <result property="name" column="cname"/> </resultMap> <select id="findCourseByName" parameterType="string" resultMap="courseMap"> select c.cid,c.cname from students s,middles m,courses c where s.sid = m.sid and m.cid = c.cid and s.sname = #{name} </select> </mapper>