【问题标题】:Spring data Mongodb write operation from ApplicationRunner来自 ApplicationRunner 的 Spring Data Mongodb 写操作
【发布时间】:2021-12-30 10:10:56
【问题描述】:

我在写入 mongodb 实例时遇到问题。问题是我不能从这个类中写任何东西。

@Component
@AllArgsConstructor
public class DemoDataWriter implements ApplicationRunner {
    private WarehouseRepository warehouseRepository;
    private CustomerRepository customerRepository;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        Customer customer1 = new Customer("Gleb", new Coordinate(4, 3));
        Customer customer2 = new Customer("Sasha", new Coordinate(8, 9));
        Customer customer3 = new Customer("Misha", new Coordinate(15, 10));

        Map<String, Integer> merchandiseQuantity1 = new HashMap<>();
        merchandiseQuantity1.put("computer", 16);
        merchandiseQuantity1.put("bebra", 6);
        merchandiseQuantity1.put("vacine", 10);
        Map<String, Integer> merchandiseQuantity2 = new HashMap<>();
        merchandiseQuantity1.put("laptop", 100);
        merchandiseQuantity1.put("grivna", 20);
        merchandiseQuantity1.put("beer", 1);
        Map<String, Integer> merchandiseQuantity3 = new HashMap<>();
        merchandiseQuantity1.put("cup", 13);
        merchandiseQuantity1.put("chair", 90);
        merchandiseQuantity1.put("notebook", 18);
        Map<String, Integer> merchandiseQuantity4 = new HashMap<>();
        merchandiseQuantity1.put("gun", 54);
        merchandiseQuantity1.put("answer", 42);
        merchandiseQuantity1.put("computer", 4);
        Map<String, Integer> merchandiseQuantity5 = new HashMap<>();
        merchandiseQuantity1.put("gun", 16);
        merchandiseQuantity1.put("grinva", 6);
        merchandiseQuantity1.put("charger", 132);
        Map<String, Integer> merchandiseQuantity6 = new HashMap<>();
        merchandiseQuantity1.put("computer", 16);
        merchandiseQuantity1.put("bebra", 6);
        merchandiseQuantity1.put("vacine", 10);
        Warehouse warehouse1 = new Warehouse("Compluter Inc", merchandiseQuantity1, new Coordinate(43,12));
        Warehouse warehouse2 = new Warehouse("Bebra", merchandiseQuantity2, new Coordinate(21, 89));
        Warehouse warehouse3 = new Warehouse("LG", merchandiseQuantity3, new Coordinate(15, 90));
        Warehouse warehouse4 = new Warehouse("Abchihba", merchandiseQuantity4, new Coordinate(567, 890));
        Warehouse warehouse5 = new Warehouse("Node", merchandiseQuantity5, new Coordinate(389, 54));
        Warehouse warehouse6 = new Warehouse("Meta", merchandiseQuantity6, new Coordinate(321, 590));

        customerRepository.save(customer1);
        customerRepository.save(customer2);
        customerRepository.save(customer3);

        warehouseRepository.save(warehouse1);
        warehouseRepository.save(warehouse2);
        warehouseRepository.save(warehouse3);
        warehouseRepository.save(warehouse4);
        warehouseRepository.save(warehouse5);
        warehouseRepository.save(warehouse6);
    }
}

但我可以从控制器写入数据库。

@PostMapping(path = "/customer/create")
public Customer createNewCustomer(@RequestBody Customer customer) {
    System.out.println(customer.toString());
    return customerRepository.save(customer);
}

在我使用 spring 进行此逻辑的方式中可能存在问题,我只需要找到不同的方式来编译此类操作。

【问题讨论】:

  • “我不会写”是什么意思?你得到什么错误?
  • 我看到日志说它正在保存数据,但是在我无法从数据库中获取它之后
  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: spring mongodb spring-boot spring-data mongotemplate


【解决方案1】:

我的建议是检查你是否能够在调试模式下进入运行方法。

【讨论】:

    猜你喜欢
    • 2020-12-30
    • 1970-01-01
    • 1970-01-01
    • 2017-06-23
    • 1970-01-01
    • 2017-03-22
    • 1970-01-01
    • 2017-03-22
    • 2017-03-23
    相关资源
    最近更新 更多