MyBatis_自定义映射resultMap

文章介绍了如何在MyBatis中创建自定义的resultMap来处理字段名与属性名不一致的情况,以及如何处理多对一和一对多的映射关系,包括级联方式、association标签和collection标签的使用,还提到了分步查询和延迟加载的概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自定义映射resultMap

创建数据表

复制进MySQL数据库中运行即可

DROP TABLE IF EXISTS `t_emp`;
CREATE TABLE `t_emp`  (
  `emp_id` int(11) NOT NULL AUTO_INCREMENT,
  `emp_name` varchar(25) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  `gender` varchar(25) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
  `dept_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`emp_id`) USING BTREE,
  INDEX `my`(`dept_id`) USING BTREE,
  CONSTRAINT `my` FOREIGN KEY (`dept_id`) REFERENCES `t_dept` (`dept_id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8 COLLATE = utf8_unicode_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Records of t_emp
-- ----------------------------
INSERT INTO `t_emp` VALUES (1, '张三', 18, '男', 1);
INSERT INTO `t_emp` VALUES (2, '李四', 19, '男', 2);
INSERT INTO `t_emp` VALUES (3, '王五', 18, '男', 3);
INSERT INTO `t_emp` VALUES (4, '赵六', 18, '男', 1);

SET FOREIGN_KEY_CHECKS = 1;

DROP TABLE IF EXISTS `t_dept`;
CREATE TABLE `t_dept`  (
  `dept_id` int(11) NOT NULL,
  `dept_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`dept_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_unicode_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Records of t_dept
-- ----------------------------
INSERT INTO `t_dept` VALUES (1, 'A');
INSERT INTO `t_dept` VALUES (2, 'B');
INSERT INTO `t_dept` VALUES (3, 'C');

SET FOREIGN_KEY_CHECKS = 1;

实体类

Emp员工
在这里插入图片描述
dept公司
在这里插入图片描述

字段名和属性名不一致(三种方式)

取别名

为查询的字段设置别名,和属性名保持一致

<!--    Emp getEmpById(@Param("id") Integer id); -->
<select id="getEmpByIdOne" resultType="Emp">
   select emp_id empId , emp_name empName , age , gender from t_emp where emp_id = #{id} 
</select>

设置全局配置

当字段符合MySQL的要求使用_,而属性符合java的要求使用驼峰
此时可以在MyBatis的核心配置文件中设置一个全局配置,可以自动将下划线映射为驼峰
emp_id ==> empId , emp_name ==> empName;

<!--- 核心配置文件中设置 -->
<settings>
    <!-- 下划线映射为驼峰 -->
    <setting  name="mapUnderscoreToCamelCase" value="true"/>
</settings>
  • 设置之后在映射文件中就可以不用取别名
<!-- Emp getEmpById(@Param("id") Integer id); -->
<select id="getEmpByIdOne" resultType="Emp">
    select * from t_emp where emp_id = #{id}
</select>

设置resultMap

resultMap: 设置自定义的映射关系

id:唯一标识

type:处理映射关系的实体的类型

id标签:设置主键的

column:设置映射关系中的字段名,必须是sql查询出的某一个字段

property:设置映射关系中的属性的属性名,必须是处理的实体类类型中的属性名

    <resultMap id="empResultMap" type="Emp">
        <id column="emp_id" property="empId"></id>
        <result column="emp_name" property="empName"></result>
        <result column="gender" property="gender"></result>
        <result column="age" property="age"></result>
    </resultMap>
  • select中的resultMap只需要引用resultMap标签中的id即可
<!--  Emp getEmpById(@Param("id") Integer id); -->
<select id="getEmpById" resultMap="empResultMap">
     select * from t_emp where emp_id = #{id}
 </select>

处理多对一的映射关系(三种方式)

级联方式处理

<!-- Emp getEmpAndDeptByEmpId(@Param("empId") Integer empid); -->
<resultMap id="getEmpAndDeptResultOne" type="Emp">
    <id column="emp_id" property="empId"></id>
    <result column="emp_name" property="empName"></result>
    <result column="gender" property="gender"></result>
    <result column="age" property="age"></result>
    
    <result column="dept_id" property="dept.deptId"></result>
    <result column="dept_name" property="dept.deptName"></result>
</resultMap>

association

association:处理多对一(或一对一)的映射关系(处理实体类类型的属性)

property:设置需要处理映射关系的属性的属性名

javaType:设置要处理的属性的类型

<resultMap id="getEmpAndDeptResult" type="Emp">
    <id column="emp_id" property="empId"></id>
    <result column="emp_name" property="empName"></result>
    <result column="gender" property="gender"></result>
    <result column="age" property="age"></result>
   
    <association property="dept" javaType="Dept">
        <result column="dept_id" property="deptId"></result>
        <result column="dept_name" property="deptName"></result>
    </association>
</resultMap>
  • select中的resultMap只需要引用resultMap标签中的id即可
<!-- Emp getEmpAndDeptByEmpId(@Param("empId") String empid); -->
<select id="getEmpAndDeptByEmpId" resultMap="getEmpAndDeptResult">
    select t_emp.* , t_dept.* from t_emp left join t_dept on t_dept.dept_id=t_emp.dept_id where t_emp.emp_id=#{empId}
</select>

分步查询

property:设置需要映射关系的属性的属性名

select:设置分步查询的sql的唯一标识

column:将查询出的某个字段作为分步查询的sql的条件

fetchType:开启了延迟加载的环境中,通过该属性设置当前的分步查询是否使用延迟加载

fetchType=“eager(立即加载)| lazy(延迟加载)”

<resultMap id="empAndDeptStepResultMap" type="Emp">
    <id column="emp_id" property="empId"></id>
    <result column="emp_name" property="empName"></result>
    <result column="age" property="age"></result>
    <result column="gender" property="gender"></result>
    
    <association property="dept" fetchType="eager"
                 select="com.ch.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"
                 column="dept_id">
    </association>
</resultMap>
  • select中的resultMap只需要引用resultMap标签中的id即可
<!-- Emp getEmpAndDeptByStepOne(@Param("empId") Integer empId); -->
<select id="getEmpAndDeptByStepOne" resultMap="empAndDeptStepResultMap">
    select * from t_emp where emp_id = #{empId}
</select>
  • 此时会将getEmpAndDeptByStepOne查询出来的dept_id作为getEmpAndDeptByStepTwo的查询条件
<!-- Dept getEmpAndDeptByStepTwo(@Param("deptId") Integer deptId); -->
<select id="getEmpAndDeptByStepTwo" resultType="dept">
    select * from t_dept where dept_id = #{deptId}
</select>

分步查询的优点:可以实现延迟加载,此时就可以实现按需加载,获取的数据是什么,就只会执行相应的sql

处理一对多的映射关系(两种方式)

collection

ofType: 设置集合类型的属性中存储的数据的类型

property: 设置需要映射关系的属性的属性名

<resultMap id="deptAndEmpResultMap" type="Dept">
    <id column="dept_id" property="deptId"></id>
    <result column="dept_name" property="deptName"></result>
  
    <collection property="emps" ofType="Emp">
        <id column="emp_id" property="empId"></id>
        <result column="emp_name" property="empName"></result>
        <result column="age" property="age"></result>
        <result column="gender" property="gender"></result>
    </collection>
</resultMap>
  • select中的resultMap只需要引用resultMap标签中的id即可
<!-- Dept getDeptAndEmpByDeptId(@Param("deptId") Integer deptId); -->
<select id="getDeptAndEmpByDeptId" resultMap="deptAndEmpResultMap">
    select t_dept.*,t_emp.* from t_dept left join t_emp on t_dept.dept_id = t_emp.dept_id where t_dept.dept_id=#{deptId}
</select>

分步查询

property:设置需要映射关系的属性的属性名

select:设置分步查询的sql的唯一标识

column:将查询出的某个字段作为分步查询的sql的条件

fetchType:开启了延迟加载的环境中,通过该属性设置当前的分步查询是否使用延迟加载

fetchType=“eager(立即加载)| lazy(延迟加载)”

<resultMap id="empAndDeptByStepOneMap" type="dept">
    <id column="dept_id" property="deptId"></id>
    <result column="dept_name" property="deptName"></result>
    <association property="emps"
                 select="com.ch.mybatis.mapper.EmpMapper.getDeptAndEmpByStepTwo"
                 column="dept_id">
    </association>
</resultMap>
  • select中的resultMap只需要引用resultMap标签中的id即可
<!-- Dept getDeptAndEmpByStepOne(@Param("deptId") Integer deptId); -->
<select id="getDeptAndEmpByStepOne" resultMap="empAndDeptByStepOneMap">
    select * from t_dept where dept_id= #{deptId}
</select>
  • 此时会将getDeptAndEmpByStepOne查询出来的dept_id作为getDeptAndEmpByStepTwo的查询条件
<!-- List<Emp> getDeptAndEmpByStepTwo(@Param("deptId") Integer deptId); -->
<select id="getDeptAndEmpByStepTwo" resultType="emp">
    select * from t_emp where dept_id = #{deptId}
</select>
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,我们可以灵活地实现数据库查询结果与实体类的映射关系,提高了系统的可维护性和代码的可读性。
评论 37
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一天睡20个小时

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值