JAVA语言mybatisplus框架查询数据库时间过长优化

以前曾经优化过一个前人留下代码,数据库查询结果,循环读取某个字段为索引查询其他字典表,读取结果取字段名赋值给原结构里扩展字段,看到这个代码就有点吐血

后来我就优化成系统起来先把字典表读出来放内存,即便是需要实时变化的,查询前更新字典列表内存也比循环里查数据库要好

今天又碰到一个代码查询,初步看来问题不大,用的框架查询,分页,结果只返回15条数据竟然花费1分多钟,看之前我就断言,肯定有优化空间

仔细研究后发现原来是主表数据在百万级,然后关联4个表,关联结果还在sql里计算和判断,最后我就用字典表提前查询,然后自定义sql分页查询主表,然后聚合主表关键字段查询关联表,然后循环把几个list合并,最后优化成2秒以内,好吧虽然还有进一步改进点,但也不想再费事了,这已经够用了。

下面是在mapper.xml里使用到的主表的sql语句配置

 <sql id="listRideHistoryWhere">
        <where>
            <if test="query.mediaType != null and query.mediaType != '' ">
                teer.media_type = #{query.mediaType}
            </if>            
        </where>
    </sql>
    <select id="listRideHistory"
        parameterType="com.model.RideHistoryQuery"
        resultType="com.dto.TxnEntryExitRecordDto">
        select * from txn_entry_exit_record teer
        <include refid="listRideHistoryWhere"/>
        order by teer.id DESC limit #{query.page.index},#{query.page.size}
    </select>
    <select id="pageRideHistory_COUNT" resultType="Long" useCache="false"  parameterType="com.model.RideHistoryQuery">
        SELECT  COUNT(1)  FROM txn_entry_exit_record teer
        <include refid="listRideHistoryWhere"/>
    </select>

下面是主程序逻辑

 List<DictStation> dictStations= TmsCacheData.getInstance().getDictStations();
            if(CollectionUtils.isEmpty(dictStations)){
                dictStations=dictStationMapper.selectAllStation();
                if(!CollectionUtils.isEmpty(dictStations)){
                    TmsCacheData.getInstance().setDictStations(dictStations);
                }
            } 
 if(rideHistoryQuery.getPage().getIndex()==null){
                rideHistoryQuery.getPage().setIndex(0);
            }
            if(rideHistoryQuery.getPage().getCurrent()>0 &&  rideHistoryQuery.getPage().getIndex()==0){
                rideHistoryQuery.getPage().setIndex(rideHistoryQuery.getPage().getCurrent()*rideHistoryQuery.getPage().getSize());
            }
            List<TxnEntryExitRecordDto> records=txnEntryExitRecordMapper.listRideHistory(rideHistoryQuery);
            String tickets=records.stream().map(TxnEntryExitRecordDto::getMediaNumber).collect(Collectors.joining(","));
            List<TxnTicketStub> ticketList=txnTicketStubMapper.selectByTicketNumber(tickets);
            List<TxnFineStub> fineList=txnFineStubMapper.selectByTicketNumber(tickets);
            for(TxnEntryExitRecordDto record:records){
                DictStation dictStation=dictStations.stream().filter(e-> (e.getStationNumber().equals( record.getStationNumber()))).findFirst().orElse(null);
                record.setStationName(dictStation);
                if(record.getMediaType().equals("1")) {
                    TxnTicketStub txnTicketStub = ticketList.stream().filter(e -> (e.getTicketNumber().equals(record.getMediaNumber()))).findFirst().orElse(null);
                    if (txnTicketStub != null) {                       
                        record.setPrice(txnTicketStub.getActualPayment());                        
                    }

                }else {
                    TxnFineStub txnFineStub = fineList.stream().filter(e -> (e.getTicketNumber().equals(record.getMediaNumber()))).findFirst().orElse(null);
                    if (txnFineStub != null) {
                        record.setPrice(txnFineStub.getFineAmount());
                    }
                }
                record.setContractType("");
            }
            page.setTotal(txnEntryExitRecordMapper.pageRideHistory_COUNT(rideHistoryQuery));
            page.setRecords(records);

其中用到list的流用法,能把代码简化很多。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MybatisPlus是一个基于Mybatis框架的增强工具,可以简化和加速开发过程。在使用MybatisPlus进行数据库操作时,我们可以利用它自带的功能来进行基本的CRUD操作,同时也可以进行自定义的映射resultmap。 自定义映射resultmap可以帮助我们在数据库查询结果与实体类之间建立映射关系,进而方便地操作数据。一般情况下,MybatisPlus会根据数据库字段与实体类属性的对应关系自动进行映射,但在某些特殊情况下,我们可能需要自己来定义映射关系。 要自定义映射resultmap,我们可以通过在实体类使用注解`@ResultMap`来实现。首先,我们需要在实体类定义与数据库字段对应的属性,并通过注解`@TableField`来指定数据库字段名。然后,我们可以在实体类使用注解`@ResultMap`来声明自定义的映射resultmap,并指定与数据库字段对应的属性。 例如,有一个数据库表student,包含字段id、name和age,我们可以定义一个实体类Student,其id属性对应数据库字段id,name属性对应数据库字段name,age属性对应数据库字段age。然后,在Student类使用注解`@ResultMap`自定义映射resultmap,例如: ```java @Data public class Student { @TableField("id") private Long id; @TableField("name") private String name; @TableField("age") private Integer age; @ResultMap("studentResultMap") public class StudentResultMap{ return new StudentResultMap(){ put("id", "id"); put("name","name"); put("age","age"); } } } ``` 在上述示例,我们定义了一个名为studentResultMap的自定义映射resultmap,并通过`put`方法指定了属性与数据库字段的对应关系。 当我们需要进行数据库查询时,可以通过`@Select`注解或者使用MybatisPlus提供的查询方法来执行查询操作。在查询操作,我们可以使用自定义映射resultmap来获取查询结果,并将其映射到实体类。 总之,通过自定义映射resultmap,我们可以灵活地实现数据库查询结果与实体类的映射关系,提高了系统的可维护性和代码的可读性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值