MyBatis联表处理

多对一的处理
  • 数据库结构


  • 实体类

public class Student {
    private int id;
    private String name;
    //多个学生对应一个老师
    private Teacher teacher;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Teacher getTeacher() {
        return teacher;
    }
    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }
}
public class Teacher {
    private int id;
    private String name;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
  • 编写student.mapper.xml映射文件

    • 结果嵌套处理

      <!-- 按结果嵌套处理 -->
      <select id="getStudents" resultMap="StudentTeacher">
          select s.id sid, s.name sname, s.tid stid, t.id tid, t.name tname from student  s, teacher t where s.tid = t.id
      </select>
      <resultMap type="Student" id="StudentTeacher">
          <id column="sid" property="id"/>
          <result column="sname" property="name"/>
          <!-- 关联对象 -->
          <!-- property关联对象在Student实体类中的属性 -->
          <association property="teacher" javaType="Teacher">
              <id column="tid" property="id"/>
              <result column="tname" property="name"/>
          </association>
      </resultMap>
      • resultMap中通过<association>嵌套了teacher属性
    • 查询嵌套处理

      <!-- 按查询嵌套处理 -->
      <select id="getStudents2" resultMap="StudentTeacher2">
          select * from student
      </select>
      <resultMap type="Student" id="StudentTeacher2">
          <!-- association关联属性 -->
          <!-- property属性名 -->
          <!-- column关联属性在多的一方的列名 -->
          <!-- javaType关联属性的类型 -->
          <!-- select关联查询 -->
          <association property="teacher" column="tid" javaType="Teacher" select="getTeacher">
          </association>
      </resultMap>
      <select id="getTeacher" resultType="Teacher">
          select * from teacher where id=#{id}
      </select>

一对多的处理
  • 数据库结构


  • 实体类

public class Teacher {
    private int id;
    private String name;
    //一个老师对应多个学生
    private List<Student> students;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public List<Student> getStudents() {
        return students;
    }
    public void setStudents(List<Student> students) {
        this.students = students;
    }
}
public class Student {
    private int id;
    private String name;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
  • 编写teacher.mapper.xml

    • 按结果嵌套处理

      <select id="getTeacher" resultMap="TeacherStudent">
      select s.id sid, s.name sname, s.tid stid, t.id tid, t.name tname from student  s, teacher t where s.tid = t.id and t.id=#{id}
      </select>
      <resultMap type="Teacher" id="TeacherStudent">
      <id column="tid" property="id"/>
      <result column="tname" property="name"/>
      <!-- association是关联一个 -->
      <!-- collection是关联多个 -->
      <collection property="students" ofType="Student">
          <id column="sid" property="id"/>
          <result column="sname" property="name"/>
      </collection>
      </resultMap>
      • <resultMap使用的标签是<collection>
      • <collection>中属性是ofType
    • 按查询嵌套处理

      <select id="getTeacher2" resultMap="TeacherStudent2">
          select * from teacher where id=#{id}
      </select>
      <resultMap type="Teacher" id="TeacherStudent2">
          <!-- column是一端的主键列名 -->
          <collection property="students" javaType="ArrayList" ofType="Student" column="id" select="getStudentByTid"></collection>
      </resultMap>
      <select id="getStudentByTid" resultType="Student">
           select * from student where tid=#{id}
      </select>

动态sql
  • Dynamic sql
    • 动态sql: 根据不同饿查询条件,生成不同的查询语句
    • 通过<select>中的<where><if>标签实现判断
<mapper namespace="com.eric.entity.UserMapper">
    <select id="getUserByCondition" parameterType="Map" resultType="User">
        select * from user
        <where>
            <if test="name!=null">
                name like CONCAT('%', #{name}, '%')
            </if>
        </where>
    </select>
</mapper>

猜你喜欢

转载自blog.csdn.net/weixin_40683252/article/details/81090280