关于字段注入

40 阅读2分钟
  • 在查询的时候,为了避免多表关联查询所带来的性能问题,使用字段注入的方法:
    • 先在实体类中添加字段:
    @TableField(exist = false)
    @Excel(name = "供应商编号")
    private String supplierNo;
    
    @TableField(exist = false)
    @Excel(name = "供应商名称")
    private String supplierName;
    
    • @TableField(exist = false):在MyBatis-Plus中,@TableField(exist = false) 注解用于标记一个字段在数据库表中不存在。当使用MyBatis-Plus进行查询时,如果一个字段被标记为@TableField(exist = false),那么查询语句将不会包含这个字段。这个注解通常用于以下情况:
    1. 数据库表中的列不存在:有时候,数据库表中的列可能被删除或更改,但是你的代码仍然引用这个列。通过使用@TableField(exist = false)注解,你可以告诉MyBatis-Plus忽略这个不存在的列,避免出现错误.
    2. 查询时不需要的字段:有时候,在执行查询时,你可能只需要获取表中的一部分字段,而不需要获取其他字段。通过将不需要的字段标记为@TableField(exist = false),可以优化查询性能,减少不必要的数据库操作。需要注意的是,@TableField(exist = false)注解只适用于MyBatis-Plus的查询操作,不会影响实体的映射关系。如果你在实体类中定义了一个属性,即使它被标记为@TableField(exist = false),MyBatis-Plus仍然会尝试将其映射到数据库表中的相应列.
    • 在经过查询后的列表是定义方法:
    /**
     * 查询物料列表
     *
     * @param query 查询条件
     * @param page  分页条件
     * @return 物料
     */
    public List<Item> selectList(ItemQuery query, Pageable page) {
        if (page != null) {
            PageHelper.startPage(page.getPageNumber() + 1, page.getPageSize());
        }
        QueryWrapper<Item> qw = new QueryWrapper<>();
        qw.orderByDesc("id");
        if (!StrUtil.isEmpty(query.getSearch())) {
            String search = query.getSearch();
            qw.eq("create_by", SecurityUtils.getUserId());
            qw.and((qw1) -> {
                qw1 = qw1.like("item_no", search)
                        .or()
                        .like("item_name", search);
                if (search.matches("^\\d+$")) {
                    qw1.eq("id", Long.valueOf(search));
                }
            });
            return itemMapper.selectList(qw);
        }
        if (!CollUtil.isEmpty(query.getIds())) {
            qw.in("id", query.getIds());
        }
        qw.eq("del_flag", 0);
        String itemNo = query.getItemNo();
        if (!StringUtils.isEmpty(itemNo)) {
            qw.eq("item_no", itemNo);
        }
        String itemNameLike = query.getItemNameLike();
        if (!StringUtils.isEmpty(itemNameLike)) {
            qw.like("item_name", itemNameLike);
        }
        String itemType = query.getItemType();
        if (!StringUtils.isEmpty(itemType)) {
            qw.eq("item_type", itemType);
        }
        String unit = query.getUnit();
        if (!StringUtils.isEmpty(unit)) {
            qw.eq("unit", unit);
        }
        Long rackId = query.getRackId();
        if (rackId != null) {
            qw.eq("rack_id", rackId);
        }
        Long areaId = query.getAreaId();
        if (areaId != null) {
            qw.eq("area_id", areaId);
        }
        Long warehouseId = query.getWarehouseId();
        if (warehouseId != null) {
            qw.eq("warehouse_id", warehouseId);
        }
        BigDecimal quantity = query.getQuantity();
        if (quantity != null) {
            qw.eq("quantity", quantity);
        }
        LocalDateTime expiryDate = query.getExpiryDate();
        if (expiryDate != null) {
            qw.eq("expiry_date", expiryDate);
        }
        return getItemList(qw);
    }
    
    • 进入到getItemList方法中:
    private List<Item> getItemList(QueryWrapper<Item> qw) {
        List<Item> items = itemMapper.selectList(qw);
        injectTypeName(items);
        injectWarehouseName(items);
        injectAreaName(items);
        injectSupplierName(items);
        return items;
    }
    
    • 比如注入产品供应商:
    /**
     * 注入供应商名称
     * @param res
     */
    private void injectSupplierName(List<Item> res) {
        if (CollUtil.isEmpty(res)) {
            return;
        }
        Set<Long> itemSuppliers = res.stream().map(Item::getId).collect(Collectors.toSet());
        Map<Long, OperateItemSupplier> itemSupplierMap = operateItemSupplierService.selectByIdIn(itemSuppliers).stream()
                .collect(Collectors.toMap(OperateItemSupplier::getItemId, it -> it));
        res.forEach(it -> {
            if (it.getId() != null && itemSupplierMap.containsKey(it.getId())) {
                Supplier supplier = supplierService.selectById(itemSupplierMap.get(it.getId()).getItemId());
                it.setSupplierNo(supplier.getSupplierNo());
                it.setSupplierName(supplier.getSupplierName());
            }
        });
    }
    
    • 这样一来,编号和名称就注入到Item中了