标签:time() gettime 对比 userinfo erb 最好 调用 type 分享
最近几天闲下来,主动把之前的代码优化了一下:)
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 }
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>
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标签的使用
标签:time() gettime 对比 userinfo erb 最好 调用 type 分享
原文地址:http://www.cnblogs.com/quiet-snowy-day/p/6166340.html