|
| 1 | +package geektime.spring.springbucks.jpademo; |
| 2 | + |
| 3 | +import geektime.spring.springbucks.jpademo.model.Coffee; |
| 4 | +import geektime.spring.springbucks.jpademo.model.CoffeeOrder; |
| 5 | +import geektime.spring.springbucks.jpademo.model.OrderState; |
| 6 | +import geektime.spring.springbucks.jpademo.repository.CoffeeOrderRepository; |
| 7 | +import geektime.spring.springbucks.jpademo.repository.CoffeeRepository; |
| 8 | +import lombok.extern.slf4j.Slf4j; |
| 9 | +import org.joda.money.CurrencyUnit; |
| 10 | +import org.joda.money.Money; |
| 11 | +import org.springframework.beans.factory.annotation.Autowired; |
| 12 | +import org.springframework.boot.ApplicationArguments; |
| 13 | +import org.springframework.boot.ApplicationRunner; |
| 14 | +import org.springframework.boot.SpringApplication; |
| 15 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 16 | +import org.springframework.data.domain.Sort; |
| 17 | +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; |
| 18 | +import org.springframework.transaction.annotation.EnableTransactionManagement; |
| 19 | + |
| 20 | +import javax.transaction.Transactional; |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.List; |
| 24 | +import java.util.stream.Collectors; |
| 25 | + |
| 26 | +@SpringBootApplication |
| 27 | +@EnableJpaRepositories |
| 28 | +@EnableTransactionManagement |
| 29 | +@Slf4j |
| 30 | +public class JpaDemoApplication implements ApplicationRunner { |
| 31 | + @Autowired |
| 32 | + private CoffeeRepository coffeeRepository; |
| 33 | + @Autowired |
| 34 | + private CoffeeOrderRepository orderRepository; |
| 35 | + |
| 36 | + public static void main(String[] args) { |
| 37 | + SpringApplication.run(JpaDemoApplication.class, args); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + @Transactional |
| 42 | + public void run(ApplicationArguments args) throws Exception { |
| 43 | + initOrders(); |
| 44 | + findOrders(); |
| 45 | + } |
| 46 | + |
| 47 | + private void initOrders() { |
| 48 | + Coffee latte = Coffee.builder().name("latte") |
| 49 | + .price(Money.of(CurrencyUnit.of("CNY"), 30.0)) |
| 50 | + .build(); |
| 51 | + coffeeRepository.save(latte); |
| 52 | + log.info("Coffee: {}", latte); |
| 53 | + |
| 54 | + Coffee espresso = Coffee.builder().name("espresso") |
| 55 | + .price(Money.of(CurrencyUnit.of("CNY"), 20.0)) |
| 56 | + .build(); |
| 57 | + coffeeRepository.save(espresso); |
| 58 | + log.info("Coffee: {}", espresso); |
| 59 | + |
| 60 | + CoffeeOrder order = CoffeeOrder.builder() |
| 61 | + .customer("Li Lei") |
| 62 | + .items(Collections.singletonList(espresso)) |
| 63 | + .state(OrderState.INIT) |
| 64 | + .build(); |
| 65 | + orderRepository.save(order); |
| 66 | + log.info("Order: {}", order); |
| 67 | + |
| 68 | + order = CoffeeOrder.builder() |
| 69 | + .customer("Li Lei") |
| 70 | + .items(Arrays.asList(espresso, latte)) |
| 71 | + .state(OrderState.INIT) |
| 72 | + .build(); |
| 73 | + orderRepository.save(order); |
| 74 | + log.info("Order: {}", order); |
| 75 | + } |
| 76 | + |
| 77 | + private void findOrders() { |
| 78 | + coffeeRepository |
| 79 | + .findAll(Sort.by(Sort.Direction.DESC, "id")) |
| 80 | + .forEach(c -> log.info("Loading {}", c)); |
| 81 | + |
| 82 | + List<CoffeeOrder> list = orderRepository.findTop3ByOrderByUpdateTimeDescIdAsc(); |
| 83 | + log.info("findTop3ByOrderByUpdateTimeDescIdAsc: {}", getJoinedOrderId(list)); |
| 84 | + |
| 85 | + list = orderRepository.findByCustomerOrderById("Li Lei"); |
| 86 | + log.info("findByCustomerOrderById: {}", getJoinedOrderId(list)); |
| 87 | + |
| 88 | + // 不开启事务会因为没Session而报LazyInitializationException |
| 89 | + list.forEach(o -> { |
| 90 | + log.info("Order {}", o.getId()); |
| 91 | + o.getItems().forEach(i -> log.info(" Item {}", i)); |
| 92 | + }); |
| 93 | + |
| 94 | + list = orderRepository.findByItems_Name("latte"); |
| 95 | + log.info("findByItems_Name: {}", getJoinedOrderId(list)); |
| 96 | + } |
| 97 | + |
| 98 | + private String getJoinedOrderId(List<CoffeeOrder> list) { |
| 99 | + return list.stream().map(o -> o.getId().toString()) |
| 100 | + .collect(Collectors.joining(",")); |
| 101 | + } |
| 102 | +} |
| 103 | + |
0 commit comments