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>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,902评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,037评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,978评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,867评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,763评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,104评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,565评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,236评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,379评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,313评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,363评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,034评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,637评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,719评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,952评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,371评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,948评论 2 341