zl程序教程

您现在的位置是:首页 >  其他

当前栏目

MyBatis_自定义映射resultMap

2023-04-18 15:36:55 时间

自定义映射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>