性能优化之mybatis实现接口的批量查询,减少数据库的查询消耗

Posted qmfsun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了性能优化之mybatis实现接口的批量查询,减少数据库的查询消耗相关的知识,希望对你有一定的参考价值。

    <select id="selectByTime" resultType="com.neo.xnol.api.activity.dto.ActivityMqmsgDTO">
        SELECT
            id,
            eventType,
            eventId,
            userId,
            userName,
            content,
            msgStatus,
            retryCount,
            errorDetails,
            createTime,
            updateTime,
            msgVersion,
            sysVersion
        FROM
            t_activity_mqmsg
        WHERE
            userId= #userId
        AND
            eventType IN
            <foreach collection="eventTypeList" index="index" item="item" open="(" separator="," close=")">
                  #item
            </foreach>
        AND
            createTime &gt;= #beginTime
        AND
            createTime &lt;= #endTime
        ORDER BY
            createTime
    </select>

 

    <select id="findActivityInfoListByCategoryId" resultMap="BaseResultMap" >
        SELECT
            a.*
        FROM  t_activity_info a
        WHERE
        a.categoryId in
        <foreach collection="list" item="categoryId" open="(" close=")"
            separator=",">
            #categoryId
        </foreach>
        and a.isDelete = 0
        and a.`endTime` > now()
        <if test="organizationId != null and organizationId != 3">
            AND a.organizationId = #organizationId,jdbcType=INTEGER
        </if>
        ORDER BY a.createTime DESC
    </select>

 

  <select id="selectOneByRewardType" resultMap="BaseResultMap">
    select w.* 
      from t_lucky_winning w 
     inner join t_lucky_activity a 
        on w.luckyId = a.luckyId and a.activityId = #activityId,jdbcType=INTEGER
     inner join t_lucky_reward r on w.rewardId = r.id
     where r.rewardId in(
     <foreach collection="rewardIds" item="rewardId" separator=",">
         #rewardId,jdbcType=INTEGER
     </foreach>)
     order by w.luckytime desc
     limit 1
  </select>

 

以上是关于性能优化之mybatis实现接口的批量查询,减少数据库的查询消耗的主要内容,如果未能解决你的问题,请参考以下文章

Mybatis批量更新优化方案

MYSQL之高性能的mysql--查询性能优化

《高性能MySQL》之MySQL查询性能优化

mysql小技能:批量插入性能优化

Mybatis Plus 批量插入性能优化,非常实用!

MyBatis配置的优化