【Java】记录一次代码优化
最近几天闲下来,主动把之前的代码优化了一下:)

原处理
String eventId = "";
String aTime = "";
for (UserEvent event : userEventList) {
eventId = event.getEventId(); // 获取业务B信息
List<EntityB> listB = mapperB.selectBInfoByEventId(eventId);
event.setListB(listB); // 获取业务C信息
List<EntityC> listC = mapperC.selectCInfoByEventId(eventId);
event.setListC(listC); // 查看是否有业务B处理
EntityB entityB = mapperB.selectBInfoByPrimary(phone, eventId);
event.setIsActionB(null == entityB ? "false" : "true"); // 获取业务A和用户信息
User userInfo = mapperA.selectEventUserInfoByEventId(eventId);
if(null != userInfo){
aTime = userInfo.getTime();
event.setTime(aTime == null ? "" : sdfMd.format(sdfYmd.parse(aTime)));
event.setUserInfo(userInfo);
}
}
<select id="selectBInfoByEventId" parameterType="String" resultType="EntityA">
SELECT
B.phone AS phone,
B.time AS time,
U.name AS userName
FROM table_b B
LEFT JOIN user U ON U.phone = B.phone
WHERE B.event_id = #{eventId}
ORDER BY B.time ASC
</select>
<select id="selectCInfoByEventId" parameterType="String" resultType="EntityC">
SELECT
C.cmtId,
C.referId,
C.time,
U.name AS userName
( SELECT TU.name FROM table_c TC
LEFT JOIN user TU ON TU.phone = TC.phone
WHERE TC.cmt_id = TC.refer_id
) AS referName
FROM table_c C
LEFT JOIN user U ON C.phone = U.phone
WHERE C.event_id = #{eventId}
ORDER BY C.time ASC
<select id="selectEventUserInfoByEventId" parameterType="java.lang.String" resultType="User">
SELECT
U.name,
U.picId,
A.time
FROM table_a A
LEFT JOIN user U ON U.phone = A.phone
WHERE A.event_id = #{eventId}
</select>
优化分析
- 在代码结构上,要避免在for循环中作查询处理。考虑将查询参数evenId从for循环中提取出来,做批量查询,然后再将查询结果设定到对应的实体类中。
- 在业务上,对于每一个UserEvent中的eventId,业务表A中必定对应有一条记录,而在业务表B和业务表C中则未必有与这个eventId关联的数据。因此,可以将业务表A作为主表,通过eventId与另外几个表关联查询。查询次数也由原来的至少四次减少为一次查询。
- 对于联合查询的结果,以UserEvent作为查询结果的实体类,使用Mybatis中的collection、association来处理结果映射。
- 另外,各业务表的查询中都有与用户表User的关联,考虑将各业务信息的查询处理创建为视图。这样不仅能简化联合查询中SQL语句,也可以隔离基础表的数据。
优化后的代码
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());
}
}
<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>
编码时需要注意的几个地方
1. 复杂对象的映射解析
<!-- 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.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>
2. foreach标签的使用
处理时间对比

【Java】记录一次代码优化的更多相关文章
- JAVA记录-java代码优化策略
java代码优化策略 1.生成对象时,合理分配空间和大小:new ArrayList(100); 2.优化for循环: Vector vect = new Vector(1000); For(int ...
- java记录在线人数小案例
文件目录: web.xml: <?xml version="1.0" encoding="UTF-8"?> <web-app version= ...
- JAVA记录-Servlet介绍
1.什么是Servlet Servlet是sun公司提供的一门用于开发动态web资源的技术.Sun公司在其API中提供了一个servlet接口,用户若想用发一个动态web资源(即开发一个Java程序向 ...
- JAVA记录-Mybatis介绍
1.什么是 MyBatis ? MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyB ...
- JAVA记录-JSP内容
JSP(JavaServer Pages )是什么? JavaServer Pages(JSP)是一种支持动态内容开发的网页技术它可以帮助开发人员通过利用特殊的JSP标签,其中大部分以<%开始并 ...
- java 记录对象前后修改的内容(工具类)
有时候业务需要,需记录一条记录的修改历史,但是不能为完成任务而硬编码,不靠谱 这种情况可以使用java反射来完成 对对象属性的描述可以通过自定义注解来完成,读取里面的属性进而记录修改历史. 在对象的属 ...
- java基础一(阅读Head First Java记录)
写在前面的话 在实际应用java中,因为没有系统去看过书或者学习过,所以基础薄弱,刚好这本书是比较入门级的一些书,记录一下下面的一些基本概念,以方便自己来学习.当然如果对大家有帮助也是很好的. 因为书 ...
- java记录linux top命令信息
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...
- 初学java记录
记录一: if语句: if(x < y) System.out.println("x is less than y"); 记录二: 强制转换字符类型赋值的方法: num2= ...
随机推荐
- java基本类型的默认值及其取值范围
- Nginx 配置从零开始
作为一个 nginx 的初学者记录一下从零起步的点滴. 基本概念 Nginx 最常的用途是提供反向代理服务,那么什么反向代理呢?正向代理相信很多大陆同胞都在这片神奇的土地上用过了,原理大致如下图: 代 ...
- Surface Shader简单向导
Surface Shader 表面着色器 描述 当你的Material要处理光照,则一般使用Surface Shader.Surface Shader隐藏了光照的计算,你只需要在surf函数里设置好反 ...
- Ajax是如何运行的?
1.我们需要知道什么是Ajax: AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML). AJAX 不是新的编程语言,而是一种使用现 ...
- 什么是jquery $ jQuery对象和DOM对象 和一些选择器
1什么是jQuery: jQuery就是将一些方法封装在一个js文件中.就是个js库 我们学习这些方法. 2为什么要学习jQuery: 原生js有以下问题: 1.兼容性问题2.代码重复3.DOM提供的 ...
- java.sql.SQLException: 关闭的连接
在Dao接口实现类里面的conn.close()之类的关闭数据库连接的代码注释掉就可以了. 可能还有别的解决方法,不过这样改比较方便.
- 安装SQL Server2016正式版
安装SQL Server2016正式版 今天终于有时间安装SQL Server2016正式版,下载那个安装包都用了一个星期 安装包可以从这里下载: http://www.itellyou.cn/ ht ...
- SQL Server 2016 CTP2.2 安装手记
SQL Server 2016 CTP2.2 安装手记 下载一个iso文件,解压出来(大约2.8G左右),在该路径下双击Setup.exe即可开始安装. 安装之前请先安装.NET 3.5 SP1,在服 ...
- ASP.NET Core Kestrel 中使用 HTTPS (SSL)
在ASP.NET Core中,如果在Kestrel中想使用HTTPS对站点进行加密传输,可以按照如下方式 申请证书 这一步就不详细说了,有免费的和收费的,申请完成之后会给你一个*.pfx结尾的文件. ...
- 系列篇|编译可在Android上运行的依赖库(一):glib库
前言 这是系列文章,它们由<编译可在Android上运行的glib库>及其他4篇文章组成,这4篇文章在“编译依赖库”一节中列出.由于glib库依赖于其他第三方库,所以需要先将依赖的第三方库 ...