当前位置:网站首页>8、自定义映射resultMap
8、自定义映射resultMap
2022-08-04 05:24:00 【猿 白】
8、自定义映射resultMap
搭建MyBatis框架
新建modules ----->resultMap
t_emp表:
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for t_emp
-- ----------------------------
DROP TABLE IF EXISTS `t_emp`;
CREATE TABLE `t_emp` (
`emp_id` int(0) NOT NULL AUTO_INCREMENT,
`emp_name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`age` int(0) NULL DEFAULT NULL,
`gender` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`dept_id` int(0) NULL DEFAULT NULL,
PRIMARY KEY (`emp_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- 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, '王五', 20, '女', 1);
INSERT INTO `t_emp` VALUES (4, '赵六', 21, '男', 3);
SET FOREIGN_KEY_CHECKS = 1;
t_dept表:
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for t_dept
-- ----------------------------
DROP TABLE IF EXISTS `t_dept`;
CREATE TABLE `t_dept` (
`dept_id` int(0) NOT NULL AUTO_INCREMENT,
`dept_name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
PRIMARY KEY (`dept_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- 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');
INSERT INTO `t_dept` VALUES (4, 'D');
SET FOREIGN_KEY_CHECKS = 1;
8.1、resultMap处理字段和属性的映射关系
使用全局配置处理字段名和属性名不一致的问题
<settings>
<!--将下划线映射为驼峰-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>


private Integer empId;
private String empName;
private Integer age;
private String gender;

<!--
字段名和属性名不一致的情况,如何处理映射关系
1.为查询的字段设置别名,和属性名保持一致
2.当查询的字段符合MYSQL的要求使用_,而属性符合java的要求使用驼峰
此时可以在mybatis的核心配置文件中设置一个全局配置,可以自动将下划线映射为驼峰
emp_id:empId emp_name:empName
-->
<!-- Emp getEmpById(@Param("empId") int empId);-->
<select id="getEmpById" resultType="Emp">
<!--select emp_id empId,emp_name empName,age,gender from t_emp where emp_id = #{
empId}-->
select * from t_emp where emp_id = #{
empId}
</select>
使用resultMap处理字段名和属性名不一致的问题
<!--
resultMap:设置自定义映射
属性:
id:表示自定义映射的唯一标识
type:查询的数据要映射的实体类的类型
子标签:
id:设置主键的映射关系
result:设置普通字段的映射关系
association:设置多对一的映射关系(处理实体类类型的属性)
collection:设置一对多的映射关系
属性:
property:设置映射关系中实体类中的属性名
column:设置映射关系中表中的字段名
-->
<resultMap id="empResultMap" 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>
</resultMap>
<select id="getEmpById" resultMap="empResultMap">
select * from t_emp where emp_id = #{
empId}
</select>
8.2、多对一映射处理
8.2.1、级联方式处理映射关系
Emp中添加dept
private Dept dept;
<resultMap id="getemptAndDeptByEmpId" 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>
<result column="dept_id" property="dept.deptId"></result>
<result column="dept_name" property="dept.deptName"></result>
</resultMap>
<!-- Emp getempAndDeptByEmpId(@Param("empId") Integer empId);-->
<select id="getempAndDeptByEmpId" resultMap="getemptAndDeptByEmpId">
select t_emp.*,t_dept.*
from t_emp left join t_dept on
t_emp.dept_id = t_dept.dept_id
where t_emp.emp_id = #{
empId}
</select>
@Test
public void testGetEmpAndDeptByEmpId(){
SqlSession sqlSession = sqlSessionUtil.getSqlSession();
EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
Emp empById = mapper.getempAndDeptByEmpId(1);
System.out.println(empById);
}
8.2.2、使用association处理映射关系
<!--
assocation:处理多对一多的映射关系(处理实体类类型的属性)
propertye:设置需要处理映射关系的属性的属性名
javaType:设置需要处理的属性的类型
-->
<resultMap id="getemptAndDeptByEmpId" 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" javaType="dept">
<id column="dept_id" property="deptId"></id>
<result column="dept_name" property="deptName"></result>
</association>
</resultMap>
8.2.3、分步查询
EmpMapper接口
//分步查询(1)①查询员工信息
Emp getempAndDeptByStepOne (@Param("empId") Integer empId);
DeptMapper接口
//分步查询(2)②根据员工所对应的部门id查询部门信息
Dept getEmpAndDeptByStepTwo(@Param("deptId") Integer deptId);
EmpMapper.xml
<!--
proerty:设置㤇处理映射关系的属性的属性名
Select:设置需要分步查询的SQL的唯一标识
column:将查询出的某个字段作为分步查询的SQL的条件
-->
<resultMap id="getemptAndDeptStepOne" 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"
select="com.gao.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"
column="dept_id">
</association>
</resultMap>
<!--Emp getempAndDeptByStepOne (@Param("empId") Integer empId);-->
<select id="getempAndDeptByStepOne" resultMap="getemptAndDeptStepOne">
select * from t_emp where emp_id = #{
empId}
</select>
DeptMapper.xml
<!--Dept getEmpAndDeptByStepTwo(@Param("deptId") Integer deptId);-->
<select id="getEmpAndDeptByStepTwo" resultType="dept">
select * from t_dept where dept_id = #{
deptId}
</select>
测试
@Test
public void testGetEmpAndDeptByStep(){
SqlSession sqlSession = sqlSessionUtil.getSqlSession();
EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
Emp emp = mapper.getempAndDeptByStepOne(2);
System.out.println(emp);
}
延迟加载
分步查询的优点:可以实现延迟加载
但是必须在核心配置文件中设置全局配置信息:lazyLoadingEnabled:延迟加载的全局开关。当开启时,所有关联对象都会延迟加载aggressiveLazyLoading:当开启时,任何方法的调用都会加载该对象的所有属性。否则,每个属性会按需加载
此时就可以实现按需加载,获取的数据是什么,就只会执行相应的sql。此时可通过association和collection中的fetchType属性设置当前的分步查询是否使用延迟加载, fetchType=“lazy(延迟加载)|eager(立即加载)”
<settings>
<!--将下划线映射为驼峰-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!--开启延迟加载-->
<setting name="lazyLoadingEnabled" value="true"/>
<!--按需加载-->
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
测试:
@Test
public void testGetEmpAndDeptByStep(){
SqlSession sqlSession = sqlSessionUtil.getSqlSession();
EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
Emp emp = mapper.getempAndDeptByStepOne(2);
System.out.println(emp.getEmpName());
}
<resultMap id="getemptAndDeptStepOne" 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="lazy"
select="com.gao.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"
column="dept_id">
</association>
</resultMap>
fetchType:在开启了延迟加载的环境中,通过该属性设置当前的分步查询是否使用延迟加载
fetchType="lazy(延迟) | eager(立即加载)"
8.3、一对多映射处理
8.3.1、collection
Dept类增加属性:
private List<Emp> emps;
//查询部门以及部门中员工的信息
Dept getDeptAndEmpByDeptId(@Param("deptId") Integer deptId);
<!--
处理一对多的映射关系:
1.collection:处理一对多的映射关系(处理集合类型的属性)
2.分步查询
ofType:设置集合类型的属性中存储的数据的类型
-->
<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>
<!--Dept getDeptAndEmpByDeptId(@Param("deptId") Integer deptId)-->
<select id="getDeptAndEmpByDeptId" resultMap="deptAndEmpResultMap">
select * from t_dept
left join t_emp on
t_dept.dept_id = t_emp.dept_id
where t_dept.dept_id = #{
deptId}
</select>
测试
@Test
public void testGetDeptAndEmpByDeptId(){
SqlSession sqlSession = sqlSessionUtil.getSqlSession();
DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
Dept deptAndEmpByDeptId = mapper.getDeptAndEmpByDeptId(1);
System.out.println(deptAndEmpByDeptId);
}
8.3.2、分步查询
Dept
private List<Emp> emps;
DeptMapper
//分步查询(1)查询部门以及部门中的员工
Dept getDeptAndEmpByStepOne(@Param("deptId") Integer deptId);
<resultMap id="deptAndEmpResultMapStep" type="dept">
<id column="dept_id" property="deptId"></id>
<result column="dept_name" property="deptName"></result>
<collection property="emps"
fetchType="eager"
select="com.gao.mybatis.mapper.EmpMapper.getDeptAndEmpByStepTwo"
column="dept_id">
</collection>
</resultMap>
<!--Dept getDeptAndEmpByStepOne(@Param("deptId") Integer deptId);-->
<select id="getDeptAndEmpByStepOne" resultMap="deptAndEmpResultMapStep">
select * from t_dept where dept_id = #{
deptId}
</select>
EmpManner
//分步查询(2)查询部门以及部门中的员工
List<Emp> getDeptAndEmpByStepTwo(@Param("deptId") Integer deptId);
!--List<Emp> getDeptAndEmpByStepTwo(@Param("deptId") Integer deptId);-->
<select id="getDeptAndEmpByStepTwo" resultType="emp">
select * from t_emp where dept_id = #{
deptId};
</select>
测试
@Test
public void testGetDeptAndEmpByStep(){
SqlSession sqlSession = sqlSessionUtil.getSqlSession();
DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
Dept deptAndEmpByStepOne = mapper.getDeptAndEmpByStepOne(1);
System.out.println(deptAndEmpByStepOne);
}
边栏推荐
- Turn: Management is the love of possibility, and managers must have the courage to break into the unknown
- 符号表
- C Expert Programming Chapter 5 Thinking about Linking 5.2 Advantages of Dynamic Linking
- 文献管理工具 | Zotero
- System design. Seckill system
- 力扣:509. 斐波那契数
- 字节最爱问的智力题,你会几道?
- 部署LVS-DR群集【实验】
- Gartner 权威预测未来4年网络安全的8大发展趋势
- System design. How to design a spike system (full version transfer)
猜你喜欢

你以为border-radius只是圆角吗?【各种角度】

一个对象引用的思考

【流程图】

What is the salary of a software testing student?

震惊,99.9% 的同学没有真正理解字符串的不可变性

高性能高可靠性高扩展性分布式防火墙架构

解决错误:npm WARN config global `--global`, `--local` are deprecated
![[21 Days Learning Challenge] Image rotation problem (two-dimensional array)](/img/51/fb78f36c71e1eaac665ce9f1ce04ea.png)
[21 Days Learning Challenge] Image rotation problem (two-dimensional array)

npm安装依赖报错npm ERR! code ENOTFOUNDnpm ERR! syscall getaddrinfonpm ERR! errno ENOTFOUND

SLSA 框架与软件供应链安全防护
随机推荐
See how DevExpress enriches chart styles and how it empowers fund companies to innovate their business
动态规划总括
编程大杂烩(四)
触觉智能分享-SSD20X实现升级显示进度条
擎朗智能全国研发创新中心落地光谷:去年曾获2亿美元融资
npm init [email protected] 构建项目报错SyntaxError: Unexpected token ‘.‘解决办法
处理List<Map<String, String>>类型
C专家编程 第4章 令人震惊的事实:数组和指针并不相同 4.1 数组并非指针
你以为border-radius只是圆角吗?【各种角度】
BFC、IFC、GFC、FFC概念理解、布局规则、形成方法、用处浅析
C Expert Programming Chapter 4 The Shocking Fact: Arrays and Pointers Are Not the Same 4.3 What is a Declaration and What is a Definition
leetcode 12. 整数转罗马数字
[One step in place] Jenkins installation, deployment, startup (complete tutorial)
在被面试官说了无数次后,终于潜下心来整理了一下JVM的类加载器
解决错误:npm WARN config global `--global`, `--local` are deprecated
2022年PMP考试延迟了,该喜该忧?
心余力绌:企业面临的软件供应链安全困境
C专家编程 第5章 对链接的思考 5.4 警惕Interpositioning
How to keep the source code confidential in the development under the burning scenario
The symbol table