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

本文详细介绍了如何在MyBatis中解决属性名与数据库字段名不匹配的问题,通过查询别名、全局配置和ResultMap进行调整。同时探讨了多对一和一对一关系的映射策略,包括级联、association标签以及分步查询的方法。
摘要由CSDN通过智能技术生成

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

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
    评论
MybatisPlus是一个基于Mybatis框架的增强工具,可以简化和加速开发过程。在使用MybatisPlus进行数据库操作时,我们可以利用它自带的功能来进行基本的CRUD操作,同时也可以进行自定义映射resultmap自定义映射resultmap可以帮助我们在数据库查询结果与实体类之间建立映射关系,进而方便地操作数据。一般情况下,MybatisPlus会根据数据库字段与实体类属性的对应关系自动进行映射,但在某些特殊情况下,我们可能需要自己来定义映射关系。 要自定义映射resultmap,我们可以通过在实体类中使用注解`@ResultMap`来实现。首先,我们需要在实体类中定义与数据库字段对应的属性,并通过注解`@TableField`来指定数据库字段名。然后,我们可以在实体类中使用注解`@ResultMap`来声明自定义映射resultmap,并指定与数据库字段对应的属性。 例如,有一个数据库表student,包含字段id、name和age,我们可以定义一个实体类Student,其中id属性对应数据库字段id,name属性对应数据库字段name,age属性对应数据库字段age。然后,在Student类中使用注解`@ResultMap`自定义映射resultmap,例如: ```java @Data public class Student { @TableField("id") private Long id; @TableField("name") private String name; @TableField("age") private Integer age; @ResultMap("studentResultMap") public class StudentResultMap{ return new StudentResultMap(){ put("id", "id"); put("name","name"); put("age","age"); } } } ``` 在上述示例中,我们定义了一个名为studentResultMap自定义映射resultmap,并通过`put`方法指定了属性与数据库字段的对应关系。 当我们需要进行数据库查询时,可以通过`@Select`注解或者使用MybatisPlus提供的查询方法来执行查询操作。在查询操作中,我们可以使用自定义映射resultmap来获取查询结果,并将其映射到实体类中。 总之,通过自定义映射resultmap,我们可以灵活地实现数据库查询结果与实体类的映射关系,提高了系统的可维护性和代码的可读性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值