From 2a6e741222ec79623cf6056aa0536dd7ba18d03d Mon Sep 17 00:00:00 2001 From: DigitalSonic Date: Thu, 21 Mar 2019 20:53:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=AD=94=E7=96=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FAQ 2019-03/jpa-demo/.gitignore | 25 ++++++++ FAQ 2019-03/jpa-demo/pom.xml | 64 +++++++++++++++++++ .../jpademo/JpaDemoApplication.java | 61 ++++++++++++++++++ .../springbucks/jpademo/model/Coffee.java | 41 ++++++++++++ .../jpademo/model/CoffeeOrder.java | 44 +++++++++++++ .../repository/CoffeeOrderRepository.java | 7 ++ .../jpademo/repository/CoffeeRepository.java | 7 ++ .../src/main/resources/application.properties | 7 ++ .../jpademo/JpaDemoApplicationTests.java | 17 +++++ FAQ 2019-03/stream-demo/.gitignore | 25 ++++++++ FAQ 2019-03/stream-demo/pom.xml | 30 +++++++++ .../main/java/geektime/spring/faq/Demo.java | 19 ++++++ 12 files changed, 347 insertions(+) create mode 100644 FAQ 2019-03/jpa-demo/.gitignore create mode 100644 FAQ 2019-03/jpa-demo/pom.xml create mode 100644 FAQ 2019-03/jpa-demo/src/main/java/geektime/spring/springbucks/jpademo/JpaDemoApplication.java create mode 100644 FAQ 2019-03/jpa-demo/src/main/java/geektime/spring/springbucks/jpademo/model/Coffee.java create mode 100644 FAQ 2019-03/jpa-demo/src/main/java/geektime/spring/springbucks/jpademo/model/CoffeeOrder.java create mode 100644 FAQ 2019-03/jpa-demo/src/main/java/geektime/spring/springbucks/jpademo/repository/CoffeeOrderRepository.java create mode 100644 FAQ 2019-03/jpa-demo/src/main/java/geektime/spring/springbucks/jpademo/repository/CoffeeRepository.java create mode 100644 FAQ 2019-03/jpa-demo/src/main/resources/application.properties create mode 100644 FAQ 2019-03/jpa-demo/src/test/java/geektime/spring/springbucks/jpademo/JpaDemoApplicationTests.java create mode 100644 FAQ 2019-03/stream-demo/.gitignore create mode 100644 FAQ 2019-03/stream-demo/pom.xml create mode 100644 FAQ 2019-03/stream-demo/src/main/java/geektime/spring/faq/Demo.java diff --git a/FAQ 2019-03/jpa-demo/.gitignore b/FAQ 2019-03/jpa-demo/.gitignore new file mode 100644 index 0000000..c456c4a --- /dev/null +++ b/FAQ 2019-03/jpa-demo/.gitignore @@ -0,0 +1,25 @@ +/target/ +!.mvn/wrapper/maven-wrapper.jar + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +/build/ diff --git a/FAQ 2019-03/jpa-demo/pom.xml b/FAQ 2019-03/jpa-demo/pom.xml new file mode 100644 index 0000000..8a0fdb8 --- /dev/null +++ b/FAQ 2019-03/jpa-demo/pom.xml @@ -0,0 +1,64 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.1.2.RELEASE + + + geektime.spring.springbucks + jpa-demo + 0.0.1-SNAPSHOT + jpa-demo + Demo project for Spring Boot + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + + org.joda + joda-money + 1.0.1 + + + org.jadira.usertype + usertype.core + 6.0.1.GA + + + + mysql + mysql-connector-java + + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/FAQ 2019-03/jpa-demo/src/main/java/geektime/spring/springbucks/jpademo/JpaDemoApplication.java b/FAQ 2019-03/jpa-demo/src/main/java/geektime/spring/springbucks/jpademo/JpaDemoApplication.java new file mode 100644 index 0000000..d9021fd --- /dev/null +++ b/FAQ 2019-03/jpa-demo/src/main/java/geektime/spring/springbucks/jpademo/JpaDemoApplication.java @@ -0,0 +1,61 @@ +package geektime.spring.springbucks.jpademo; + +import geektime.spring.springbucks.jpademo.model.Coffee; +import geektime.spring.springbucks.jpademo.model.CoffeeOrder; +import geektime.spring.springbucks.jpademo.repository.CoffeeOrderRepository; +import geektime.spring.springbucks.jpademo.repository.CoffeeRepository; +import lombok.extern.slf4j.Slf4j; +import org.joda.money.CurrencyUnit; +import org.joda.money.Money; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; + +import java.util.Arrays; + +@SpringBootApplication +@EnableJpaRepositories +@Slf4j +public class JpaDemoApplication implements ApplicationRunner { + @Autowired + private CoffeeRepository coffeeRepository; + @Autowired + private CoffeeOrderRepository orderRepository; + + public static void main(String[] args) { + SpringApplication.run(JpaDemoApplication.class, args); + } + + @Override + public void run(ApplicationArguments args) throws Exception { + initOrders(); + } + + private void initOrders() { + Coffee espresso = Coffee.builder().name("espresso") + .price(Money.of(CurrencyUnit.of("CNY"), 20.0)) + .build(); + coffeeRepository.save(espresso); + log.info("Coffee: {}", espresso); + + Coffee latte = Coffee.builder().name("latte") + .price(Money.of(CurrencyUnit.of("CNY"), 30.0)) + .build(); + coffeeRepository.save(latte); + log.info("Coffee: {}", latte); + + log.info("Total Coffee: {}", coffeeRepository.count()); + + CoffeeOrder order = CoffeeOrder.builder() + .customer("Li Lei") + .items(Arrays.asList(espresso, latte)) + .state(0) + .build(); + orderRepository.save(order); + log.info("Order: {}", order); + } +} + diff --git a/FAQ 2019-03/jpa-demo/src/main/java/geektime/spring/springbucks/jpademo/model/Coffee.java b/FAQ 2019-03/jpa-demo/src/main/java/geektime/spring/springbucks/jpademo/model/Coffee.java new file mode 100644 index 0000000..c5fcf89 --- /dev/null +++ b/FAQ 2019-03/jpa-demo/src/main/java/geektime/spring/springbucks/jpademo/model/Coffee.java @@ -0,0 +1,41 @@ +package geektime.spring.springbucks.jpademo.model; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.hibernate.annotations.CreationTimestamp; +import org.hibernate.annotations.Type; +import org.hibernate.annotations.UpdateTimestamp; +import org.joda.money.Money; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; +import java.io.Serializable; +import java.util.Date; + +@Entity +@Table(name = "T_MENU") +@Builder +@Data +@NoArgsConstructor +@AllArgsConstructor +public class Coffee implements Serializable { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + private String name; + @Column + @Type(type = "org.jadira.usertype.moneyandcurrency.joda.PersistentMoneyMinorAmount", + parameters = {@org.hibernate.annotations.Parameter(name = "currencyCode", value = "CNY")}) + private Money price; + @Column(updatable = false) + @CreationTimestamp + private Date createTime; + @UpdateTimestamp + private Date updateTime; +} diff --git a/FAQ 2019-03/jpa-demo/src/main/java/geektime/spring/springbucks/jpademo/model/CoffeeOrder.java b/FAQ 2019-03/jpa-demo/src/main/java/geektime/spring/springbucks/jpademo/model/CoffeeOrder.java new file mode 100644 index 0000000..db40986 --- /dev/null +++ b/FAQ 2019-03/jpa-demo/src/main/java/geektime/spring/springbucks/jpademo/model/CoffeeOrder.java @@ -0,0 +1,44 @@ +package geektime.spring.springbucks.jpademo.model; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.hibernate.annotations.CreationTimestamp; +import org.hibernate.annotations.UpdateTimestamp; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinTable; +import javax.persistence.ManyToMany; +import javax.persistence.Table; +import java.io.Serializable; +import java.util.Date; +import java.util.List; + +@Entity +@Table(name = "T_ORDER") +@Data +@NoArgsConstructor +@AllArgsConstructor +@Builder +public class CoffeeOrder implements Serializable { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + private String customer; + @ManyToMany + @JoinTable(name = "T_ORDER_COFFEE") + private List items; + @Column(nullable = false) + private Integer state; + @Column(updatable = false) + @CreationTimestamp + private Date createTime; + @UpdateTimestamp + private Date updateTime; +} + diff --git a/FAQ 2019-03/jpa-demo/src/main/java/geektime/spring/springbucks/jpademo/repository/CoffeeOrderRepository.java b/FAQ 2019-03/jpa-demo/src/main/java/geektime/spring/springbucks/jpademo/repository/CoffeeOrderRepository.java new file mode 100644 index 0000000..aaf1c4e --- /dev/null +++ b/FAQ 2019-03/jpa-demo/src/main/java/geektime/spring/springbucks/jpademo/repository/CoffeeOrderRepository.java @@ -0,0 +1,7 @@ +package geektime.spring.springbucks.jpademo.repository; + +import geektime.spring.springbucks.jpademo.model.CoffeeOrder; +import org.springframework.data.repository.CrudRepository; + +public interface CoffeeOrderRepository extends CrudRepository { +} diff --git a/FAQ 2019-03/jpa-demo/src/main/java/geektime/spring/springbucks/jpademo/repository/CoffeeRepository.java b/FAQ 2019-03/jpa-demo/src/main/java/geektime/spring/springbucks/jpademo/repository/CoffeeRepository.java new file mode 100644 index 0000000..1ed18ca --- /dev/null +++ b/FAQ 2019-03/jpa-demo/src/main/java/geektime/spring/springbucks/jpademo/repository/CoffeeRepository.java @@ -0,0 +1,7 @@ +package geektime.spring.springbucks.jpademo.repository; + +import geektime.spring.springbucks.jpademo.model.Coffee; +import org.springframework.data.repository.CrudRepository; + +public interface CoffeeRepository extends CrudRepository { +} diff --git a/FAQ 2019-03/jpa-demo/src/main/resources/application.properties b/FAQ 2019-03/jpa-demo/src/main/resources/application.properties new file mode 100644 index 0000000..5494f31 --- /dev/null +++ b/FAQ 2019-03/jpa-demo/src/main/resources/application.properties @@ -0,0 +1,7 @@ +spring.jpa.hibernate.ddl-auto=create-drop +spring.jpa.properties.hibernate.show_sql=true +spring.jpa.properties.hibernate.format_sql=true + +spring.datasource.url=jdbc:mysql://localhost/springbucks?useSSL=false +spring.datasource.username=springbucks +spring.datasource.password=springbucks diff --git a/FAQ 2019-03/jpa-demo/src/test/java/geektime/spring/springbucks/jpademo/JpaDemoApplicationTests.java b/FAQ 2019-03/jpa-demo/src/test/java/geektime/spring/springbucks/jpademo/JpaDemoApplicationTests.java new file mode 100644 index 0000000..384e947 --- /dev/null +++ b/FAQ 2019-03/jpa-demo/src/test/java/geektime/spring/springbucks/jpademo/JpaDemoApplicationTests.java @@ -0,0 +1,17 @@ +package geektime.spring.springbucks.jpademo; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class JpaDemoApplicationTests { + + @Test + public void contextLoads() { + } + +} + diff --git a/FAQ 2019-03/stream-demo/.gitignore b/FAQ 2019-03/stream-demo/.gitignore new file mode 100644 index 0000000..c456c4a --- /dev/null +++ b/FAQ 2019-03/stream-demo/.gitignore @@ -0,0 +1,25 @@ +/target/ +!.mvn/wrapper/maven-wrapper.jar + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +/build/ diff --git a/FAQ 2019-03/stream-demo/pom.xml b/FAQ 2019-03/stream-demo/pom.xml new file mode 100644 index 0000000..dfb0d5c --- /dev/null +++ b/FAQ 2019-03/stream-demo/pom.xml @@ -0,0 +1,30 @@ + + + 4.0.0 + + geektime.spring.faq + stream-demo + 0.0.1-SNAPSHOT + jar + + + UTF-8 + 1.8 + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + ${java.version} + ${java.version} + ${project.build.sourceEncoding} + + + + + \ No newline at end of file diff --git a/FAQ 2019-03/stream-demo/src/main/java/geektime/spring/faq/Demo.java b/FAQ 2019-03/stream-demo/src/main/java/geektime/spring/faq/Demo.java new file mode 100644 index 0000000..ed1f6d5 --- /dev/null +++ b/FAQ 2019-03/stream-demo/src/main/java/geektime/spring/faq/Demo.java @@ -0,0 +1,19 @@ +package geektime.spring.faq; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class Demo { + public static void main(String[] args) { + Arrays.asList("Foo", "Bar").stream() + .filter(s -> s.equalsIgnoreCase("foo")) + .map(s -> s.toUpperCase()) + .forEach(System.out::println); + + Arrays.stream(new String[] { "s1", "s2", "s3" }) + .map(s -> Arrays.asList(s)) + .flatMap(l -> l.stream()) + .forEach(System.out::println); + } +}