用 Java 方式寫入測試資料

package com.example.orderservice;  

import com.example.orderservice.entity.Order;  
import com.example.orderservice.repository.OrderRepository;  
import org.springframework.boot.CommandLineRunner;  
import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
import org.springframework.cloud.openfeign.EnableFeignClients;  
import org.springframework.context.annotation.Bean;  

import java.math.BigDecimal;  

@SpringBootApplication  
@EnableFeignClients  
public class OrderServiceApplication {  
    public static void main(String[] args) {  
        SpringApplication.run(OrderServiceApplication.class, args);  
    }  

    @Bean  
    public CommandLineRunner initData(OrderRepository orderRepo) {  
        return args -> {  
            Order order = new Order();  
            order.setId(1L);  
            order.setUserId(1L);  
            order.setProductName("Laptop");  
            order.setAmount(BigDecimal.valueOf(30000));  
            order.setStatus("PENDING");  
            orderRepo.save(order);  

            System.out.println(">>> 測試資料已透過 CommandLineRunner 初始化成功!");  
        };  
    }  
}

開啟 H2 Console

URL: http://localhost:[order-service-port]/h2-console

JDBC URL: jdbc:h2:mem:testdb (需對應你的 yml 設定)