1. 程式人生 > 實用技巧 >Mybatis-08-結果集對映

Mybatis-08-結果集對映

  • 作用

    • MyBatis 中最重要最強大的元素

    • 解決欄位名和屬性名不一致的問題

    • ResultMap 的設計思想是,對簡單的語句做到零配置,對於複雜一點的語句,只需要描述語句之間的關係

  • ResultMap配置詳解

    <!--
    id為這個resultMap的標識
    type為對映的型別,一個Java類的全限定名,或一個類型別名
    -->
    <resultMap id="" type="">
    <!--
    id和result都將一個列的值對映到一個簡單資料型別的屬性或欄位
    column:對應資料庫中的列
    property:對應實體類中的屬性
    javaType:一個Java類的全限定名,或一個類型別名,結果集對映返回的結果
    jdbcType:所支援的JDBC型別
    typeHandler:型別處理器
    -->
    <id column="" property="" javaType="" jdbcType="" typeHandler=""></id>
    <result column="" property="" javaType="" jdbcType="" typeHandler=""></result>
    <!--
    association處理“有一個”型別的關係
    collection處理“有多個”型別的關係
    column:對應資料庫中的列
    property:對應實體類中的屬性(物件)
    javaType:指定實體類中屬性的型別
    ofType:指定對映到集合中的實體類型別,泛型中的約束型別
    select:用於載入複雜型別屬性的對映語句的id
    會從column屬性指定的列中檢索資料,作為引數傳遞給目標select語句
    -->
    <association column="" property="" javaType="" select=""></association>
    <collection column="" property="" javaType="" select=""></collection>
    </resultMap>
  • 1、解決資料庫中的欄位和實體類中屬性不一致

    • 實體類

      @Data
      @NoArgsConstructor
      @AllArgsConstructor
      public class User {

      private Integer id;
      private String name;
      private String password;
      }
    • 介面類

      User selectById(int id);
    • 介面類的實現

      <resultMap id="UserMap" type="user">
      <!--
      id,name屬性和資料庫中的欄位一樣,所有不需要顯式寫出,可省略
      <result column="id" property="id"/>
      <result column="name" property="name"/>
      -->
      <result column="pwd" property="password"/>
      </resultMap>

      <!--resultMap裡的值為結果集對映<resultMap></resultMap>的id值-->
      <select id="selectById" resultMap="UserMap">
      select * from mybatistest where id = #{id};
      </select>
  • 2、多對一以及一對多問題

    • Teacher表

      id:老師id
      name:老師姓名
    • Student表

      id:學生id
      name:學生姓名
      tid:學生關聯的老師id
    • 多對一:多個學生對應一個老師,查詢學生的資訊以及學生的老師的資訊

      • 1、老師類:Teacher

        @Data
        public class Teacher {
        private int id;
        private String name;
        }
      • 2、老師的介面類:TeacherMapper

        public interface TeacherMapper {
        }
      • 3、老師介面類的實現:TeacherMapper.xml

        <?xml version="1.0" encoding="UTF8" ?>
        <!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

        <mapper namespace="com.hmx.mapper.TeacherMapper">
        </mapper>
      • 4、學生類:Student

        @Data
        public class Student {

        private int id;
        private String name;
        private Teacher teacher;
        }
      • 5、學生的介面類:StudentMapper

        public interface StudentMapper {
        List<Student> getStudent();
        }
      • 6、學生介面類的實現:StudentMapper.xml

        • 方法一:根據查詢巢狀處理,子查詢

          <?xml version="1.0" encoding="UTF8" ?>
          <!DOCTYPE mapper
          PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
          "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

          <mapper namespace="com.hmx.mapper.StudentMapper">

          <!--查詢所有的學生資訊-->
          <select id="getStudent" resultMap="StudentTeacher">
          select * from student
          </select>

          <!--
          根據查詢出來的學生的tid屬性,查詢老師的資訊
          tid的值是從resultMap中傳遞過來的
          -->
          <select id="getTeacher" resultType="Teacher">
          select * from teacher where id = #{tid}
          </select>

          <resultMap id="StudentTeacher" type="Student">
          <result property="id" column="id"/>
          <result property="name" column="name"/>

          <!--從tid列中檢索資料,作為引數傳遞給id為getTeacher的select語句-->
          <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
          </resultMap>
          </mapper>
        • 方法二:根據結果巢狀處理,關聯查詢

          <?xml version="1.0" encoding="UTF8" ?>
          <!DOCTYPE mapper
          PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
          "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

          <mapper namespace="com.hmx.mapper.StudentMapper">

          <select id="getStudent" resultMap="StudentTeacher">
          select s.id as sid,s.`name` as sname,t.`name` tname
          from student s,teacher t
          where s.tid = t.id
          </select>

          <resultMap id="StudentTeacher" type="Student">
          <result property="id" column="sid"/>
          <result property="name" column="sname"/>
          <association property="teacher" javaType="Teacher">
          <result property="name" column="tname"/>
          </association>
          </resultMap>

          </mapper>
      • 7、測試

        public static void selectAll() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> studentList = studentMapper.getStudent();
        for (Student student : studentList) {
        System.out.println(student);
        }

        sqlSession.close();
        }

    • 一對多:一個老師有多個學生,查詢指定老師下的學生的資訊,以及該老師的資訊

      • 1、學生類:Student

        @Data
        public class Student {

        private int id;
        private String name;
        private int tid;
        }
      • 2、學生的介面類:StudentMapper

        public interface StudentMapper {
        }
      • 3、學生介面類的實現類:StudentMapper.xml

        <?xml version="1.0" encoding="UTF8" ?>
        <!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

        <mapper namespace="com.hmx.mapper.StudentMapper">
        </mapper>
      • 4、老師類:Teacher

        @Data
        public class Teacher {

        private int id;
        private String name;
        private List<Student> studentList;
        }
      • 5、老師的介面類:TeacherMapper

        public Teacher getTeacher(int id);
      • 6、老師介面類的實現類:TeacherMapper.xml

        • 方法一:根據查詢巢狀處理,子查詢

          <?xml version="1.0" encoding="UTF8" ?>
          <!DOCTYPE mapper
          PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
          "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
          <mapper namespace="com.hmx.mapper.TeacherMapper">

          <!--查詢老師的所有資訊-->
          <select id="getTeacher" resultMap="TeacherStudent">
          select * from teacher where id = #{id}
          </select>

          <!--根據老師的id查詢學生的所有資訊-->
          <select id="getStudentByTeacherId" resultType="Student">
          select * from student where tid = #{tid}
          </select>

          <!--把老師裡的id傳給select查詢塊-->
          <resultMap id="TeacherStudent" type="Teacher">
          <collection property="studentList" column="id" javaType="ArrayList" ofType="student" select="getStudentByTeacherId">
          </collection>
          </resultMap>

          </mapper>
        • 方法二:根據結果巢狀處理,關聯查詢

          <?xml version="1.0" encoding="UTF8" ?>
          <!DOCTYPE mapper
          PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
          "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
          <mapper namespace="com.hmx.mapper.TeacherMapper">

          <select id="getTeacher" resultMap="TeacherStudent">
          SELECT
          s.id AS sid,
          s.`name` AS sname,
          t.id AS tid,
          t.`name` tname
          FROM
          student s,
          teacher t
          WHERE
          s.tid = t.id AND t.id = #{tid}
          </select>

          <resultMap id="TeacherStudent" type="Teacher">
          <result property="id" column="tid"/>
          <result property="name" column="tname"/>

          <collection property="studentList" ofType="Student">
          <result property="id" column="sid"/>
          <result property="name" column="sname"/>
          <result property="tid" column="tid"/>
          </collection>
          </resultMap>

          </mapper>
      • 7、測試

        public static void testTeacher() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        TeacherMapper teacherMapper = sqlSession.getMapper(TeacherMapper.class);
        Teacher teacher = teacherMapper.getTeacher(1);
        System.out.println(teacher);

        sqlSession.close();
        }