Mybatis中的collection、association来处理结果映射

时间:2021-09-13 22:55:21
前不久的项目时间紧张,为了尽快完成原型开发,写了一段效率相当低的代码。

最近几天闲下来,主动把之前的代码优化了一下:)

 
标签:Java、Mybatis、MySQL
概况:本地系统从另外一个系统得到实体类集合List<UserEvent>,但是实体中只有eventId信息,其他属性值均为空。
需要从数据库中查询数据,完善List<UserEvent>的信息并返回。
相关业务表以及对应的实体类,如下图。(为了回避项目信息,相关业务内容均省略,以下表名、实体名、代码变量名等均用字母ABC代替。)
Mybatis中的collection、association来处理结果映射

原处理

1.先来看代码,乍一看逻辑清晰,符合正常思维习惯。
但是仔细查看发现,for循环中的每步处理都和数据查询有关。假设有10次循环,每次循环中有4次数据库连接查询,一共需要连接数据库40次。
每次数据库连接都需要一定的开销,随着循环量不断增加,处理时间将成倍增长,造成资源浪费。
Mybatis中的collection、association来处理结果映射
 1  String eventId = "";
2 String aTime = "";
3 for (UserEvent event : userEventList) {
4 eventId = event.getEventId();
5
6 // 获取业务B信息
7 List<EntityB> listB = mapperB.selectBInfoByEventId(eventId);
8 event.setListB(listB);
9
10 // 获取业务C信息
11 List<EntityC> listC = mapperC.selectCInfoByEventId(eventId);
12 event.setListC(listC);
13
14 // 查看是否有业务B处理
15 EntityB entityB = mapperB.selectBInfoByPrimary(phone, eventId);
16 event.setIsActionB(null == entityB ? "false" : "true");
17
18 // 获取业务A和用户信息
19 User userInfo = mapperA.selectEventUserInfoByEventId(eventId);
20 if(null != userInfo){
21 aTime = userInfo.getTime();
22 event.setTime(aTime == null ? "" : sdfMd.format(sdfYmd.parse(aTime)));
23 event.setUserInfo(userInfo);
24 }
25 }
Mybatis中的collection、association来处理结果映射
2.再来看查询语句。
业务表A和业务表B没有复杂的查询。只有业务表C使用了一个子查询,来获取表内自身数据引用的信息。
各业务表数据都需要关联到用户表User。
Mybatis中的collection、association来处理结果映射
 1   <select id="selectBInfoByEventId" parameterType="String" resultType="EntityA">
2 SELECT
3 B.phone AS phone,
4 B.time AS time,
5 U.name AS userName
6 FROM table_b B
7 LEFT JOIN user U ON U.phone = B.phone
8 WHERE B.event_id = #{eventId}
9 ORDER BY B.time ASC
10 </select>
11 <select id="selectCInfoByEventId" parameterType="String" resultType="EntityC">
12 SELECT
13 C.cmtId,
14 C.referId,
15 C.time,
16 U.name AS userName
17 ( SELECT TU.name FROM table_c TC
18 LEFT JOIN user TU ON TU.phone = TC.phone
19 WHERE TC.cmt_id = TC.refer_id
20 ) AS referName
21 FROM table_c C
22 LEFT JOIN user U ON C.phone = U.phone
23 WHERE C.event_id = #{eventId}
24 ORDER BY C.time ASC
25 <select id="selectEventUserInfoByEventId" parameterType="java.lang.String" resultType="User">
26 SELECT
27 U.name,
28 U.picId,
29 A.time
30 FROM table_a A
31 LEFT JOIN user U ON U.phone = A.phone
32 WHERE A.event_id = #{eventId}
33 </select>
Mybatis中的collection、association来处理结果映射

优化分析

  1. 在代码结构上,要避免在for循环中作查询处理。考虑将查询参数evenId从for循环中提取出来,做批量查询,然后再将查询结果设定到对应的实体类中。
  2. 在业务上,对于每一个UserEvent中的eventId,业务表A中必定对应有一条记录,而在业务表B和业务表C中则未必有与这个eventId关联的数据。因此,可以将业务表A作为主表,通过eventId与另外几个表关联查询。查询次数也由原来的至少四次减少为一次查询。
  3. 对于联合查询的结果,以UserEvent作为查询结果的实体类,使用Mybatis中的collection、association来处理结果映射。
  4. 另外,各业务表的查询中都有与用户表User的关联,考虑将各业务信息的查询处理创建为视图。这样不仅能简化联合查询中SQL语句,也可以隔离基础表的数据。

优化后的代码

Mybatis中的collection、association来处理结果映射
  int eventSize = userEventList.size();
List<String> eventIds = new ArrayList<String>();
// 如果考虑去掉重复数据,可以使用集合Set,但是作为Mybatis的输入参数,最后还是需要将Set转化为List。
// 此处直接使用List,因为在业务上排除了重复数据的可能性。
for (int i = 0; i < eventSize; i++) {
eventIds.add(userEventList.get(i).getEventId());
}
Map<String, Object> paramsMap = new HashMap<String, Object>();
paramsMap.put("eventIds", eventIds);
paramsMap.put("phone", phone);
List<UserEvent> eventInfoList = eventMapper.selectUserEventInfo(paramsMap); // 将查询结果转化为Map存储,方便调用
Map<String, UserEvent> eventInfoMap = new HashMap<String, UserEvent>();
for(UserEvent event : eventInfoList){
eventInfoMap.put(event.getEventId(), event);
}
UserEvent newEvent = null;
String aTime = null;
for(UserEvent event : roadEventList){ // 从查询结果Map中取出补充信息,保存到原UserEvent对象中
newEvent =eventInfoMap.get(event.getEventId());
if(null != newEvent ){
aTime = newEvent.getTime();
event.setTime(aTime == null ? "" : sdfMd.format(sdfYmd.parse(aTime )));
event.setIsActionB(newEvent.getIsActionB() == null ? "false" : newEvent.getIsActionB());
event.setUserInfo(newEvent.getUserInfo());
event.setListB(newEvent.getListB());
event.setListC(newEvent.getListC());
}
}
Mybatis中的collection、association来处理结果映射
Mybatis中的collection、association来处理结果映射
    <resultMap id="UserMap" type="User">
<result column="name" property="name" />
<result column="picId" property="picId" />
<result column="time" property="time" />
</resultMap> <resultMap id="BMap" type="EntityB">
<id column="bPhone" property="phone" />
<result column="bUserName" property="userName" />
<result column="bTime" property="time" />
</resultMap> <resultMap id="CMap" type="EntityC">
<id column="cmtId" property="cmtId" />
<result column="referId" property="referId" />
<result column="cUserName" property="userName" />
<result column="referName" property="referName" />
<result column="cTime" property="time" />
</resultMap> <resultMap id="EventResultMap" type="com.xxxx.bean.UserEvent">
<id column="eventId" property="eventId" />
<result column="time" property="time" />
<result column="isActionB" property="isActionB" />
<association property="userInfo" resultMap="UserMap" />
<collection property="listB" resultMap="BMap" />
<collection property="listC" resultMap="CMap" />
</resultMap> <select id="selectUserEventInfo" resultMap="EventResultMap">
SELECT
A.eventId,
A.time,
A.name,
A.picId,
CASE WHEN B.phone = #{phone} THEN "true" ELSE "false" END AS isActionB,
B.phone AS bPhone,
B.userName AS bUserName,
B.time AS bTime,
C.cmtId,
C.referId,
C.userName AS cUserName,
C.referName AS referName,
C.time AS cTime
FROM v_table_a A
LEFT JOIN v_table_b B ON B.eventId = A.eventId
LEFT JOIN v_table_c C ON C.eventId = A.eventId
<where>
A.event_id in
<foreach collection="eventIds" index="" item="eventId"
            open="(" separator="," close=")">
#{eventId}
</foreach>
</where>;
</select>
Mybatis中的collection、association来处理结果映射

编码时需要注意的几个地方

1. 复杂对象的映射解析

采用resultMap嵌套。其中,collection标签表示映射一个集合,association标签表示映射一个实体类,
标签中的property属性值对应的是,该集合/实体在查询结果对象中的变量名。
 
对于各表中名称相同的字段,需要建立别名,否则解析时无法确定各属性与表字段的对应关系。
如:业务表B和业务表C中都有userName字段,在查询语句中为为字段别名加了前缀来区分。
B.userName AS bUserName, <result column="bUserName"property="userName"/>
C.userName AS cUserName, <result column="cUserName" property="userName" />
 
resultMap中type属性表示标签所包含内容对应映射的Java类。
该属性可以写类的全路径(如:<resultMap id="EventResultMap" type="com.xxxx.bean.UserEvent"> ),
也可以配置为简写的类名(如:<resultMap id="UserMap" type="User"> )。
简写的类名需要在xml配置文件中设置(如下),配好之后的简写类名可以在各个sql.xml中使用。
Mybatis中的collection、association来处理结果映射
  <!-- spring-mybatis.xml文件 -->
<!-- 配置sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
     <property name="dataSource" ref="dataSource" />
    <!-- 将各Java类的简写别名单独放到文件mybatis.xml中,方便修改和管理 -->
     <property name="configLocation" value="classpath:xml/mybatis.xml" />
     <property name="mapperLocations" value="classpath:sql/*.xml" />
</bean>
Mybatis中的collection、association来处理结果映射
Mybatis中的collection、association来处理结果映射
  <!-- mybatis.xml文件 -->
<configuration>
    <typeAliases>
         <typeAlias alias="EntityA" type="com.xxxx.model.EntityA" />
         <typeAlias alias="EntityB" type="com.xxxx.model.EntityB" />
        <typeAlias alias="EntityC" type="com.xxxx.model.EntityC" />
        <typeAlias alias="User" type="com.xxxx.model.User" />
    </typeAliases>
</configuration>
Mybatis中的collection、association来处理结果映射

2. foreach标签的使用

如果查询接口只有一个参数,参数类型为list,则标签中的collection属性应该设定collection="list";参数类型为数组,则应设定为collection="array"
如果查询接口有多个参数,则最好通过Map来传递各参数。此时,foreach标签的collection属性应设置为,Map中表示集合参数的键。
如上面的代码中,表示集合参数是eventIds,它在Map中的键为"eventIds" ,所以collection="eventIds"

处理时间对比

各表数据量在200、300条左右,List<UserEvent>集合记录为13条。
虽然优化后的代码行数有所增加,查询结果解析略微复杂,但是十几条数据的查询已有2秒的差距。
 Mybatis中的collection、association来处理结果映射

http://www.cnblogs.com/quiet-snowy-day/p/6166340.html

Mybatis中的collection、association来处理结果映射的更多相关文章

  1. Mybatis中使用collection进行多对多双向关联示例(含XML版与注解版)

    Mybatis中使用collection进行多对多双向关联示例(含XML版与注解版) XML版本: 实体类: @Data @NoArgsConstructor public class Course ...

  2. 谈一下思考&comma;关于mybatis中&lt&semi;foreach collection&equals;&quot&semi;list&quot&semi;&gt&semi;中list得来的原因 没看到官方说明

    <foreach> 是在sql语句中进行多个id查询 时用到的,因为mybatis代替jdbc和hibernate, 使用 在xml文件中编写sql语句,这是一个标签文件.然后在 dao层 ...

  3. Mybatis中的collection和association一关系

    collection 一对多和association的多对一关系 学生和班级的一对多的例子 班级类: package com.glj.pojo; import java.io.Serializable ...

  4. mybatis 中 foreach collection的三种用法(转)

    文章转自 https://blog.csdn.net/qq_24084925/article/details/53790287 oreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集 ...

  5. mybatis 中 foreach collection的三种用法

    foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有 item,index,collection,open,separator,close. ...

  6. Mybatis 中 foreach collection 的三种用法

    foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有 item,index,collection,open,separator,close. ...

  7. mybatis中foreach collection的三种用法

    foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有 item,index,collection,open,separator,close. ...

  8. MyBatis 中 resultMap 详解

    resultMap 是 Mybatis 最强大的元素之一,它可以将查询到的复杂数据(比如查询到几个表中数据)映射到一个结果集当中.如在实际应用中,有一个表为(用户角色表),通过查询用户表信息展示页面, ...

  9. mybatis中常见的问题总结

    如下所有举例基于springboot+mybatis项目中,SSH使用mybatis的写法也一样,只是形式不同而已 问题1.org.apache.ibatis.binding.BindingExcep ...

随机推荐

  1. &lt&semi;&lt&semi;&lt&semi; 如何查看自己是外网还是内网

    判断的方法很简单,就是看你的网络中有没有路由器,不管是有线路由还是无线路由,只要你的网络中用了路由,那你就是内网,用路由器的网络有一个特点,那就是只要路由器在开着,那你开了电脑之后就可以直接上网,不需 ...

  2. 【团队项目演示】FZU5BOYS之团队项目链接汇总

    FZU5BOYS      项目冲刺之博客汇总 Alpha版本 Day One Day Two Day Three Day Four Day Five Day Six Day Seven Day Ei ...

  3. php--某个字符在字符串中的位置比较

    <?php $haystack = 'helloe'; $needle = 'e'; $pos = stripos($haystack, $needle); echo "\n&quot ...

  4. python之lxml&lpar;xpath&rpar;

    bs4确实没这个好用,bs4的树太复杂 lxml很好 定位非常好 详细解说在注释里面有了 #!/usr/bin/python3.4 # -*- coding: utf-8 -*- from lxml ...

  5. linux内核中与进程相关的数据结构(基于linux3&period;16-rc4)

    1.进程描述符 struct task_struct { volatile long state; ....... struct list_head tasks; ....... struct mm_ ...

  6. HW4&period;34

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  7. Linux内核配置浅析

    1.Linux Kernel Kconfig系统的基本结构 Linux内核的配置系统由三个部分组成,分别是: 1>.Makefile:分布在 Linux 内核源代码根目录及各层目录中,定义 Li ...

  8. poptest交流QQ群

    欢迎大家加入Poptest大家庭. 测试开发交流群-1 450192312 测试开发交流群2 195983133 POPtest-接口测试交流 376529971 POPtest-接口测试交流群 13 ...

  9. &lpar;转&rpar;Java对象克隆(Clone)及Cloneable接口、Serializable接口的深入探讨

    原文地址:http://blog.csdn.net/kenthong/article/details/5758884 Part I 没啥好说的,直接开始Part II吧. Part II 谈到了对象的 ...

  10. python 练习3

    简单计算器的实现: 计算:1-2*((60-30+(-40.0/5)*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2))' #!usr/bin/en ...