敏捷工具
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

268 lines
7.3 KiB

2 years ago
  1. package com.whn.hellospring;
  2. import com.whn.hellospring.mapper.CoffeeMapper;
  3. import com.whn.hellospring.model.CoffeeDO;
  4. import com.whn.hellospring.model.CoffeeOrderDO;
  5. import com.whn.hellospring.model.OrderState;
  6. import com.whn.hellospring.repository.CoffeeOrderRepository;
  7. import com.whn.hellospring.repository.CoffeeRepository;
  8. import com.whn.hellospring.service.CoffeeOrderService;
  9. import com.whn.hellospring.service.CoffeeService;
  10. import com.whn.hellospring.service.JuheService;
  11. import org.joda.money.CurrencyUnit;
  12. import org.joda.money.Money;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.boot.ApplicationArguments;
  15. import org.springframework.boot.ApplicationRunner;
  16. import org.springframework.boot.SpringApplication;
  17. import org.springframework.boot.autoconfigure.SpringBootApplication;
  18. import org.springframework.boot.builder.SpringApplicationBuilder;
  19. import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
  20. import org.springframework.context.annotation.ComponentScan;
  21. import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
  22. import org.springframework.transaction.annotation.EnableTransactionManagement;
  23. import org.springframework.transaction.annotation.Transactional;
  24. import org.springframework.web.bind.annotation.RequestMapping;
  25. import org.springframework.web.bind.annotation.RestController;
  26. import lombok.extern.slf4j.Slf4j;
  27. import redis.clients.jedis.Jedis;
  28. import java.util.List;
  29. import java.util.Optional;
  30. import java.util.stream.Collectors;
  31. @ComponentScan("com")
  32. @SpringBootApplication
  33. @RestController
  34. @Slf4j
  35. @EnableJpaRepositories
  36. @EnableTransactionManagement
  37. public class HelloSpringApplication extends SpringBootServletInitializer implements ApplicationRunner {
  38. @Autowired
  39. private CoffeeRepository coffeeRepository;
  40. @Autowired
  41. private CoffeeOrderRepository orderRepository;
  42. @Autowired
  43. private CoffeeMapper coffeeMapper;
  44. @Autowired
  45. private CoffeeService coffeeService;
  46. @Autowired
  47. private CoffeeOrderService orderService;
  48. @Autowired
  49. private JuheService juheService;
  50. public static void main(String[] args) {
  51. SpringApplication.run(HelloSpringApplication.class, args);
  52. }
  53. // 继承SpringBootServletInitializer 实现configure 方便打war 外部服务器部署。
  54. @Override
  55. protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
  56. return application.sources(HelloSpringApplication.class);
  57. }
  58. @Transactional
  59. @Override
  60. public void run(ApplicationArguments args) throws Exception {
  61. // initOrders();
  62. // findOrders();
  63. // testMybatis();
  64. // breakPage();
  65. // bucks();//初步测试
  66. // oil();
  67. // testRedis();
  68. }
  69. private void testRedis() {
  70. Jedis jedis = new Jedis("192.168.0.217");
  71. jedis.auth("root");//加入这一段代码
  72. System.out.println("Server is running: "+jedis.ping());
  73. System.out.println("connection to server sucessfully");
  74. jedis.set("title","hellword");
  75. System.out.println(jedis.get("title"));
  76. }
  77. private void oil() {
  78. juheService.getOilPrice();
  79. }
  80. private void bucks() {
  81. log.info("findAllCoffee{}",coffeeRepository.findAll());
  82. Optional<CoffeeDO> latte = coffeeService.findOneCoffee("Latte");
  83. if (latte.isPresent()){
  84. // CoffeeOrderDO order = orderService.createOrder( latte.get());
  85. // log.info("update",orderService.updateState(order,OrderState.INIT));
  86. // log.info("update",orderService.updateState(order,OrderState.PAID));
  87. }
  88. }
  89. /**
  90. * 分页测试
  91. * Mybatis PageHelper
  92. */
  93. // private void breakPage() {
  94. // //第一页数据
  95. // coffeeMapper.findAllWithRowBounds(new RowBounds(1,3))
  96. // .forEach(c->log.info("page(1) Coffee{}",c));
  97. //
  98. // //第二页数据
  99. // coffeeMapper.findAllWithRowBounds(new RowBounds(2,3))
  100. // .forEach(c->log.info("page(2) Coffee{}",c));
  101. //
  102. // log.info("=============================");
  103. //
  104. // //page传递为0的时候会取出所有数据
  105. // coffeeMapper.findAllWithRowBounds(new RowBounds(1,0))
  106. // .forEach(c->log.info("Page(1) 所有数据 Coffee{}",c));
  107. //
  108. // log.info("=============================");
  109. //
  110. // //使用参数来获取
  111. // coffeeMapper.findAllWithParam(1,3)
  112. // .forEach(c->log.info("Page(1) 使用参数来获取3 Coffee{}",c));
  113. //
  114. //
  115. // List<Coffee> list = coffeeMapper.findAllWithParam(2,3);
  116. // PageInfo<Coffee> page = new PageInfo<>(list);
  117. // log.info("page{}",page);
  118. //
  119. // }
  120. // private void testMybatis() {
  121. // Coffee c = Coffee.builder().name("espresso")
  122. // .price(Money.of(CurrencyUnit.of("CNY"),20.0))
  123. // .build();
  124. //
  125. // int count = coffeeMapper.save(c);
  126. // log.info("Save{} coffee:{}",count,c);
  127. //
  128. // c = Coffee.builder().name("latte")
  129. // .price(Money.of(CurrencyUnit.of("CNY"),25.0))
  130. // .build();
  131. //
  132. // count = coffeeMapper.save(c);
  133. // log.info("Save{} coffee:{}",count,c);
  134. //
  135. // c = coffeeMapper.findById(c.getId());
  136. // log.info("find coffee:{}",c);
  137. //
  138. // }
  139. @RequestMapping("/hello")
  140. public String hello(){
  141. return "hello spring";
  142. }
  143. private void initOrders(){
  144. //创建一杯咖啡
  145. CoffeeDO espresso = CoffeeDO.builder().name("espresso")
  146. .price(Money.of(CurrencyUnit.of("CNY"),20.0))
  147. .build();
  148. coffeeRepository.save(espresso);
  149. log.info("Coffee:{}",espresso);
  150. //创建一杯咖啡
  151. CoffeeDO latte = CoffeeDO.builder().name("latte")
  152. .price(Money.of(CurrencyUnit.of("CNY"),30.0))
  153. .build();
  154. coffeeRepository.save(latte);
  155. log.info("Coffee:{}",latte);
  156. //创建一个订单,包含一杯咖啡
  157. CoffeeOrderDO order = CoffeeOrderDO.builder()
  158. .customer("Li Lei")
  159. // .items(Collections.singletonList(espresso))
  160. .state(OrderState.INIT)
  161. .build();
  162. orderRepository.save(order);
  163. log.info("Order:{}",order);
  164. //创建一个订单,包含两杯咖啡
  165. order = CoffeeOrderDO.builder()
  166. .customer("Li Lei")
  167. // .items(Arrays.asList(espresso,latte))
  168. .state(OrderState.INIT)
  169. .build();
  170. orderRepository.save(order);
  171. log.info("Order:{}",order);
  172. //创建一个订单,包含两杯咖啡
  173. order = CoffeeOrderDO.builder()
  174. .customer("Li Lei")
  175. // .items(Arrays.asList(espresso,latte))
  176. .state(OrderState.INIT)
  177. .build();
  178. orderRepository.save(order);
  179. log.info("Order:{}",order);
  180. //创建一个订单,包含两杯咖啡
  181. order = CoffeeOrderDO.builder()
  182. .customer("Li Lei")
  183. // .items(Arrays.asList(espresso,latte))
  184. .state(OrderState.INIT)
  185. .build();
  186. orderRepository.save(order);
  187. log.info("Order:{}",order);
  188. }
  189. // private void findOrders(){
  190. // //获取咖啡
  191. // coffeeRepository
  192. // .findAll()
  193. // .forEach(c->log.info("Loading{}",c));
  194. //
  195. // //获取前三,更新时间降序,ID升序
  196. // List<CoffeeOrder> list = orderRepository.findTop3ByOrderByUpdateTimeDescIdAsc();
  197. // log.info("findTop3ByOrderByUpdateTimeDescIdAsc:{}" ,getJoinedOrderId(list));
  198. //
  199. // //获取Li Lei 的订单id
  200. // list = orderRepository.findByCustomerOrderById("Li Lei");
  201. // log.info("findByCustomerOrderById:{}" ,getJoinedOrderId(list));
  202. //
  203. // //列出订单,订单中的咖啡
  204. // list.forEach(o->{
  205. // log.info("Order{}",o.getId());
  206. // o.getItems().forEach(i->log.info(" Item{}",i));
  207. // });
  208. //
  209. // //获取latte的咖啡列表
  210. // list = orderRepository.findByItems_Name("latte");
  211. // log.info("findByItems_Name:{}" ,getJoinedOrderId(list));
  212. // }
  213. /**
  214. * 将列表中的id用,拼接
  215. * @param list
  216. * @return
  217. */
  218. private String getJoinedOrderId(List<CoffeeOrderDO> list) {
  219. return list.stream().map(o->o.getId().toString())
  220. .collect(Collectors.joining(","));
  221. }
  222. }