9月份技术问题汇总

本文主要是针对工作中初期遇到的问题,进行汇总和思考,警醒后续开发过程中更加仔细和能力提升

RPC远程调用问题汇总:

Question 1

  在远程调用此方法时:
image.png

  如果将Page改为IPage进行返回将会报错,信息如下:

Type definition error: [simple type, class com.baomidou.mybatisplus.core.metadata.IPage]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.baomidou.mybatisplus.core.metadata.IPage` (no Creators, like default constructor, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information

  这个错误信息表明在反序列化过程中,Jackson 无法实例化 IPage 接口,因为它是一个抽象类型,没有默认构造函数。

解决方法1. 使用具体实现类

  使用 MyBatis-Plus 提供的具体实现类,比如 Page<T>,而不是接口IPage;

解决方法2. 使用 Map 或 List

  如果只需要返回分页的数据而不需要 IPage 的所有功能,可以将结果映射到一个 Map 或 List,这样可以避免直接使用 IPage
  修改后及时上传Git,并发布到测试环境流水线
image.png

Question 2

  如果本地服务需要注册到Nacos,远程调用调试时不想走公司内部测试环境服务器,而是走本地服务端口,需要在打包前在feignclient中加上指定url。

解决方法 加上指定url

image.png
  端口需修改得与流水线中测试环境中服务端口不同.
  指令:-Dserver.port=8084

SQL问题汇总

Question 1

  因为门店日志记录等数据量会越来越大,所以禁止循环内有查询操作,选择用批量查询操作。如果需要查询符合条件的每家门店对应价签品牌的最新一条同步日志信息。
工作中错误的SQL案例:

SELECT * FROM elec_store_sync_day_log WHERE store_id IN ("33010212","33010699") 
AND channel="YTL" AND stat="" 
GROUP BY store_id ORDER BY id

  错误原因:SQL执行的顺序中GROUP BY 会先执行,ORDER BY 会后执行
也就是先分组后再根据Id进行排序,此时的排序是没有意义的,预期是在分组前对id(等同于日期排序,因为id自增)排序后,再分组,这样拿到的就是每个门店最新的一条。但是这条SQL语句并拿不到最新的记录。

解决方法:子查询
SELECT *
FROM elec_store_sync_day_log AS es
WHERE store_id IN ("33010212", "33010699","33010210") AND channel = "YTL" AND stat=0
AND id = (
    SELECT MAX(id)
    FROM elec_store_sync_day_log
    WHERE store_id = es.store_id AND channel = "YTL" AND stat=0
);

  查出本表满足条件的最大的id(最新日期),再根据此id查本条记录,相当于每家门店都有一条最新的记录。
  对应的XML文件代码。
  其中参数:@Param("request") FeignClientRequest request:

<select id="getPriceTagSynPage"
            resultType="com.xfsg.group.xfpos.migratedatacenter.poshd.dao.entity.ElecStoreSyncDayLogEntity">
        SELECT es.store_id      AS storeId
             , es.channel       AS channel
             , es.stat          AS stat
             , es.update_time   AS updateTime
        FROM elec_store_sync_day_log AS es
        WHERE store_id IN
        <foreach item="id" collection="request.storeIds" open="(" separator="," close=")">
            #{id}
        </foreach>
        <if test="request.channel != null and request.channel.trim() != ''">
            AND channel = #{request.channel}
        </if>
        <if test="request.stat != null">
            AND stat = #{request.stat}
        </if>
        AND id = (
        SELECT MAX(id)
        FROM elec_store_sync_day_log
        WHERE store_id = es.store_id
        <if test="request.channel != null and request.channel.trim() != ''">
            AND channel = #{request.channel}
        </if>
        <if test="request.stat != null">
            AND stat = #{request.stat}
        </if>
        )
    </select>

恐龙不画画
1 声望0 粉丝

仰天大笑出门去,我辈岂是蓬蒿人