MyBatis resultMap id标签的错误使用方式
作者:BEESTACK 发布时间:2022-02-01 05:25:37
标签:MyBatis,resultMap,id标签
MyBatis resultMap id标签的错误使用
我们在编写VO对象,如果业务场景稍微复杂一点,就会用到集合属性。例如用户查看个人订单列表,每个订单又包含多种或者多个规格的商品。
本节的问题主要是我对mybatis id标签的错误使用
id是resultMap以及Collection的子标签,标记出作为 ID 的结果可以帮助提高整体性能。特别注意的是,id是当前命名空间中的一个唯一标识,用于标识一个结果映射。
如下图,itemId(商品id)字段值在数据库中不唯一,错误使用会导致只返回该订单某商品的一条记录。因为对于某个商品,麻辣味和五香味只是商品规格,其商品id是相同的。
改用普通result标签后,返回正确结果。
EOF
resultMap标签的使用规则
自定义结果映射规则
<!-- resultMap自定义某个javabean的封装规则
? ? ? ?type:自定义规则的java类型
? ? ? ?id:唯一id方便引用
? ? ?-->
? ? <resultMap type="entity.Employee" id="getEmpByIdMap">
? ? ? ?<!-- id指定主键列的封装规则
? ? ? ? ? ?column:指定哪一列
? ? ? ? ? ?property:指定对应的javabean属性
? ? ? ? -->
? ? ? ?<id column="id" property="id"/>
? ? ? ?<!-- result定义普通列封装规则,若属性名与数据库对应表的列名相同可不写,
? ? ? ? ? ? mybatis会自动封装,但建议将所有的映射规则都写上
? ? ? ?-->
? ? ? ?<result column="name" property="name"/>
? ? ? ?<result column="sex" property="sex"/>
? ? ? ?<result column="email" property="email"/>
? ? </resultMap>
? ? <!-- public Employee getEmpById(Integer id) -->
? ? <select id="getEmpById" resultMap="getEmpByIdMap">
? ? ? ?select * from employee where id=#{id}
? ? </select>
association联合查询
association
可以指定联合的javabean对象property="dept"
:指定哪个属性是联合对象javaType
:指定这个属性的类型
<resultMap type="entity.Employee" id="getEmpAndDeptMap">
? ? ? ?<id column="id" property="id"/>
? ? ? ?<result column="empName" property="name"/>
? ? ? ?<result column="sex" property="sex"/>
? ? ? ?<result column="email" property="email"/>
? ? ? ?<!-- association可以指定联合的javabean对象
? ? ? ? ? ? property="dept":指定哪个属性是联合对象
? ? ? ? ? ? javaType:指定这个属性的类型-->
? ? ? ?<association property="dept" javaType="entity.Department">
? ? ? ? ? ?<id column="did" property="id"/>
? ? ? ? ? ?<result column="deptName" property="departmentName"/>
? ? ? ?</association>
? ? </resultMap>
? ? <!-- public Employee getEmpAndDept(Integer id) -->
? ? <select id="getEmpAndDept" resultMap="getEmpAndDeptMap">
? ? ? ?select e.id id,e.name empName,e.email email,e.sex sex,e.d_id d_id,
? ? ? ? ? ?d.id did,d.name deptName from employee e,dept d
? ? ? ? ? ?where e.d_id=d.id and e.id=#{id}
? ? </select>
使用association进行分布查询
1、先按照员工id查询员工信息将会调用查询员工的sql
2、根据查询员工信息中的d_id值去部门表中查出部门信息
3、部门设置到员工中
<resultMap type="entity.Employee" id="getEmpAndDeptStepMap">
? ? ? ?<id column="id" property="id"/>
? ? ? ?<result column="name" property="name"/>
? ? ? ?<result column="sex" property="sex"/>
? ? ? ?<result column="email" property="email"/>
? ? ? ?<!-- association定义关联对象的封装规则
? ? ? ? ? ? select:表明当前属性是调用select指定的方法查出的结果
? ? ? ? ? ? column:指定将那一列的值作为参数传给这个方法
? ? ? ? ? ? ?流程:使用select指定的方法(传入column指定的这列参数的值)查出对象,
? ? ? ? ? ? ?并封装给property指定的属性
? ? ? ? ? ? -->
? ? ? ? ? ? <!-- discriminator鉴别器
? ? ? ? ? ? ? ? ?column:指定判定的列名
? ? ? ? ? ? ? ? ?javaType:列值对应的java类型
? ? ? ? ? ? ?-->
? ? ? ?<discriminator javaType="string" column="sex">
? ? ? ? ? ?<!-- resultType不能缺少 -->
? ? ? ? ? ?<case value="男" resultType="entity.Employee">
? ? ? ? ? ? ? <association property="dept" select="dao.DepartmentMapper.getDeptById"
? ? ? ? ? ? ? ? ? column="d_id">
? ? ? ? ? ? ? </association>
? ? ? ? ? ?</case>
? ? ? ?</discriminator>
? ? </resultMap>
? ? <!-- public Employee getEmpByIdStep(Integer id) -->
? ? <select id="getEmpByIdStep" resultMap="getEmpAndDeptStepMap">
? ? ? ?select * from employee where id=#{id}
? ? </select>
嵌套结果集的方式,使用collection标签定义关联的集合类型的属性封装规则
<resultMap type="entity.Department" id="getDeptByIdPlusMap">
? ? ? ?<id column="did" property="id"/>
? ? ? ?<result column="deptName" property="departmentName"/>
? ? ? ?<!-- collection定义关联集合类型的属性的封装规则
? ? ? ? ? ? ofType:指定集合里面元素的类型 ? ? ? ? ? ??
? ? ? ? -->
? ? ? ?<collection property="emps" ofType="entity.Employee">
? ? ? ? ? ?<!-- 定义这个集合中元素的封装规则 -->
? ? ? ? ? ?<id column="eid" property="id"/>
? ? ? ? ? ?<result column="empName" property="name"/>
? ? ? ? ? ?<result column="sex" property="sex"/>
? ? ? ? ? ?<result column="email" property="email"/>
? ? ? ?</collection>
? ? </resultMap>
? ? <!-- public Department getDeptByIdPlus(Integer id) -->
? ? <select id="getDeptByIdPlus" resultMap="getDeptByIdPlusMap">
? ? ? ?select d.id did,d.name deptName,e.id eid,
? ? ? ? ? ?e.name empName,e.sex,e.email
? ? ? ? ? ?from dept d left join employee e
? ? ? ? ? ?on d.id=e.d_id
? ? ? ? ? ?where d.id=#{id}
? ? </select>
collection分步查询
<resultMap type="entity.Department" id="getDeptByIdStepMap">
? ? ? ?<id column="id" property="id"/>
? ? ? ?<result column="name" property="departmentName"/>
? ? ? ?<collection property="emps" select="dao.EmployeeMapperPlus.getEmpsByDeptId"
? ? ? ? ? ?column="{id}">
? ? ? <!-- 或则 column="{deptId=id}"-->
? ? ? ?</collection>
? ? </resultMap>
? ?<!-- public List<Employee> getEmpsByDeptId(Integer deptId -->
? ?<select id="getEmpsByDeptId" resultType="entity.Employee">
? ? ? ?select * from employee where d_id=#{deptId}
? ? </select>
? ? <!-- public Department getDeptByIdStep(Integer id) -->
? ? <select id="getDeptByIdStep" resultMap="getDeptByIdStepMap">
? ? ? ?select * from dept where id=#{id}
? ? </select>
当分布查询需要传递多个多个值时,将多个值封装map传递
colum=“{key1=column1,key2=colum2...}”
来源:https://blog.csdn.net/LIZHONGPING00/article/details/109148269


猜你喜欢
- Java 8的18个常用日期处理一、简介伴随lambda表达式、streams以及一系列小优化,Java 8 推出了全新的日期时间API。J
- 前言了解一下将 Android library 发布到中央仓库(比如 Maven Center,jitpack) 的过程中关于一些细节的疑惑
- 提到输入参数的基本验证(非空、长度、大小、格式…),在以前我们还是通过手写代码,各种if、else、StringUtils.isEmpty、
- 1、首先创建一个测试实体类Person,并携带如上注解,其注解的作用描述在messagepackage com.clickpaas.pojo
- 需要添加引用,System.Configuration;写系统配置文件: Configuration cfa =
- 声明类定义类:class MyClass { // 字段、构造函数和 // 方法声明}
- Java游戏俄罗斯方块的实现实例 java小
- 本文实例讲述了C#使用RichTextBox实现替换文字及改变字体颜色功能。分享给大家供大家参考,具体如下:替换文字private void
- 本文实例为大家分享了Java实现医院管理系统的具体代码,供大家参考,具体内容如下1.开发工具NetBeans8.2Mysql5.7mysql
- 1. this 引用1.1 为什么要有this引用先来写一个日期类的例子:public class classCode { &
- 今天看了一下数据结构,一个练习就是构建哈夫曼树,就顺手用C#写了一个。static void Main(string[] args){ &n
- 第1部分 HashSet介绍HashSet 简介HashSet 是一个没有重复元素的集合。它是由HashMap实现的,不保证元素的顺序,而且
- 一、DataGridView不显示下面的新行通常DataGridView的最下面一行是用户新追加的行(行头显示*)。如果不想让用户新追加行即
- java语言的输入输出功能是十分强大而灵活的,美中不足的是看上去输入输出的代码并不是很简洁,因为你往往需要包装许多不同的对象。在Java类库
- 由于最近的工作需要用到文本转语音的功能,在网上找到的资料有些不完整,特此记录下整个完整功能。这种方式的优点在于不会被浏览器限制,在js的文本
- 本文实例讲述了Android开发实现ImageView加载摄像头拍摄的大图功能。分享给大家供大家参考,具体如下:这个方法是从官方demo中摘
- 概述企业内部一般都有一套单点登录系统(常用的实现有apereo cas),所有的内部系统的登录认证都对接它。本文介绍spring boot的
- 本文实例讲述了Android游戏开发学习①弹跳小球实现方法。分享给大家供大家参考。具体如下:在学习了一点点Android之后,觉得有必要记录
- SpringBoot实现单文件上传功能,供大家参考,具体内容如下架构为springboot+thymeleaf,采用ajax方式提交1. 页
- Intent是一种运行时绑定(run-time binding)机制,它能在程序运行过程中连接两个不同的组件。通过Intent,你的程序可以