五、Mybatis-自定义映射ResultMap用法

一、处理属性名与字段名不同问题

1.通过设置查询别名,使类属性名与字段名(数据库内的名)一致

2.设置全局配置,使下划线自动映射为驼峰

<settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>

3.ResultMap

<resultMap id="empResultMap" type="com.mybatis.Bean.Emp">
        
<!--        id,说明主键,property 表示属性名,column 表示字段名-->
        
        <id property="eid" column="eid" ></id>
        <result property="name" column="name"></result>
        <result property="age" column="age"></result>
        <result property="sex" column="sex"></result>
        <result property="email" column="email"></result>
        <result property="did" column="did"></result>
</resultMap>
    
<select id="getEmpt" resultMap="empResultMap">
        select * from emp
</select>

二、处理多对一映射问题

前提背景

package com.mybatis.Bean;

public class Dept {
    private int did;
    private String name;

    public int getDid() {
        return did;
    }

    public void setDid(int did) {
        this.did = did;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Dept{" +
                "did=" + did +
                ", name='" + name + '\'' +
                '}';
    }
}

package com.mybatis.Bean;

public class Emp {

    private int eid;
    private String name;
    private int age;
    private char sex;
    private String email;
    private  Dept dept;

    public Dept getDept() {
        return dept;
    }

    public void setDept(Dept dept) {
        this.dept = dept;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "eid=" + eid +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", sex=" + sex +
                ", email='" + email + '\'' +
                ", dept=" + dept +
                '}';
    }

    public int getEid() {
        return eid;
    }

    public void setEid(int eid) {
        this.eid = eid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }


}

1.使用级联来实现

<resultMap id="empResultMap" type="com.mybatis.Bean.Emp">

<!--        id,说明主键,property 表示属性名,column 表示字段名-->

        <id property="eid" column="eid" ></id>
        <result property="name" column="name"></result>
        <result property="age" column="age"></result>
        <result property="sex" column="sex"></result>
        <result property="email" column="email"></result>
        <result property="dept.did" column="did"></result>
        <result property="dept.name" column="dname"></result>
</resultMap>

<select id="getEmpt" resultMap="empResultMap">
        select * from emp left join dept 
</select>

2.association 标签实现

 <resultMap id="empResultMap_" type="com.mybatis.Bean.Emp">

        <!--        id,说明主键,property 表示属性名,column 表示字段名-->

        <id property="eid" column="eid" ></id>
        <result property="name" column="name"></result>
        <result property="age" column="age"></result>
        <result property="sex" column="sex"></result>
        <result property="email" column="email"></result>
        <association property="dept" javaType="com.mybatis.Bean.Dept">
            <id property="did" column="did"></id>
            <id property="name" column="name"></id>
        </association>
    </resultMap>

3.分步查询来实现

在这里插入图片描述

三、处理一对多问题

背景

package com.mybatis.Bean;

import java.util.List;

public class Dept {
    private int did;
    private String name;

    private List<Emp> emps;

    public List<Emp> getEmps() {
        return emps;
    }

    @Override
    public String toString() {
        return "Dept{" +
                "did=" + did +
                ", name='" + name + '\'' +
                ", emps=" + emps +
                '}';
    }

    public void setEmps(List<Emp> emps) {
        this.emps = emps;
    }

    public int getDid() {
        return did;
    }

    public void setDid(int did) {
        this.did = did;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

1.collection实现

   <resultMap id="xx" type="com.mybatis.Bean.Dept">
        <id property="did" column="did"></id>
        <result property="name" column="name"></result>

<!--        collection 处理一对多的映射关系
            ofType 表示集合中的属性类型
-->
        <collection property="emps" ofType="com.mybatis.Bean.Emp">
            <id property="eid" column="eid"></id>
            <result property="name" column="name"></result>
        </collection>
    </resultMap>

<select id="xxx" resultMap="xx">
        
</select>
    

2.分步查询

在这里插入图片描述

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值