JAVA_EE_Mybatis之ResultMap标签

一、简介:

  1. <resultMap>标签写在 mapper.xml中,由程序员控制 SQL查询结果与
    实体类的映射关系.
    • 默认 MyBatis 使用 Auto Mapping 特性.
  2. 使用<resultMap>标签时,<select>标签不写 resultType 属性,而是使
    用 resultMap 属性引用<resultMap>标签.
  3. 使用 resultMap 实现单表映射关系
1. 实体类命名与数据库命名不同
private int id1;
    private String name1;
    private int age1;
    public int getId() {
        return id1;
    }
--------------------------------------------------------------------
<resultMap type="People" id="myMap">
        <id column="id" property="id1"/>
        <result column="name" property="name1"/>
        <result column="age" property="age1"/>
    </resultMap>
    <select id="selAll" resultMap="myMap">
        select * from people
    </select>

二、使用 resultMap 实现关联单个对象(N+1 方式)

介绍:N+1 查询方式,先查询出某个表的全部信息,根据这个表的信息查询另一个表的信息.
  • 与业务装配的区别:
    业务装配:完全可以在 service 里面写的代码,由 mybatis 完成装配,即先查询
  • 实现步骤:
  1. 在 Student 实现类中包含了一个 Teacher 对象
public class Student {
private int id;
private String name;
private int age;
private int tid;
private Teacher teacher;
  1. 在 TeacherMapper 中提供一个查询
<select id="selById" resultType="teacher" parameterType="int">
    select * from teacher where id=#{0}
</select
  1. 在 StudentMapper 中4.3.3.1 <association> 装配一个对象时使用
    property: 对象在类中的属性名
    select:通过哪个查询查询出这个对象的信息
    column: 把当前表的哪个列的值做为参数传递给另
    一个查询
<resultMap type="student" id="stuMap">
    <id property="id" column="id"/>
    <result property="name" column="name"/>
    <result property="age" column="age"/>
    <result property="tid" column="tid"/>
    <!-- 如果关联一个对象 -->
    <association property="teacher"         select="com.bjsxt.mapper.TeacherMapper.selById"column="tid"></association>
</resultMap>
<select id="selAll" resultMap="stuMap">
    select * from student
</select>

三、使用 resultMap 实现关联单个对象(联合查询方式)

  • 只需要编写一个 SQL,在 StudentMapper 中添加下面效果
  • <association/>只要专配一个对象就用这个标签
  • 此时把<association/>小的<resultMap>看待
  • javaType 属性:<association/>专配完后返回一个什么类型的对象.取值是一个类(或类的别名)
<resultMap type="Student" id="stuMap1">
    <id column="sid" property="id"/>
    <result column="sname" property="name"/>
    <result column="age" property="age"/>
    <result column="tid" property="tid"/>
    <association property="teacher" javaType="Teacher" >
        <id column="tid" property="id"/>
        <result column="tname" property="name"/>
    </association>
</resultMap>
<select id="selAll1" resultMap="stuMap1">
    select s.id sid,s.name sname,age age,t.id tid,t.name tname FROM student s left outer join teachert on s.tid=t.id
</select>

四、使用 resultMap 实现关联集合对象(N+1 方式)

  1. 在 Teacher 中添加 List<Student>
  2. 在 StudentMapper.xml 中添加通过 tid 查询
  3. 在 TeacherMapper.xml 中添加查询全部
    <collection/> 当属性是集合类型时使用的标签
<resultMap type="teacher" id="mymap">
    <id column="id" property="id"/>
    <result column="name" property="name"/>
    <collection property="list"
        select="com.bjsxt.mapper.StudentMapper.selByTid"
        column="id"></collection>
</resultMap>
<select id="selAll" resultMap="mymap">
    select * from teacher
</select>

五、使用<resultMap>实现关联集合对象(联合查询方式)

  • 在 teacherMapper.xml 中添加
  • mybatis 可以通过主键判断对象是否被加载过.不需要担心创建重复 Teacher
  • 注意:这里的sql语句为什么要起别名,是因为不起别名的花上面的配置文件中就都使用column=“id”。
<resultMap type="teacher" id="mymap1">
    <id column="tid" property="id"/>
    <result column="tname" property="name"/>
    <collection property="list" ofType="student" >
        <id column="sid" property="id"/>
        <result column="sname" property="name"/>
        <result column="age" property="age"/>
        <result column="tid" property="tid"/>
    </collection>
</resultMap>
<select id="selAll1" resultMap="mymap1">
    select t.id tid,t.name tname,s.id sid,s.name sname,age,tid from teacher t LEFT JOIN student s on t.id=s.tid;
</select>

六、使用 Auto Mapping 结合别名实现多表查询.

  • 只能使用多表联合查询方式,且智能查询单个对象
  • 要求:查询出的列别和属性名相同.
实现方式
  • 在 SQL 是关键字符,两侧添加反单引号
<select id="selAll" resultType="student">
    select t.id `teacher.id`,t.name `teacher.name`,s.id id,s.name name,age,tid from student s LEFT JOIN teacher t on t.id=s.tid
</select>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。