300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > Mybatis——自定义映射ResultMap

Mybatis——自定义映射ResultMap

时间:2019-01-25 05:09:50

相关推荐

Mybatis——自定义映射ResultMap

Mybatis

引入一、ResultMap处理字段和属性映射关系二、多对一映射处理①dept表②emp表③级联方式处理映射关系④使用association处理映射关系三、一对多映射处理四、感慨

引入

简述:

ResultMap自定义映射是为了解决我们的Java中的属性和数据库的字段名称不一致的问题。

一、ResultMap处理字段和属性映射关系

先来看一张表格:下面是类的属性和 数据库表中的字段

解决字段和属性不匹配问题采用resultMap标签 自定义映射id : 标识自定义映射的唯一标识type : 查询数据要映射的实体类类型resultMap标签下的子标签详解:id标签 : 设置主键的映射关系result标签 : 设置普通字段的映射关系property : 类中的属性名column : 表中的字段名

<resultMap id="empResultMap" type="emp"><id property="eid" column="eid"></id><result property="empName" column="emp_name"></result><result property="age" column="age"></result><result property="sex" column="sex"></result><result property="email" column="email"></result></resultMap>

<!--resultMap填入自定义resultMap的id--><select id="getAllEmp" resultMap="empResultMap">select * from emp</select>

若字段名和实体类中的属性名不一致,但是字段名符合数据库的使用,实体类中的属性名符合Java的规则(驼峰),此时可以通过以下两种方式处理映射关系

通过起别名的方式,保证和实体类中的属性名保持一致在mybatis核心配置文件下,设置全局配置信息mapUnderscoreToCamelCase自动将带有下划线的字段名转换为驼峰

例如:字段名 emp_name --> empName

<!--设置Mybatis的全局配置--><settings><!--将下划线自动映射为驼峰 例如:emp_name -> empName--><setting name="mapUnderscoreToCamelCase" value="true"/></settings>

二、多对一映射处理

直接上代码进行实操

①dept表

/*Navicat Premium Data TransferSource Server : DeSource Server Type : MySQLSource Server Version : 50519Source Host : localhost:3306Source Schema : mybatisTarget Server Type : MySQLTarget Server Version : 50519File Encoding : 65001Date: 11/04/ 15:48:06*/SET NAMES utf8mb4;SET FOREIGN_KEY_CHECKS = 0;-- ------------------------------ Table structure for dept-- ----------------------------DROP TABLE IF EXISTS `dept`;CREATE TABLE `dept` (`did` int(11) NOT NULL AUTO_INCREMENT,`dept_name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,PRIMARY KEY (`did`) USING BTREE) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;-- ------------------------------ Records of dept-- ----------------------------INSERT INTO `dept` VALUES (1, 'A');SET FOREIGN_KEY_CHECKS = 1;

②emp表

/*Navicat Premium Data TransferSource Server : DeSource Server Type : MySQLSource Server Version : 50519Source Host : localhost:3306Source Schema : mybatisTarget Server Type : MySQLTarget Server Version : 50519File Encoding : 65001Date: 11/04/ 15:47:53*/SET NAMES utf8mb4;SET FOREIGN_KEY_CHECKS = 0;-- ------------------------------ Table structure for emp-- ----------------------------DROP TABLE IF EXISTS `emp`;CREATE TABLE `emp` (`eid` int(11) NOT NULL AUTO_INCREMENT,`emp_name` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,`age` int(11) NULL DEFAULT NULL,`sex` char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,`email` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,`did` int(11) NULL DEFAULT NULL,PRIMARY KEY (`eid`) USING BTREE) ENGINE = InnoDB AUTO_INCREMENT = 12 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;-- ------------------------------ Records of emp-- ----------------------------INSERT INTO `emp` VALUES (1, 'admir', 22, NULL, '456@', 3);INSERT INTO `emp` VALUES (2, '李四', 22, '男', '123@', 2);INSERT INTO `emp` VALUES (3, '王五', 23, '男', '123@', 3);INSERT INTO `emp` VALUES (4, '赵六', 24, '男', '123@', 1);INSERT INTO `emp` VALUES (8, 'a1', 18, '男', '123@', NULL);INSERT INTO `emp` VALUES (9, 'a2', 18, '男', '123@', NULL);INSERT INTO `emp` VALUES (10, 'a3', 18, '男', '123@', NULL);INSERT INTO `emp` VALUES (11, 'jack', 18, '男', '123@', NULL);SET FOREIGN_KEY_CHECKS = 1;

③级联方式处理映射关系

<resultMap id="empAndDeptResultMapOne" type="Emp"><id property="eid" column="eid"></id><result property="empName" column="emp_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.deptName" column="dept_name"></result></resultMap>

④使用association处理映射关系

association:设置多对一的映射关系property:设置映射关系中的属性名 必须是type属性所设置的实体类型中的属性名column : 设置映射关系中的字段名 必须是sql语句查询出的字段名

<resultMap id="empAndDeptResultMapTwo" type="Emp"><id property="eid" column="eid"></id><result property="empName" column="emp_name"></result><result property="age" column="age"></result><result property="sex" column="sex"></result><result property="email" column="email"></result><association property="dept" javaType="Dept"><id property="did" column="did"></id><result property="deptName" column="dept_name"></result></association></resultMap>

三、一对多映射处理

使用collection标签

/*** 获取部门及部门中所有的员工对象*/Dept getDeptAndEmp(@Param("did")int did);

<!--一对多映射--><resultMap id="deptAndEmpResultMap" type="Dept"><id property="did" column="did"></id><result property="deptName" column="dept_name"></result><!--ofType : 集合存储的类型collection 和 association 比较前者需要知道集合所存储的类型后者需要知道Java类型--><collection property="emps" ofType="Emp"><id property="eid" column="eid"></id><result property="empName" column="emp_name"></result><result property="age" column="age"></result><result property="sex" column="sex"></result><result property="email" column="email"></result></collection></resultMap><!--Dept getDeptAndEmp(@Param("did")int did);--><select id="getDeptAndEmp" resultMap="deptAndEmpResultMap">select * from dept left join emp on dept.did=emp.did where dept.did=#{did}</select>

四、感慨

每一次的笔记写入都加深了自己的印象,也深刻体会到了自己的无力与无知。自己并不能准确的表达每个知识点的具体含义,希望读者能够给予纠正以及支持。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。