├── .gitignore ├── README.md ├── demo-dbflute ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── README.md ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ ├── DemoDBFluteApplication.java │ │ │ ├── PizzaController.java │ │ │ ├── PizzaRepository.java │ │ │ ├── dbflute │ │ │ ├── allcommon │ │ │ │ ├── DBCurrent.java │ │ │ │ ├── DBFluteBeansJavaConfig.java │ │ │ │ ├── DBFluteConfig.java │ │ │ │ ├── DBFluteInitializer.java │ │ │ │ ├── DBMetaInstanceHandler.java │ │ │ │ ├── EntityDefinedCommonColumn.java │ │ │ │ ├── ImplementedBehaviorSelector.java │ │ │ │ ├── ImplementedCommonColumnAutoSetupper.java │ │ │ │ ├── ImplementedInvokerAssistant.java │ │ │ │ └── ImplementedSqlClauseCreator.java │ │ │ ├── bsbhv │ │ │ │ ├── BsBaseBhv.java │ │ │ │ ├── BsPizzaBhv.java │ │ │ │ ├── BsPizzaToppingsBhv.java │ │ │ │ ├── BsToppingBhv.java │ │ │ │ └── loader │ │ │ │ │ ├── LoaderOfBase.java │ │ │ │ │ ├── LoaderOfPizza.java │ │ │ │ │ ├── LoaderOfPizzaToppings.java │ │ │ │ │ └── LoaderOfTopping.java │ │ │ ├── bsentity │ │ │ │ ├── BsBase.java │ │ │ │ ├── BsPizza.java │ │ │ │ ├── BsPizzaToppings.java │ │ │ │ ├── BsTopping.java │ │ │ │ └── dbmeta │ │ │ │ │ ├── BaseDbm.java │ │ │ │ │ ├── PizzaDbm.java │ │ │ │ │ ├── PizzaToppingsDbm.java │ │ │ │ │ └── ToppingDbm.java │ │ │ ├── cbean │ │ │ │ ├── BaseCB.java │ │ │ │ ├── PizzaCB.java │ │ │ │ ├── PizzaToppingsCB.java │ │ │ │ ├── ToppingCB.java │ │ │ │ ├── bs │ │ │ │ │ ├── BsBaseCB.java │ │ │ │ │ ├── BsPizzaCB.java │ │ │ │ │ ├── BsPizzaToppingsCB.java │ │ │ │ │ └── BsToppingCB.java │ │ │ │ ├── cq │ │ │ │ │ ├── BaseCQ.java │ │ │ │ │ ├── PizzaCQ.java │ │ │ │ │ ├── PizzaToppingsCQ.java │ │ │ │ │ ├── ToppingCQ.java │ │ │ │ │ ├── bs │ │ │ │ │ │ ├── AbstractBsBaseCQ.java │ │ │ │ │ │ ├── AbstractBsPizzaCQ.java │ │ │ │ │ │ ├── AbstractBsPizzaToppingsCQ.java │ │ │ │ │ │ ├── AbstractBsToppingCQ.java │ │ │ │ │ │ ├── BsBaseCQ.java │ │ │ │ │ │ ├── BsPizzaCQ.java │ │ │ │ │ │ ├── BsPizzaToppingsCQ.java │ │ │ │ │ │ └── BsToppingCQ.java │ │ │ │ │ └── ciq │ │ │ │ │ │ ├── BaseCIQ.java │ │ │ │ │ │ ├── PizzaCIQ.java │ │ │ │ │ │ ├── PizzaToppingsCIQ.java │ │ │ │ │ │ └── ToppingCIQ.java │ │ │ │ └── nss │ │ │ │ │ ├── PizzaNss.java │ │ │ │ │ └── PizzaToppingsNss.java │ │ │ ├── exbhv │ │ │ │ ├── BaseBhv.java │ │ │ │ ├── PizzaBhv.java │ │ │ │ ├── PizzaToppingsBhv.java │ │ │ │ └── ToppingBhv.java │ │ │ └── exentity │ │ │ │ ├── Base.java │ │ │ │ ├── Pizza.java │ │ │ │ ├── PizzaToppings.java │ │ │ │ └── Topping.java │ │ │ └── model │ │ │ ├── Base.java │ │ │ ├── Pizza.java │ │ │ └── Topping.java │ └── resources │ │ ├── application.properties │ │ ├── data.sql │ │ └── schema.sql │ └── test │ ├── java │ └── com │ │ └── example │ │ └── PizzaControllerTests.java │ └── resources │ └── com │ └── example │ └── expected.json ├── demo-doma2 ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── README.md ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ ├── DemoDoma2Application.java │ │ │ ├── PizzaController.java │ │ │ ├── PizzaRepository.java │ │ │ ├── doma │ │ │ ├── Pizza.java │ │ │ ├── PizzaDao.java │ │ │ └── PizzaToppings.java │ │ │ └── model │ │ │ ├── Base.java │ │ │ ├── Pizza.java │ │ │ └── Topping.java │ └── resources │ │ ├── META-INF │ │ └── com │ │ │ └── example │ │ │ └── doma │ │ │ └── PizzaDao │ │ │ └── findOrderByIdAsc.sql │ │ ├── application.properties │ │ ├── data.sql │ │ └── schema.sql │ └── test │ ├── java │ └── com │ │ └── example │ │ └── PizzaControllerTests.java │ └── resources │ └── com │ └── example │ └── expected.json ├── demo-ebean ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── README.md ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ ├── DemoEbeanApplication.java │ │ │ ├── EbeanFactroyBean.java │ │ │ ├── PizzaController.java │ │ │ ├── PizzaRepository.java │ │ │ └── model │ │ │ ├── Base.java │ │ │ ├── Pizza.java │ │ │ └── Topping.java │ └── resources │ │ ├── application.properties │ │ ├── data.sql │ │ └── schema.sql │ └── test │ ├── java │ └── com │ │ └── example │ │ └── PizzaControllerTests.java │ └── resources │ └── com │ └── example │ └── expected.json ├── demo-iciql ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── README.md ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ ├── DemoIciqlApplication.java │ │ │ ├── PizzaController.java │ │ │ ├── PizzaRepository.java │ │ │ ├── iciql │ │ │ ├── Base.java │ │ │ ├── Pizza.java │ │ │ ├── PizzaToppings.java │ │ │ └── Topping.java │ │ │ └── model │ │ │ ├── Base.java │ │ │ ├── Pizza.java │ │ │ └── Topping.java │ └── resources │ │ ├── application.properties │ │ ├── data.sql │ │ └── schema.sql │ └── test │ ├── java │ └── com │ │ └── example │ │ └── PizzaControllerTests.java │ └── resources │ └── com │ └── example │ └── expected.json ├── demo-jdbi ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── README.md ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ ├── DemoJdbiApplication.java │ │ │ ├── PizzaController.java │ │ │ ├── PizzaRepository.java │ │ │ ├── jdbi │ │ │ ├── Pizza.java │ │ │ ├── PizzaDao.java │ │ │ └── PizzaMapper.java │ │ │ └── model │ │ │ ├── Base.java │ │ │ ├── Pizza.java │ │ │ └── Topping.java │ └── resources │ │ ├── application.properties │ │ ├── data.sql │ │ └── schema.sql │ └── test │ ├── java │ └── com │ │ └── example │ │ └── PizzaControllerTests.java │ └── resources │ └── com │ └── example │ └── expected.json ├── demo-jooq ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── README.md ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── generated │ └── java │ │ └── com │ │ └── example │ │ └── jooq │ │ ├── DefaultCatalog.java │ │ ├── Keys.java │ │ ├── Public.java │ │ ├── Sequences.java │ │ ├── Tables.java │ │ └── tables │ │ ├── Base.java │ │ ├── Pizza.java │ │ ├── PizzaToppings.java │ │ ├── Topping.java │ │ └── records │ │ ├── BaseRecord.java │ │ ├── PizzaRecord.java │ │ ├── PizzaToppingsRecord.java │ │ └── ToppingRecord.java │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ ├── DemoJooqApplication.java │ │ │ ├── PizzaController.java │ │ │ ├── PizzaRepository.java │ │ │ └── model │ │ │ ├── Base.java │ │ │ ├── Pizza.java │ │ │ └── Topping.java │ └── resources │ │ ├── application.properties │ │ ├── data.sql │ │ └── schema.sql │ └── test │ ├── java │ └── com │ │ └── example │ │ └── PizzaControllerTests.java │ └── resources │ └── com │ └── example │ └── expected.json ├── demo-mybatis ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── README.md ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ ├── DemoMybatisApplication.java │ │ │ ├── PizzaController.java │ │ │ ├── PizzaRepository.java │ │ │ ├── model │ │ │ ├── Base.java │ │ │ ├── Pizza.java │ │ │ └── Topping.java │ │ │ └── mybatis │ │ │ ├── Pizza.java │ │ │ ├── PizzaMapper.java │ │ │ └── PizzaToppings.java │ └── resources │ │ ├── application.properties │ │ ├── data.sql │ │ └── schema.sql │ └── test │ ├── java │ └── com │ │ └── example │ │ └── PizzaControllerTests.java │ └── resources │ └── com │ └── example │ └── expected.json ├── demo-querydsl ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── README.md ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── generated │ └── java │ │ └── com │ │ └── example │ │ └── querydsl │ │ ├── Base.java │ │ ├── Pizza.java │ │ ├── PizzaToppings.java │ │ ├── QBase.java │ │ ├── QPizza.java │ │ ├── QPizzaToppings.java │ │ ├── QTopping.java │ │ └── Topping.java │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ ├── DemoQueryDslApplication.java │ │ │ ├── PizzaController.java │ │ │ ├── PizzaRepository.java │ │ │ └── model │ │ │ ├── Base.java │ │ │ ├── Pizza.java │ │ │ └── Topping.java │ └── resources │ │ ├── application.properties │ │ ├── data.sql │ │ └── schema.sql │ └── test │ ├── java │ └── com │ │ └── example │ │ └── PizzaControllerTests.java │ └── resources │ └── com │ └── example │ └── expected.json ├── demo-s2jdbc ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── README.md ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ ├── DemoS2jdbcApplication.java │ │ │ ├── PizzaController.java │ │ │ ├── PizzaRepository.java │ │ │ └── model │ │ │ ├── Base.java │ │ │ ├── Pizza.java │ │ │ ├── PizzaToppings.java │ │ │ └── Topping.java │ └── resources │ │ ├── application.properties │ │ ├── data.sql │ │ └── schema.sql │ └── test │ ├── java │ └── com │ │ └── example │ │ └── PizzaControllerTests.java │ └── resources │ └── com │ └── example │ └── expected.json ├── demo-spring-data-jpa ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── README.md ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ ├── DemoSpringDataJpaApplication.java │ │ │ ├── PizzaController.java │ │ │ ├── PizzaRepository.java │ │ │ └── model │ │ │ ├── Base.java │ │ │ ├── Pizza.java │ │ │ └── Topping.java │ └── resources │ │ ├── application.properties │ │ ├── data.sql │ │ └── schema.sql │ └── test │ ├── java │ └── com │ │ └── example │ │ └── PizzaControllerTests.java │ └── resources │ └── com │ └── example │ └── expected.json ├── demo-spring-jdbc ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── README.md ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ ├── DemoSpringJdbcApplication.java │ │ │ ├── PizzaController.java │ │ │ ├── PizzaRepository.java │ │ │ └── model │ │ │ ├── Base.java │ │ │ ├── Pizza.java │ │ │ └── Topping.java │ └── resources │ │ ├── application.properties │ │ ├── data.sql │ │ └── schema.sql │ └── test │ ├── java │ └── com │ │ └── example │ │ └── PizzaControllerTests.java │ └── resources │ └── com │ └── example │ └── expected.json └── demo-sql2o ├── .mvn └── wrapper │ ├── maven-wrapper.jar │ └── maven-wrapper.properties ├── README.md ├── mvnw ├── mvnw.cmd ├── pom.xml └── src ├── main ├── java │ └── com │ │ └── example │ │ ├── DemoSql2oApplication.java │ │ ├── PizzaController.java │ │ ├── PizzaRepository.java │ │ └── model │ │ ├── Base.java │ │ ├── Pizza.java │ │ └── Topping.java └── resources │ ├── application.properties │ ├── data.sql │ └── schema.sql └── test ├── java └── com │ └── example │ └── PizzaControllerTests.java └── resources └── com └── example └── expected.json /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .idea 3 | target 4 | transaction-logs -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Spring Boot Data Accesss Examples 2 | 3 | ### Implemented technologies 4 | 5 | * [Spring JDBC](demo-spring-jdbc) 6 | * [Spring Data JPA](demo-spring-data-jpa) 7 | * [jOOQ](demo-jooq) 8 | * [MyBatis](demo-mybatis) 9 | * [Doma2](demo-doma2) 10 | * [Querydsl](demo-querydsl) 11 | * [JDBI](demo-jdbi) 12 | * [Sql2o](demo-sql2o) 13 | * [Iciql](demo-iciql) 14 | * Exposed 15 | * [S2JDBC](demo-s2jdbc) 16 | * [DBFlute](demo-dbflute) 17 | * [Ebean](demo-ebean) 18 | 19 | Pull Requests Welcome :) 20 | 21 | ### Points to implement 22 | 23 | * many to one 24 | * many to many 25 | * batch insert 26 | * generate key 27 | -------------------------------------------------------------------------------- /demo-dbflute/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /bin 3 | /.settings 4 | /.classpath 5 | /.project 6 | /.idea 7 | /*.iml 8 | *.mv.db 9 | *.trace.db 10 | 11 | # Normally DBFlute Client 'dbflute_demodb' and DBFlute Engine 'mydbflute' 12 | # is not ignored to share the settings and engine with other developers. 13 | # But this is a code example so ignore them. 14 | /dbflute_pizzadb 15 | /mydbflute 16 | /dbflute-intro.jar -------------------------------------------------------------------------------- /demo-dbflute/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/making/spring-boot-db-samples/c2d01559bbd8843f4e239b47734c08fe481afdee/demo-dbflute/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /demo-dbflute/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.3.9/apache-maven-3.3.9-bin.zip 2 | -------------------------------------------------------------------------------- /demo-dbflute/README.md: -------------------------------------------------------------------------------- 1 | Get all pizza 2 | 3 | ``` console 4 | curl localhost:8080 5 | ``` 6 | 7 | Add a pizza 8 | 9 | ``` console 10 | curl -d '{"name" : "Spring Pizza", "price" : 1200, "toppings" : [{"id" : 3}, {"id" : 10}], "base" : {"id" : "2"}}' -H 'Content-Type: application/json' localhost:8080 11 | ``` 12 | 13 | How to generate classes by DBFlute 14 | 15 | 1. download DBFlute Engine 16 | ``` 17 | mvn dbflute:download 18 | ``` 19 | 2. create DBFlute Client 20 | ``` 21 | mvn dbflute:create-client 22 | ``` 23 | (DBFlute Client and DBFlute Engine are git-ignored in this project for repository size so needed here) 24 | 3. copy SQLs in data.sql and schems.sql to replace-schema.sql for ReplaceSchema 25 | ``` 26 | cat src/main/resources/schema.sql >> dbflute_pizzadb/playsql/replace-schema.sql 27 | cat src/main/resources/data.sql >> dbflute_pizzadb/playsql/replace-schema.sql 28 | ``` 29 | 4. sh manage.sh (at dbflute_pizzadb) and execute ReplaceSchema to create a DB 30 | ``` 31 | demo-dbflute 32 | |-dbflute_pizzadb 33 | |-mydbflute 34 | | |-dbflute-1.1.1 35 | | |-h2database 36 | | |-pizzadb.mv.db // *created here 37 | ``` 38 | 5. sh manage.sh (at dbflute_pizzadb) and execute JDBC, Doc, Generate to generate classes and documents 39 | ``` 40 | demo-dbflute 41 | |-src/main/java 42 | | |-com.example 43 | | | |-dbflute // *generated here 44 | | | | |-allcommon 45 | | | | |-bsbhv 46 | | | | |-... 47 | | | |-DemoDBFluteApplication 48 | | | |-PizzaController 49 | | | |-PizzaRepository 50 | | |-... 51 | | 52 | |-dbflute_pizzadb 53 | | |-dfprop 54 | | |-output 55 | | | |-doc 56 | | | | |-schema-pizzadb.html // *generated here 57 | | 58 | |-... 59 | ``` 60 | 61 | How to show SQL logs as DEBUG 62 | 63 | add a logging level setting for 'org.dbflute' to the application.properties 64 | ``` 65 | logging.level.org.dbflute=DEBUG 66 | ``` 67 | -------------------------------------------------------------------------------- /demo-dbflute/src/main/java/com/example/DemoDBFluteApplication.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import java.math.BigDecimal; 4 | import java.util.Arrays; 5 | 6 | import org.springframework.boot.CommandLineRunner; 7 | import org.springframework.boot.SpringApplication; 8 | import org.springframework.boot.autoconfigure.SpringBootApplication; 9 | import org.springframework.context.annotation.Bean; 10 | 11 | import com.example.model.Base; 12 | import com.example.model.Pizza; 13 | import com.example.model.Topping; 14 | 15 | @SpringBootApplication 16 | public class DemoDBFluteApplication { 17 | 18 | public static void main(String[] args) { 19 | SpringApplication.run(DemoDBFluteApplication.class, args); 20 | } 21 | 22 | @Bean 23 | CommandLineRunner runner(PizzaRepository pizzaRepository) { 24 | return a -> { 25 | Pizza pizza = new Pizza(); 26 | pizza.setBase(new Base(1L)); 27 | pizza.setToppings(Arrays.asList(new Topping(1L), new Topping(2L), new Topping(3L), new Topping(4L), new Topping(5L))); 28 | pizza.setName("Hello Pizza"); 29 | pizza.setPrice(new BigDecimal("1000")); 30 | pizzaRepository.save(pizza); 31 | }; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /demo-dbflute/src/main/java/com/example/PizzaController.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.PostMapping; 7 | import org.springframework.web.bind.annotation.RequestBody; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | import com.example.model.Pizza; 11 | 12 | @RestController 13 | public class PizzaController { 14 | private final PizzaRepository pizzaRepository; 15 | 16 | public PizzaController(PizzaRepository pizzaRepository) { 17 | this.pizzaRepository = pizzaRepository; 18 | } 19 | 20 | @GetMapping 21 | List list() { 22 | return pizzaRepository.findOrderByIdAsc(); 23 | } 24 | 25 | @PostMapping 26 | Pizza add(@RequestBody Pizza pizza) { 27 | return pizzaRepository.save(pizza); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /demo-dbflute/src/main/java/com/example/dbflute/allcommon/EntityDefinedCommonColumn.java: -------------------------------------------------------------------------------- 1 | package com.example.dbflute.allcommon; 2 | 3 | import org.dbflute.Entity; 4 | 5 | /** 6 | * The interface of entity defined common columns. 7 | * @author DBFlute(AutoGenerator) 8 | */ 9 | public interface EntityDefinedCommonColumn extends Entity { 10 | } 11 | -------------------------------------------------------------------------------- /demo-dbflute/src/main/java/com/example/dbflute/allcommon/ImplementedCommonColumnAutoSetupper.java: -------------------------------------------------------------------------------- 1 | package com.example.dbflute.allcommon; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | 6 | import org.dbflute.Entity; 7 | import org.dbflute.hook.CommonColumnAutoSetupper; 8 | 9 | /** 10 | * The basic implementation of the auto set-upper of common column. 11 | * @author DBFlute(AutoGenerator) 12 | */ 13 | public class ImplementedCommonColumnAutoSetupper implements CommonColumnAutoSetupper { 14 | 15 | // ===================================================================================== 16 | // Definition 17 | // ========== 18 | /** The logger instance for this class. (NotNull) */ 19 | private static final Logger _log = LoggerFactory.getLogger(ImplementedCommonColumnAutoSetupper.class); 20 | 21 | // ===================================================================================== 22 | // Set up 23 | // ====== 24 | /** {@inheritDoc} */ 25 | public void handleCommonColumnOfInsertIfNeeds(Entity targetEntity) { 26 | } 27 | 28 | /** {@inheritDoc} */ 29 | public void handleCommonColumnOfUpdateIfNeeds(Entity targetEntity) { 30 | } 31 | 32 | // ===================================================================================== 33 | // Logging 34 | // ======= 35 | protected boolean isInternalDebugEnabled() { 36 | return DBFluteConfig.getInstance().isInternalDebug() && _log.isDebugEnabled(); 37 | } 38 | 39 | protected void logSettingUp(EntityDefinedCommonColumn entity, String keyword) { 40 | _log.debug("...Setting up column columns of " + entity.asTableDbName() + " before " + keyword); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /demo-dbflute/src/main/java/com/example/dbflute/cbean/BaseCB.java: -------------------------------------------------------------------------------- 1 | package com.example.dbflute.cbean; 2 | 3 | import com.example.dbflute.cbean.bs.BsBaseCB; 4 | 5 | /** 6 | * The condition-bean of BASE. 7 | *

8 | * You can implement your original methods here. 9 | * This class remains when re-generating. 10 | *

11 | * @author DBFlute(AutoGenerator) 12 | */ 13 | public class BaseCB extends BsBaseCB { 14 | } 15 | -------------------------------------------------------------------------------- /demo-dbflute/src/main/java/com/example/dbflute/cbean/PizzaCB.java: -------------------------------------------------------------------------------- 1 | package com.example.dbflute.cbean; 2 | 3 | import com.example.dbflute.cbean.bs.BsPizzaCB; 4 | 5 | /** 6 | * The condition-bean of PIZZA. 7 | *

8 | * You can implement your original methods here. 9 | * This class remains when re-generating. 10 | *

11 | * @author DBFlute(AutoGenerator) 12 | */ 13 | public class PizzaCB extends BsPizzaCB { 14 | } 15 | -------------------------------------------------------------------------------- /demo-dbflute/src/main/java/com/example/dbflute/cbean/PizzaToppingsCB.java: -------------------------------------------------------------------------------- 1 | package com.example.dbflute.cbean; 2 | 3 | import com.example.dbflute.cbean.bs.BsPizzaToppingsCB; 4 | 5 | /** 6 | * The condition-bean of PIZZA_TOPPINGS. 7 | *

8 | * You can implement your original methods here. 9 | * This class remains when re-generating. 10 | *

11 | * @author DBFlute(AutoGenerator) 12 | */ 13 | public class PizzaToppingsCB extends BsPizzaToppingsCB { 14 | } 15 | -------------------------------------------------------------------------------- /demo-dbflute/src/main/java/com/example/dbflute/cbean/ToppingCB.java: -------------------------------------------------------------------------------- 1 | package com.example.dbflute.cbean; 2 | 3 | import com.example.dbflute.cbean.bs.BsToppingCB; 4 | 5 | /** 6 | * The condition-bean of TOPPING. 7 | *

8 | * You can implement your original methods here. 9 | * This class remains when re-generating. 10 | *

11 | * @author DBFlute(AutoGenerator) 12 | */ 13 | public class ToppingCB extends BsToppingCB { 14 | } 15 | -------------------------------------------------------------------------------- /demo-dbflute/src/main/java/com/example/dbflute/cbean/cq/BaseCQ.java: -------------------------------------------------------------------------------- 1 | package com.example.dbflute.cbean.cq; 2 | 3 | import org.dbflute.cbean.ConditionQuery; 4 | import org.dbflute.cbean.sqlclause.SqlClause; 5 | import com.example.dbflute.cbean.cq.bs.BsBaseCQ; 6 | 7 | /** 8 | * The condition-query of BASE. 9 | *

10 | * You can implement your original methods here. 11 | * This class remains when re-generating. 12 | *

13 | * @author DBFlute(AutoGenerator) 14 | */ 15 | public class BaseCQ extends BsBaseCQ { 16 | 17 | // =================================================================================== 18 | // Constructor 19 | // =========== 20 | // You should NOT touch with this constructor. 21 | /** 22 | * Auto-generated constructor to create query instance, basically called in DBFlute. 23 | * @param referrerQuery The instance of referrer query. (NullAllowed: if null, this is base query) 24 | * @param sqlClause The instance of SQL clause. (NotNull) 25 | * @param aliasName The alias name for this query. (NotNull) 26 | * @param nestLevel The nest level of this query. (if zero, this is base query) 27 | */ 28 | public BaseCQ(ConditionQuery referrerQuery, SqlClause sqlClause, String aliasName, int nestLevel) { 29 | super(referrerQuery, sqlClause, aliasName, nestLevel); 30 | } 31 | 32 | // =================================================================================== 33 | // Arrange Query 34 | // ============= 35 | // You can make your arranged query methods here. e.g. public void arrangeXxx() 36 | } 37 | -------------------------------------------------------------------------------- /demo-dbflute/src/main/java/com/example/dbflute/cbean/cq/PizzaCQ.java: -------------------------------------------------------------------------------- 1 | package com.example.dbflute.cbean.cq; 2 | 3 | import org.dbflute.cbean.ConditionQuery; 4 | import org.dbflute.cbean.sqlclause.SqlClause; 5 | import com.example.dbflute.cbean.cq.bs.BsPizzaCQ; 6 | 7 | /** 8 | * The condition-query of PIZZA. 9 | *

10 | * You can implement your original methods here. 11 | * This class remains when re-generating. 12 | *

13 | * @author DBFlute(AutoGenerator) 14 | */ 15 | public class PizzaCQ extends BsPizzaCQ { 16 | 17 | // =================================================================================== 18 | // Constructor 19 | // =========== 20 | // You should NOT touch with this constructor. 21 | /** 22 | * Auto-generated constructor to create query instance, basically called in DBFlute. 23 | * @param referrerQuery The instance of referrer query. (NullAllowed: if null, this is base query) 24 | * @param sqlClause The instance of SQL clause. (NotNull) 25 | * @param aliasName The alias name for this query. (NotNull) 26 | * @param nestLevel The nest level of this query. (if zero, this is base query) 27 | */ 28 | public PizzaCQ(ConditionQuery referrerQuery, SqlClause sqlClause, String aliasName, int nestLevel) { 29 | super(referrerQuery, sqlClause, aliasName, nestLevel); 30 | } 31 | 32 | // =================================================================================== 33 | // Arrange Query 34 | // ============= 35 | // You can make your arranged query methods here. e.g. public void arrangeXxx() 36 | } 37 | -------------------------------------------------------------------------------- /demo-dbflute/src/main/java/com/example/dbflute/cbean/cq/PizzaToppingsCQ.java: -------------------------------------------------------------------------------- 1 | package com.example.dbflute.cbean.cq; 2 | 3 | import org.dbflute.cbean.ConditionQuery; 4 | import org.dbflute.cbean.sqlclause.SqlClause; 5 | import com.example.dbflute.cbean.cq.bs.BsPizzaToppingsCQ; 6 | 7 | /** 8 | * The condition-query of PIZZA_TOPPINGS. 9 | *

10 | * You can implement your original methods here. 11 | * This class remains when re-generating. 12 | *

13 | * @author DBFlute(AutoGenerator) 14 | */ 15 | public class PizzaToppingsCQ extends BsPizzaToppingsCQ { 16 | 17 | // =================================================================================== 18 | // Constructor 19 | // =========== 20 | // You should NOT touch with this constructor. 21 | /** 22 | * Auto-generated constructor to create query instance, basically called in DBFlute. 23 | * @param referrerQuery The instance of referrer query. (NullAllowed: if null, this is base query) 24 | * @param sqlClause The instance of SQL clause. (NotNull) 25 | * @param aliasName The alias name for this query. (NotNull) 26 | * @param nestLevel The nest level of this query. (if zero, this is base query) 27 | */ 28 | public PizzaToppingsCQ(ConditionQuery referrerQuery, SqlClause sqlClause, String aliasName, int nestLevel) { 29 | super(referrerQuery, sqlClause, aliasName, nestLevel); 30 | } 31 | 32 | // =================================================================================== 33 | // Arrange Query 34 | // ============= 35 | // You can make your arranged query methods here. e.g. public void arrangeXxx() 36 | } 37 | -------------------------------------------------------------------------------- /demo-dbflute/src/main/java/com/example/dbflute/cbean/cq/ToppingCQ.java: -------------------------------------------------------------------------------- 1 | package com.example.dbflute.cbean.cq; 2 | 3 | import org.dbflute.cbean.ConditionQuery; 4 | import org.dbflute.cbean.sqlclause.SqlClause; 5 | import com.example.dbflute.cbean.cq.bs.BsToppingCQ; 6 | 7 | /** 8 | * The condition-query of TOPPING. 9 | *

10 | * You can implement your original methods here. 11 | * This class remains when re-generating. 12 | *

13 | * @author DBFlute(AutoGenerator) 14 | */ 15 | public class ToppingCQ extends BsToppingCQ { 16 | 17 | // =================================================================================== 18 | // Constructor 19 | // =========== 20 | // You should NOT touch with this constructor. 21 | /** 22 | * Auto-generated constructor to create query instance, basically called in DBFlute. 23 | * @param referrerQuery The instance of referrer query. (NullAllowed: if null, this is base query) 24 | * @param sqlClause The instance of SQL clause. (NotNull) 25 | * @param aliasName The alias name for this query. (NotNull) 26 | * @param nestLevel The nest level of this query. (if zero, this is base query) 27 | */ 28 | public ToppingCQ(ConditionQuery referrerQuery, SqlClause sqlClause, String aliasName, int nestLevel) { 29 | super(referrerQuery, sqlClause, aliasName, nestLevel); 30 | } 31 | 32 | // =================================================================================== 33 | // Arrange Query 34 | // ============= 35 | // You can make your arranged query methods here. e.g. public void arrangeXxx() 36 | } 37 | -------------------------------------------------------------------------------- /demo-dbflute/src/main/java/com/example/dbflute/cbean/nss/PizzaNss.java: -------------------------------------------------------------------------------- 1 | package com.example.dbflute.cbean.nss; 2 | 3 | import com.example.dbflute.cbean.cq.PizzaCQ; 4 | 5 | /** 6 | * The nest select set-upper of PIZZA. 7 | * @author DBFlute(AutoGenerator) 8 | */ 9 | public class PizzaNss { 10 | 11 | // =================================================================================== 12 | // Attribute 13 | // ========= 14 | protected final PizzaCQ _query; 15 | public PizzaNss(PizzaCQ query) { _query = query; } 16 | public boolean hasConditionQuery() { return _query != null; } 17 | 18 | // =================================================================================== 19 | // Nested Relation 20 | // =============== 21 | /** 22 | * With nested relation columns to select clause.
23 | * BASE by my BASE_ID, named 'base'. 24 | */ 25 | public void withBase() { 26 | _query.xdoNss(() -> _query.queryBase()); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /demo-dbflute/src/main/java/com/example/dbflute/cbean/nss/PizzaToppingsNss.java: -------------------------------------------------------------------------------- 1 | package com.example.dbflute.cbean.nss; 2 | 3 | import com.example.dbflute.cbean.cq.PizzaToppingsCQ; 4 | 5 | /** 6 | * The nest select set-upper of PIZZA_TOPPINGS. 7 | * @author DBFlute(AutoGenerator) 8 | */ 9 | public class PizzaToppingsNss { 10 | 11 | // =================================================================================== 12 | // Attribute 13 | // ========= 14 | protected final PizzaToppingsCQ _query; 15 | public PizzaToppingsNss(PizzaToppingsCQ query) { _query = query; } 16 | public boolean hasConditionQuery() { return _query != null; } 17 | 18 | // =================================================================================== 19 | // Nested Relation 20 | // =============== 21 | /** 22 | * With nested relation columns to select clause.
23 | * PIZZA by my PIZZA_ID, named 'pizza'. 24 | * @return The set-upper of more nested relation. {...with[nested-relation].with[more-nested-relation]} (NotNull) 25 | */ 26 | public PizzaNss withPizza() { 27 | _query.xdoNss(() -> _query.queryPizza()); 28 | return new PizzaNss(_query.queryPizza()); 29 | } 30 | /** 31 | * With nested relation columns to select clause.
32 | * TOPPING by my TOPPINGS_ID, named 'topping'. 33 | */ 34 | public void withTopping() { 35 | _query.xdoNss(() -> _query.queryTopping()); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /demo-dbflute/src/main/java/com/example/dbflute/exbhv/BaseBhv.java: -------------------------------------------------------------------------------- 1 | package com.example.dbflute.exbhv; 2 | 3 | import com.example.dbflute.bsbhv.BsBaseBhv; 4 | 5 | /** 6 | * The behavior of BASE. 7 | *

8 | * You can implement your original methods here. 9 | * This class remains when re-generating. 10 | *

11 | * @author DBFlute(AutoGenerator) 12 | */ 13 | @org.springframework.stereotype.Component("baseBhv") 14 | public class BaseBhv extends BsBaseBhv { 15 | } 16 | -------------------------------------------------------------------------------- /demo-dbflute/src/main/java/com/example/dbflute/exbhv/PizzaBhv.java: -------------------------------------------------------------------------------- 1 | package com.example.dbflute.exbhv; 2 | 3 | import com.example.dbflute.bsbhv.BsPizzaBhv; 4 | 5 | /** 6 | * The behavior of PIZZA. 7 | *

8 | * You can implement your original methods here. 9 | * This class remains when re-generating. 10 | *

11 | * @author DBFlute(AutoGenerator) 12 | */ 13 | @org.springframework.stereotype.Component("pizzaBhv") 14 | public class PizzaBhv extends BsPizzaBhv { 15 | } 16 | -------------------------------------------------------------------------------- /demo-dbflute/src/main/java/com/example/dbflute/exbhv/PizzaToppingsBhv.java: -------------------------------------------------------------------------------- 1 | package com.example.dbflute.exbhv; 2 | 3 | import com.example.dbflute.bsbhv.BsPizzaToppingsBhv; 4 | 5 | /** 6 | * The behavior of PIZZA_TOPPINGS. 7 | *

8 | * You can implement your original methods here. 9 | * This class remains when re-generating. 10 | *

11 | * @author DBFlute(AutoGenerator) 12 | */ 13 | @org.springframework.stereotype.Component("pizzaToppingsBhv") 14 | public class PizzaToppingsBhv extends BsPizzaToppingsBhv { 15 | } 16 | -------------------------------------------------------------------------------- /demo-dbflute/src/main/java/com/example/dbflute/exbhv/ToppingBhv.java: -------------------------------------------------------------------------------- 1 | package com.example.dbflute.exbhv; 2 | 3 | import com.example.dbflute.bsbhv.BsToppingBhv; 4 | 5 | /** 6 | * The behavior of TOPPING. 7 | *

8 | * You can implement your original methods here. 9 | * This class remains when re-generating. 10 | *

11 | * @author DBFlute(AutoGenerator) 12 | */ 13 | @org.springframework.stereotype.Component("toppingBhv") 14 | public class ToppingBhv extends BsToppingBhv { 15 | } 16 | -------------------------------------------------------------------------------- /demo-dbflute/src/main/java/com/example/dbflute/exentity/Base.java: -------------------------------------------------------------------------------- 1 | package com.example.dbflute.exentity; 2 | 3 | import com.example.dbflute.bsentity.BsBase; 4 | 5 | /** 6 | * The entity of BASE. 7 | *

8 | * You can implement your original methods here. 9 | * This class remains when re-generating. 10 | *

11 | * @author DBFlute(AutoGenerator) 12 | */ 13 | public class Base extends BsBase { 14 | 15 | /** The serial version UID for object serialization. (Default) */ 16 | private static final long serialVersionUID = 1L; 17 | } 18 | -------------------------------------------------------------------------------- /demo-dbflute/src/main/java/com/example/dbflute/exentity/Pizza.java: -------------------------------------------------------------------------------- 1 | package com.example.dbflute.exentity; 2 | 3 | import com.example.dbflute.bsentity.BsPizza; 4 | 5 | /** 6 | * The entity of PIZZA. 7 | *

8 | * You can implement your original methods here. 9 | * This class remains when re-generating. 10 | *

11 | * @author DBFlute(AutoGenerator) 12 | */ 13 | public class Pizza extends BsPizza { 14 | 15 | /** The serial version UID for object serialization. (Default) */ 16 | private static final long serialVersionUID = 1L; 17 | } 18 | -------------------------------------------------------------------------------- /demo-dbflute/src/main/java/com/example/dbflute/exentity/PizzaToppings.java: -------------------------------------------------------------------------------- 1 | package com.example.dbflute.exentity; 2 | 3 | import com.example.dbflute.bsentity.BsPizzaToppings; 4 | 5 | /** 6 | * The entity of PIZZA_TOPPINGS. 7 | *

8 | * You can implement your original methods here. 9 | * This class remains when re-generating. 10 | *

11 | * @author DBFlute(AutoGenerator) 12 | */ 13 | public class PizzaToppings extends BsPizzaToppings { 14 | 15 | /** The serial version UID for object serialization. (Default) */ 16 | private static final long serialVersionUID = 1L; 17 | } 18 | -------------------------------------------------------------------------------- /demo-dbflute/src/main/java/com/example/dbflute/exentity/Topping.java: -------------------------------------------------------------------------------- 1 | package com.example.dbflute.exentity; 2 | 3 | import com.example.dbflute.bsentity.BsTopping; 4 | 5 | /** 6 | * The entity of TOPPING. 7 | *

8 | * You can implement your original methods here. 9 | * This class remains when re-generating. 10 | *

11 | * @author DBFlute(AutoGenerator) 12 | */ 13 | public class Topping extends BsTopping { 14 | 15 | /** The serial version UID for object serialization. (Default) */ 16 | private static final long serialVersionUID = 1L; 17 | } 18 | -------------------------------------------------------------------------------- /demo-dbflute/src/main/java/com/example/model/Base.java: -------------------------------------------------------------------------------- 1 | package com.example.model; 2 | 3 | import java.io.Serializable; 4 | 5 | public class Base implements Serializable { 6 | private static final long serialVersionUID = 1L; 7 | private Long id; 8 | private String name; 9 | 10 | public Base(Long id) { 11 | this.id = id; 12 | } 13 | 14 | Base() { 15 | 16 | } 17 | 18 | public Long getId() { 19 | return id; 20 | } 21 | 22 | public void setId(Long id) { 23 | this.id = id; 24 | } 25 | 26 | public String getName() { 27 | return name; 28 | } 29 | 30 | public void setName(String name) { 31 | this.name = name; 32 | } 33 | 34 | @Override 35 | public String toString() { 36 | return "Base{" + 37 | "id=" + id + 38 | ", name='" + name + '\'' + 39 | '}'; 40 | } 41 | 42 | @Override 43 | public boolean equals(Object o) { 44 | if (this == o) return true; 45 | if (!(o instanceof Base)) return false; 46 | 47 | Base base = (Base) o; 48 | 49 | if (id != null ? !id.equals(base.id) : base.id != null) return false; 50 | return name != null ? name.equals(base.name) : base.name == null; 51 | 52 | } 53 | 54 | @Override 55 | public int hashCode() { 56 | int result = id != null ? id.hashCode() : 0; 57 | result = 31 * result + (name != null ? name.hashCode() : 0); 58 | return result; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /demo-dbflute/src/main/java/com/example/model/Topping.java: -------------------------------------------------------------------------------- 1 | package com.example.model; 2 | 3 | import java.io.Serializable; 4 | 5 | public class Topping implements Serializable { 6 | private static final long serialVersionUID = 1L; 7 | private Long id; 8 | private String name; 9 | 10 | public Topping(Long id) { 11 | this.id = id; 12 | } 13 | 14 | Topping() { 15 | 16 | } 17 | 18 | public Long getId() { 19 | return id; 20 | } 21 | 22 | public void setId(Long id) { 23 | this.id = id; 24 | } 25 | 26 | public String getName() { 27 | return name; 28 | } 29 | 30 | public void setName(String name) { 31 | this.name = name; 32 | } 33 | 34 | 35 | @Override 36 | public String toString() { 37 | return "Topping{" + 38 | "id=" + id + 39 | ", name='" + name + '\'' + 40 | '}'; 41 | } 42 | 43 | @Override 44 | public boolean equals(Object o) { 45 | if (this == o) return true; 46 | if (o == null || getClass() != o.getClass()) return false; 47 | 48 | Topping topping = (Topping) o; 49 | 50 | if (id != null ? !id.equals(topping.id) : topping.id != null) return false; 51 | return name != null ? name.equals(topping.name) : topping.name == null; 52 | 53 | } 54 | 55 | @Override 56 | public int hashCode() { 57 | int result = id != null ? id.hashCode() : 0; 58 | result = 31 * result + (name != null ? name.hashCode() : 0); 59 | return result; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /demo-dbflute/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | logging.level.com.example.dbflute=DEBUG 2 | logging.level.org.dbflute=DEBUG 3 | logging.level.org.springframework.jdbc.datasource.DataSourceTransactionManager=DEBUG -------------------------------------------------------------------------------- /demo-dbflute/src/main/resources/data.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO topping (name) VALUES ('Veggies'); 2 | INSERT INTO topping (name) VALUES ('Roma Tomatoes'); 3 | INSERT INTO topping (name) VALUES ('Red Onions'); 4 | INSERT INTO topping (name) VALUES ('Spinach'); 5 | INSERT INTO topping (name) VALUES ('Broccoli'); 6 | INSERT INTO topping (name) VALUES ('Pineapple'); 7 | INSERT INTO topping (name) VALUES ('Jalapeno Peppers'); 8 | INSERT INTO topping (name) VALUES ('Kalamata Olives'); 9 | INSERT INTO topping (name) VALUES ('Fresh Mushrooms'); 10 | INSERT INTO topping (name) VALUES ('Meats'); 11 | INSERT INTO topping (name) VALUES ('Anchovies'); 12 | INSERT INTO topping (name) VALUES ('Bacon Crumble'); 13 | INSERT INTO topping (name) VALUES ('Bacon Strips'); 14 | INSERT INTO topping (name) VALUES ('Chicken'); 15 | INSERT INTO topping (name) VALUES ('Ground Beef'); 16 | INSERT INTO topping (name) VALUES ('Italian Ham'); 17 | INSERT INTO topping (name) VALUES ('Spicy Italian Sausage'); 18 | INSERT INTO topping (name) VALUES ('Steak Strips'); 19 | INSERT INTO topping (name) VALUES ('Cheese'); 20 | INSERT INTO topping (name) VALUES ('Feta Cheese'); 21 | INSERT INTO topping (name) VALUES ('Parmesan Cheese'); 22 | INSERT INTO topping (name) VALUES ('Provolone Cheese'); 23 | INSERT INTO topping (name) VALUES ('Extra Cheese'); 24 | INSERT INTO topping (name) VALUES ('Four Cheese Blend Topping'); 25 | INSERT INTO topping (name) VALUES ('Goat Cheese'); 26 | INSERT INTO topping (name) VALUES ('Mozzarella'); 27 | 28 | INSERT INTO base(name) VALUES ('Thin Crust'); 29 | INSERT INTO base(name) VALUES ('Flatbread Crust'); 30 | INSERT INTO base(name) VALUES ('Thick Crust'); 31 | INSERT INTO base(name) VALUES ('Focaccia'); -------------------------------------------------------------------------------- /demo-dbflute/src/main/resources/schema.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE base IF EXISTS; 2 | DROP TABLE pizza IF EXISTS; 3 | DROP TABLE pizza_toppings IF EXISTS; 4 | DROP TABLE topping IF EXISTS; 5 | 6 | CREATE TABLE base ( 7 | id BIGINT generated BY DEFAULT AS IDENTITY, 8 | name VARCHAR(255), 9 | PRIMARY KEY (id) 10 | ); 11 | CREATE TABLE pizza ( 12 | id BIGINT generated BY DEFAULT AS IDENTITY, 13 | name VARCHAR(255), 14 | price DECIMAL(19, 2), 15 | base_id BIGINT NOT NULL, 16 | PRIMARY KEY (id) 17 | ); 18 | CREATE TABLE pizza_toppings ( 19 | pizza_id BIGINT NOT NULL, 20 | toppings_id BIGINT NOT NULL, 21 | PRIMARY KEY (pizza_id, toppings_id) 22 | ); 23 | CREATE TABLE topping ( 24 | id BIGINT generated BY DEFAULT AS IDENTITY, 25 | name VARCHAR(255), 26 | PRIMARY KEY (id) 27 | ); 28 | 29 | ALTER TABLE pizza 30 | ADD CONSTRAINT FKm6kai6c5evr38duyfh06ue29u FOREIGN KEY (base_id) REFERENCES base; 31 | ALTER TABLE pizza_toppings 32 | ADD CONSTRAINT FK7tcti2cxau3csvbuwxn0scb93 FOREIGN KEY (toppings_id) REFERENCES topping; 33 | ALTER TABLE pizza_toppings 34 | ADD CONSTRAINT FK78l5qtferc9qhfkkgyv99lmst FOREIGN KEY (pizza_id) REFERENCES pizza; -------------------------------------------------------------------------------- /demo-dbflute/src/test/java/com/example/PizzaControllerTests.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | 5 | import java.math.BigDecimal; 6 | import java.util.Arrays; 7 | import java.util.List; 8 | 9 | import org.junit.Before; 10 | import org.junit.Test; 11 | import org.junit.runner.RunWith; 12 | import org.springframework.beans.factory.annotation.Autowired; 13 | import org.springframework.boot.test.context.SpringBootTest; 14 | import org.springframework.boot.test.json.JacksonTester; 15 | import org.springframework.boot.test.web.client.TestRestTemplate; 16 | import org.springframework.test.context.junit4.SpringRunner; 17 | 18 | import com.example.model.Base; 19 | import com.example.model.Pizza; 20 | import com.example.model.Topping; 21 | import com.fasterxml.jackson.databind.ObjectMapper; 22 | 23 | @RunWith(SpringRunner.class) 24 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 25 | public class PizzaControllerTests { 26 | 27 | @Autowired 28 | TestRestTemplate restTemplate; 29 | @Autowired 30 | ObjectMapper objectMapper; 31 | 32 | private JacksonTester> json; 33 | 34 | @Before 35 | public void setup() { 36 | JacksonTester.initFields(this, objectMapper); 37 | } 38 | 39 | @Test 40 | public void contextLoads() throws Exception { 41 | Pizza pizza = new Pizza(); 42 | pizza.setBase(new Base(2L)); 43 | pizza.setToppings(Arrays.asList(new Topping(3L), new Topping(10L))); 44 | pizza.setName("Spring Pizza"); 45 | pizza.setPrice(new BigDecimal("1200")); 46 | restTemplate.postForObject("/", pizza, Void.class); 47 | List ret = Arrays.asList(restTemplate.getForObject("/", Pizza[].class)); 48 | assertThat(json.write(ret)).isEqualToJson("expected.json"); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /demo-dbflute/src/test/resources/com/example/expected.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "id": 1, 4 | "name": "Hello Pizza", 5 | "price": 1000.00, 6 | "toppings": [ 7 | { 8 | "id": 1, 9 | "name": "Veggies" 10 | }, 11 | { 12 | "id": 2, 13 | "name": "Roma Tomatoes" 14 | }, 15 | { 16 | "id": 3, 17 | "name": "Red Onions" 18 | }, 19 | { 20 | "id": 4, 21 | "name": "Spinach" 22 | }, 23 | { 24 | "id": 5, 25 | "name": "Broccoli" 26 | } 27 | ], 28 | "base": { 29 | "id": 1, 30 | "name": "Thin Crust" 31 | } 32 | }, 33 | { 34 | "id": 2, 35 | "name": "Spring Pizza", 36 | "price": 1200.00, 37 | "toppings": [ 38 | { 39 | "id": 3, 40 | "name": "Red Onions" 41 | }, 42 | { 43 | "id": 10, 44 | "name": "Meats" 45 | } 46 | ], 47 | "base": { 48 | "id": 2, 49 | "name": "Flatbread Crust" 50 | } 51 | } 52 | ] 53 | -------------------------------------------------------------------------------- /demo-doma2/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/making/spring-boot-db-samples/c2d01559bbd8843f4e239b47734c08fe481afdee/demo-doma2/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /demo-doma2/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.3.9/apache-maven-3.3.9-bin.zip 2 | -------------------------------------------------------------------------------- /demo-doma2/README.md: -------------------------------------------------------------------------------- 1 | Get all pizza 2 | 3 | ``` console 4 | curl localhost:8080 5 | ``` 6 | 7 | Add a pizza 8 | 9 | ``` console 10 | curl -d '{"name" : "Spring Pizza", "price" : 1200, "toppings" : [{"id" : 3}, {"id" : 10}], "base" : {"id" : "2"}}' -H 'Content-Type: application/json' localhost:8080 11 | ``` -------------------------------------------------------------------------------- /demo-doma2/src/main/java/com/example/DemoDoma2Application.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import com.example.model.Base; 4 | import com.example.model.Pizza; 5 | import com.example.model.Topping; 6 | import org.springframework.boot.CommandLineRunner; 7 | import org.springframework.boot.SpringApplication; 8 | import org.springframework.boot.autoconfigure.SpringBootApplication; 9 | import org.springframework.context.annotation.Bean; 10 | 11 | import java.math.BigDecimal; 12 | import java.util.Arrays; 13 | 14 | @SpringBootApplication 15 | public class DemoDoma2Application { 16 | 17 | public static void main(String[] args) { 18 | SpringApplication.run(DemoDoma2Application.class, args); 19 | } 20 | 21 | 22 | @Bean 23 | CommandLineRunner runner(PizzaRepository pizzaRepository) { 24 | return a -> { 25 | Pizza pizza = new Pizza(); 26 | pizza.setBase(new Base(1L)); 27 | pizza.setToppings(Arrays.asList(new Topping(1L), new Topping(2L), new Topping(3L), new Topping(4L), new Topping(5L))); 28 | pizza.setName("Hello Pizza"); 29 | pizza.setPrice(new BigDecimal("1000")); 30 | pizzaRepository.save(pizza); 31 | }; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /demo-doma2/src/main/java/com/example/PizzaController.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import com.example.model.Pizza; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | import org.springframework.web.bind.annotation.PostMapping; 6 | import org.springframework.web.bind.annotation.RequestBody; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | import java.util.List; 10 | 11 | @RestController 12 | public class PizzaController { 13 | private final PizzaRepository pizzaRepository; 14 | 15 | public PizzaController(PizzaRepository pizzaRepository) { 16 | this.pizzaRepository = pizzaRepository; 17 | } 18 | 19 | @GetMapping 20 | List list() { 21 | return pizzaRepository.findOrderByIdAsc(); 22 | } 23 | 24 | @PostMapping 25 | Pizza add(@RequestBody Pizza pizza) { 26 | return pizzaRepository.save(pizza); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /demo-doma2/src/main/java/com/example/PizzaRepository.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import com.example.doma.PizzaDao; 4 | import com.example.doma.PizzaToppings; 5 | import com.example.model.Base; 6 | import com.example.model.Pizza; 7 | import com.example.model.Topping; 8 | import org.springframework.stereotype.Repository; 9 | import org.springframework.transaction.annotation.Transactional; 10 | 11 | import java.util.List; 12 | import java.util.stream.Collectors; 13 | 14 | @Repository 15 | public class PizzaRepository { 16 | 17 | private final PizzaDao pizzaDao; 18 | 19 | public PizzaRepository(PizzaDao pizzaDao) { 20 | this.pizzaDao = pizzaDao; 21 | } 22 | 23 | public List findOrderByIdAsc() { 24 | return pizzaDao.findOrderByIdAsc().stream() 25 | .collect(Collectors.groupingBy(x -> x.id)) 26 | .entrySet() 27 | .stream() 28 | .map(e -> { 29 | com.example.doma.Pizza dto = e.getValue().get(0); 30 | Pizza pizza = new Pizza(); 31 | pizza.setId(dto.id); 32 | pizza.setName(dto.name); 33 | pizza.setPrice(dto.price); 34 | Base base = new Base(dto.id); 35 | base.setName(dto.baseName); 36 | pizza.setBase(base); 37 | pizza.setToppings(e.getValue().stream() 38 | .map(x -> { 39 | Topping topping = new Topping(x.toppingId); 40 | topping.setName(x.toppingName); 41 | return topping; 42 | }) 43 | .collect(Collectors.toList())); 44 | return pizza; 45 | }) 46 | .collect(Collectors.toList()); 47 | } 48 | 49 | @Transactional 50 | public Pizza save(Pizza pizza) { 51 | com.example.doma.Pizza dto = new com.example.doma.Pizza(); 52 | dto.id = pizza.getId(); 53 | dto.name = pizza.getName(); 54 | dto.price = pizza.getPrice(); 55 | dto.baseId = pizza.getBase().getId(); 56 | pizzaDao.insert(dto); 57 | pizzaDao.insertToppings(pizza.getToppings().stream() 58 | .map(x -> { 59 | PizzaToppings pt = new PizzaToppings(); 60 | pt.pizzaId = dto.id; 61 | pt.toppingsId = x.getId(); 62 | return pt; 63 | }).collect(Collectors.toList())); 64 | return pizza; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /demo-doma2/src/main/java/com/example/doma/Pizza.java: -------------------------------------------------------------------------------- 1 | package com.example.doma; 2 | 3 | import org.seasar.doma.*; 4 | 5 | import java.math.BigDecimal; 6 | 7 | @Entity 8 | public class Pizza { 9 | @Id 10 | @GeneratedValue(strategy = GenerationType.IDENTITY) 11 | public Long id; 12 | public String name; 13 | public BigDecimal price; 14 | public Long baseId; 15 | @Column(insertable = false) 16 | public String baseName; 17 | @Column(insertable = false) 18 | public Long toppingId; 19 | @Column(insertable = false) 20 | public String toppingName; 21 | 22 | @Override 23 | public String toString() { 24 | return "Pizza{" + 25 | "id=" + id + 26 | ", name='" + name + '\'' + 27 | ", price=" + price + 28 | ", baseId=" + baseId + 29 | ", baseName='" + baseName + '\'' + 30 | ", toppingId=" + toppingId + 31 | ", toppingName='" + toppingName + '\'' + 32 | '}'; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /demo-doma2/src/main/java/com/example/doma/PizzaDao.java: -------------------------------------------------------------------------------- 1 | package com.example.doma; 2 | 3 | import com.example.doma.Pizza; 4 | import com.example.doma.PizzaToppings; 5 | import org.seasar.doma.BatchInsert; 6 | import org.seasar.doma.Dao; 7 | import org.seasar.doma.Insert; 8 | import org.seasar.doma.Select; 9 | import org.seasar.doma.boot.ConfigAutowireable; 10 | 11 | import java.util.List; 12 | 13 | @ConfigAutowireable 14 | @Dao 15 | public interface PizzaDao { 16 | @Select 17 | List findOrderByIdAsc(); 18 | 19 | @Insert 20 | int insert(Pizza pizza); 21 | 22 | @BatchInsert 23 | int[] insertToppings(List pizzaToppingses); 24 | } -------------------------------------------------------------------------------- /demo-doma2/src/main/java/com/example/doma/PizzaToppings.java: -------------------------------------------------------------------------------- 1 | package com.example.doma; 2 | 3 | import org.seasar.doma.Entity; 4 | import org.seasar.doma.Id; 5 | 6 | @Entity 7 | public class PizzaToppings { 8 | @Id 9 | public Long pizzaId; 10 | @Id 11 | public Long toppingsId; 12 | } 13 | -------------------------------------------------------------------------------- /demo-doma2/src/main/java/com/example/model/Base.java: -------------------------------------------------------------------------------- 1 | package com.example.model; 2 | 3 | import java.io.Serializable; 4 | 5 | public class Base implements Serializable { 6 | private Long id; 7 | private String name; 8 | 9 | public Base(Long id) { 10 | this.id = id; 11 | } 12 | 13 | Base() { 14 | 15 | } 16 | 17 | public Long getId() { 18 | return id; 19 | } 20 | 21 | public void setId(Long id) { 22 | this.id = id; 23 | } 24 | 25 | public String getName() { 26 | return name; 27 | } 28 | 29 | public void setName(String name) { 30 | this.name = name; 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return "Base{" + 36 | "id=" + id + 37 | ", name='" + name + '\'' + 38 | '}'; 39 | } 40 | 41 | @Override 42 | public boolean equals(Object o) { 43 | if (this == o) return true; 44 | if (!(o instanceof Base)) return false; 45 | 46 | Base base = (Base) o; 47 | 48 | if (id != null ? !id.equals(base.id) : base.id != null) return false; 49 | return name != null ? name.equals(base.name) : base.name == null; 50 | 51 | } 52 | 53 | @Override 54 | public int hashCode() { 55 | int result = id != null ? id.hashCode() : 0; 56 | result = 31 * result + (name != null ? name.hashCode() : 0); 57 | return result; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /demo-doma2/src/main/java/com/example/model/Topping.java: -------------------------------------------------------------------------------- 1 | package com.example.model; 2 | 3 | import java.io.Serializable; 4 | 5 | public class Topping implements Serializable { 6 | private Long id; 7 | private String name; 8 | 9 | public Topping(Long id) { 10 | this.id = id; 11 | } 12 | 13 | Topping() { 14 | 15 | } 16 | 17 | public Long getId() { 18 | return id; 19 | } 20 | 21 | public void setId(Long id) { 22 | this.id = id; 23 | } 24 | 25 | public String getName() { 26 | return name; 27 | } 28 | 29 | public void setName(String name) { 30 | this.name = name; 31 | } 32 | 33 | 34 | @Override 35 | public String toString() { 36 | return "Topping{" + 37 | "id=" + id + 38 | ", name='" + name + '\'' + 39 | '}'; 40 | } 41 | 42 | @Override 43 | public boolean equals(Object o) { 44 | if (this == o) return true; 45 | if (o == null || getClass() != o.getClass()) return false; 46 | 47 | Topping topping = (Topping) o; 48 | 49 | if (id != null ? !id.equals(topping.id) : topping.id != null) return false; 50 | return name != null ? name.equals(topping.name) : topping.name == null; 51 | 52 | } 53 | 54 | @Override 55 | public int hashCode() { 56 | int result = id != null ? id.hashCode() : 0; 57 | result = 31 * result + (name != null ? name.hashCode() : 0); 58 | return result; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /demo-doma2/src/main/resources/META-INF/com/example/doma/PizzaDao/findOrderByIdAsc.sql: -------------------------------------------------------------------------------- 1 | SELECT 2 | p.id AS id, 3 | p.name AS name, 4 | p.price AS price, 5 | b.id AS base_id, 6 | b.name AS base_name, 7 | t.id AS topping_id, 8 | t.name AS topping_name 9 | FROM pizza p 10 | INNER JOIN base b ON (b.id = p.base_id) 11 | INNER JOIN pizza_toppings pt ON (pt.pizza_id = p.id) 12 | INNER JOIN topping t ON (t.id = pt.toppings_id) 13 | ORDER BY p.id ASC -------------------------------------------------------------------------------- /demo-doma2/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | doma.dialect=h2 2 | doma.naming=snake_lower_case 3 | logging.level.org.springframework.jdbc.datasource.DataSourceTransactionManager=DEBUG -------------------------------------------------------------------------------- /demo-doma2/src/main/resources/data.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO topping (name) VALUES ('Veggies'); 2 | INSERT INTO topping (name) VALUES ('Roma Tomatoes'); 3 | INSERT INTO topping (name) VALUES ('Red Onions'); 4 | INSERT INTO topping (name) VALUES ('Spinach'); 5 | INSERT INTO topping (name) VALUES ('Broccoli'); 6 | INSERT INTO topping (name) VALUES ('Pineapple'); 7 | INSERT INTO topping (name) VALUES ('Jalapeno Peppers'); 8 | INSERT INTO topping (name) VALUES ('Kalamata Olives'); 9 | INSERT INTO topping (name) VALUES ('Fresh Mushrooms'); 10 | INSERT INTO topping (name) VALUES ('Meats'); 11 | INSERT INTO topping (name) VALUES ('Anchovies'); 12 | INSERT INTO topping (name) VALUES ('Bacon Crumble'); 13 | INSERT INTO topping (name) VALUES ('Bacon Strips'); 14 | INSERT INTO topping (name) VALUES ('Chicken'); 15 | INSERT INTO topping (name) VALUES ('Ground Beef'); 16 | INSERT INTO topping (name) VALUES ('Italian Ham'); 17 | INSERT INTO topping (name) VALUES ('Spicy Italian Sausage'); 18 | INSERT INTO topping (name) VALUES ('Steak Strips'); 19 | INSERT INTO topping (name) VALUES ('Cheese'); 20 | INSERT INTO topping (name) VALUES ('Feta Cheese'); 21 | INSERT INTO topping (name) VALUES ('Parmesan Cheese'); 22 | INSERT INTO topping (name) VALUES ('Provolone Cheese'); 23 | INSERT INTO topping (name) VALUES ('Extra Cheese'); 24 | INSERT INTO topping (name) VALUES ('Four Cheese Blend Topping'); 25 | INSERT INTO topping (name) VALUES ('Goat Cheese'); 26 | INSERT INTO topping (name) VALUES ('Mozzarella'); 27 | 28 | INSERT INTO base(name) VALUES ('Thin Crust'); 29 | INSERT INTO base(name) VALUES ('Flatbread Crust'); 30 | INSERT INTO base(name) VALUES ('Thick Crust'); 31 | INSERT INTO base(name) VALUES ('Focaccia'); -------------------------------------------------------------------------------- /demo-doma2/src/main/resources/schema.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE base IF EXISTS; 2 | DROP TABLE pizza IF EXISTS; 3 | DROP TABLE pizza_toppings IF EXISTS; 4 | DROP TABLE topping IF EXISTS; 5 | 6 | CREATE TABLE base ( 7 | id BIGINT generated BY DEFAULT AS IDENTITY, 8 | name VARCHAR(255), 9 | PRIMARY KEY (id) 10 | ); 11 | CREATE TABLE pizza ( 12 | id BIGINT generated BY DEFAULT AS IDENTITY, 13 | name VARCHAR(255), 14 | price DECIMAL(19, 2), 15 | base_id BIGINT, 16 | PRIMARY KEY (id) 17 | ); 18 | CREATE TABLE pizza_toppings ( 19 | pizza_id BIGINT NOT NULL, 20 | toppings_id BIGINT NOT NULL, 21 | PRIMARY KEY (pizza_id, toppings_id) 22 | ); 23 | CREATE TABLE topping ( 24 | id BIGINT generated BY DEFAULT AS IDENTITY, 25 | name VARCHAR(255), 26 | PRIMARY KEY (id) 27 | ); 28 | 29 | ALTER TABLE pizza 30 | ADD CONSTRAINT FKm6kai6c5evr38duyfh06ue29u FOREIGN KEY (base_id) REFERENCES base; 31 | ALTER TABLE pizza_toppings 32 | ADD CONSTRAINT FK7tcti2cxau3csvbuwxn0scb93 FOREIGN KEY (toppings_id) REFERENCES topping; 33 | ALTER TABLE pizza_toppings 34 | ADD CONSTRAINT FK78l5qtferc9qhfkkgyv99lmst FOREIGN KEY (pizza_id) REFERENCES pizza; -------------------------------------------------------------------------------- /demo-doma2/src/test/java/com/example/PizzaControllerTests.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import com.example.model.Base; 4 | import com.example.model.Pizza; 5 | import com.example.model.Topping; 6 | import com.fasterxml.jackson.databind.ObjectMapper; 7 | import org.junit.Before; 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.boot.test.context.SpringBootTest; 12 | import org.springframework.boot.test.json.JacksonTester; 13 | import org.springframework.boot.test.web.client.TestRestTemplate; 14 | import org.springframework.test.context.junit4.SpringRunner; 15 | 16 | import java.math.BigDecimal; 17 | import java.util.Arrays; 18 | import java.util.List; 19 | 20 | import static org.assertj.core.api.Assertions.assertThat; 21 | 22 | @RunWith(SpringRunner.class) 23 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 24 | public class PizzaControllerTests { 25 | 26 | @Autowired 27 | TestRestTemplate restTemplate; 28 | @Autowired 29 | ObjectMapper objectMapper; 30 | 31 | private JacksonTester> json; 32 | 33 | @Before 34 | public void setup() { 35 | JacksonTester.initFields(this, objectMapper); 36 | } 37 | 38 | @Test 39 | public void contextLoads() throws Exception { 40 | Pizza pizza = new Pizza(); 41 | pizza.setBase(new Base(2L)); 42 | pizza.setToppings(Arrays.asList(new Topping(3L), new Topping(10L))); 43 | pizza.setName("Spring Pizza"); 44 | pizza.setPrice(new BigDecimal("1200")); 45 | restTemplate.postForObject("/", pizza, Void.class); 46 | List ret = Arrays.asList(restTemplate.getForObject("/", Pizza[].class)); 47 | assertThat(json.write(ret)).isEqualToJson("expected.json"); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /demo-doma2/src/test/resources/com/example/expected.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "id": 1, 4 | "name": "Hello Pizza", 5 | "price": 1000.00, 6 | "toppings": [ 7 | { 8 | "id": 1, 9 | "name": "Veggies" 10 | }, 11 | { 12 | "id": 2, 13 | "name": "Roma Tomatoes" 14 | }, 15 | { 16 | "id": 3, 17 | "name": "Red Onions" 18 | }, 19 | { 20 | "id": 4, 21 | "name": "Spinach" 22 | }, 23 | { 24 | "id": 5, 25 | "name": "Broccoli" 26 | } 27 | ], 28 | "base": { 29 | "id": 1, 30 | "name": "Thin Crust" 31 | } 32 | }, 33 | { 34 | "id": 2, 35 | "name": "Spring Pizza", 36 | "price": 1200.00, 37 | "toppings": [ 38 | { 39 | "id": 3, 40 | "name": "Red Onions" 41 | }, 42 | { 43 | "id": 10, 44 | "name": "Meats" 45 | } 46 | ], 47 | "base": { 48 | "id": 2, 49 | "name": "Flatbread Crust" 50 | } 51 | } 52 | ] 53 | -------------------------------------------------------------------------------- /demo-ebean/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /bin 3 | /.settings 4 | /.classpath 5 | /.project 6 | /.idea 7 | /*.iml 8 | -------------------------------------------------------------------------------- /demo-ebean/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/making/spring-boot-db-samples/c2d01559bbd8843f4e239b47734c08fe481afdee/demo-ebean/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /demo-ebean/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.3.9/apache-maven-3.3.9-bin.zip 2 | -------------------------------------------------------------------------------- /demo-ebean/README.md: -------------------------------------------------------------------------------- 1 | Get all pizza 2 | 3 | ``` console 4 | curl localhost:8080 5 | ``` 6 | 7 | Add a pizza 8 | 9 | ``` console 10 | curl -d '{"name" : "Spring Pizza", "price" : 1200, "toppings" : [{"id" : 3}, {"id" : 10}], "base" : {"id" : "2"}}' -H 'Content-Type: application/json' localhost:8080 11 | ``` -------------------------------------------------------------------------------- /demo-ebean/src/main/java/com/example/DemoEbeanApplication.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import java.math.BigDecimal; 4 | import java.util.Arrays; 5 | 6 | import org.springframework.boot.CommandLineRunner; 7 | import org.springframework.boot.SpringApplication; 8 | import org.springframework.boot.autoconfigure.SpringBootApplication; 9 | import org.springframework.context.annotation.Bean; 10 | 11 | import com.example.model.Base; 12 | import com.example.model.Pizza; 13 | import com.example.model.Topping; 14 | 15 | @SpringBootApplication 16 | public class DemoEbeanApplication { 17 | 18 | public static void main(String[] args) { 19 | SpringApplication.run(DemoEbeanApplication.class, args); 20 | } 21 | 22 | @Bean 23 | CommandLineRunner runner(PizzaRepository pizzaRepository) { 24 | return a -> { 25 | Pizza pizza = new Pizza(); 26 | pizza.setBase(new Base(1L)); 27 | pizza.setToppings(Arrays.asList(new Topping(1L), new Topping(2L), new Topping(3L), new Topping(4L), new Topping(5L))); 28 | pizza.setName("Hello Pizza"); 29 | pizza.setPrice(new BigDecimal("1000")); 30 | pizzaRepository.save(pizza); 31 | }; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /demo-ebean/src/main/java/com/example/EbeanFactroyBean.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import javax.sql.DataSource; 4 | 5 | import org.springframework.beans.factory.FactoryBean; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Component; 8 | 9 | import com.avaje.ebean.EbeanServer; 10 | import com.avaje.ebean.EbeanServerFactory; 11 | import com.avaje.ebean.config.ServerConfig; 12 | import com.avaje.ebean.springsupport.txn.SpringAwareJdbcTransactionManager; 13 | 14 | @Component 15 | public class EbeanFactroyBean implements FactoryBean { 16 | 17 | @Autowired 18 | DataSource dataSource; 19 | 20 | @Override 21 | public EbeanServer getObject() throws Exception { 22 | 23 | ServerConfig config = new ServerConfig(); 24 | config.setName("default"); 25 | config.setDataSource(dataSource); 26 | config.setExternalTransactionManager(new SpringAwareJdbcTransactionManager()); 27 | config.setDefaultServer(true); 28 | config.setRegister(true); 29 | 30 | return EbeanServerFactory.create(config); 31 | } 32 | 33 | @Override 34 | public Class getObjectType() { 35 | return EbeanServer.class; 36 | } 37 | 38 | @Override 39 | public boolean isSingleton() { 40 | return true; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /demo-ebean/src/main/java/com/example/PizzaController.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import com.example.model.Pizza; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | import org.springframework.web.bind.annotation.PostMapping; 6 | import org.springframework.web.bind.annotation.RequestBody; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | import java.util.List; 10 | 11 | @RestController 12 | public class PizzaController { 13 | private final PizzaRepository pizzaRepository; 14 | 15 | public PizzaController(PizzaRepository pizzaRepository) { 16 | this.pizzaRepository = pizzaRepository; 17 | } 18 | 19 | @GetMapping 20 | List list() { 21 | return pizzaRepository.findOrderByIdAsc(); 22 | } 23 | 24 | @PostMapping 25 | Pizza add(@RequestBody Pizza pizza) { 26 | return pizzaRepository.save(pizza); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /demo-ebean/src/main/java/com/example/PizzaRepository.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Repository; 7 | import org.springframework.transaction.annotation.Transactional; 8 | 9 | import com.avaje.ebean.EbeanServer; 10 | import com.example.model.Pizza; 11 | 12 | @Repository 13 | public class PizzaRepository { 14 | 15 | @Autowired 16 | EbeanServer server; 17 | 18 | public List findOrderByIdAsc() { 19 | List pizzas = server.find(Pizza.class).orderBy("id asc").findList(); 20 | return pizzas; 21 | } 22 | 23 | @Transactional 24 | public Pizza save(Pizza pizza) { 25 | server.save(pizza); 26 | return pizza; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /demo-ebean/src/main/java/com/example/model/Base.java: -------------------------------------------------------------------------------- 1 | package com.example.model; 2 | 3 | import javax.persistence.*; 4 | import java.io.Serializable; 5 | 6 | @Entity 7 | public class Base implements Serializable { 8 | @Id 9 | @GeneratedValue(strategy = GenerationType.IDENTITY) 10 | private Long id; 11 | private String name; 12 | 13 | public Base(Long id) { 14 | this.id = id; 15 | } 16 | 17 | Base() { 18 | 19 | } 20 | 21 | public Long getId() { 22 | return id; 23 | } 24 | 25 | public void setId(Long id) { 26 | this.id = id; 27 | } 28 | 29 | public String getName() { 30 | return name; 31 | } 32 | 33 | public void setName(String name) { 34 | this.name = name; 35 | } 36 | 37 | @Override 38 | public String toString() { 39 | return "Base{" + 40 | "id=" + id + 41 | ", name='" + name + '\'' + 42 | '}'; 43 | } 44 | 45 | @Override 46 | public boolean equals(Object o) { 47 | if (this == o) return true; 48 | if (!(o instanceof Base)) return false; 49 | 50 | Base base = (Base) o; 51 | 52 | if (id != null ? !id.equals(base.id) : base.id != null) return false; 53 | return name != null ? name.equals(base.name) : base.name == null; 54 | 55 | } 56 | 57 | @Override 58 | public int hashCode() { 59 | int result = id != null ? id.hashCode() : 0; 60 | result = 31 * result + (name != null ? name.hashCode() : 0); 61 | return result; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /demo-ebean/src/main/java/com/example/model/Topping.java: -------------------------------------------------------------------------------- 1 | package com.example.model; 2 | 3 | import javax.persistence.*; 4 | import java.io.Serializable; 5 | 6 | @Entity 7 | public class Topping implements Serializable { 8 | @Id 9 | @GeneratedValue(strategy = GenerationType.IDENTITY) 10 | private Long id; 11 | private String name; 12 | 13 | public Topping(Long id) { 14 | this.id = id; 15 | } 16 | 17 | Topping() { 18 | 19 | } 20 | 21 | public Long getId() { 22 | return id; 23 | } 24 | 25 | public void setId(Long id) { 26 | this.id = id; 27 | } 28 | 29 | public String getName() { 30 | return name; 31 | } 32 | 33 | public void setName(String name) { 34 | this.name = name; 35 | } 36 | 37 | 38 | @Override 39 | public String toString() { 40 | return "Topping{" + 41 | "id=" + id + 42 | ", name='" + name + '\'' + 43 | '}'; 44 | } 45 | 46 | @Override 47 | public boolean equals(Object o) { 48 | if (this == o) return true; 49 | if (o == null || getClass() != o.getClass()) return false; 50 | 51 | Topping topping = (Topping) o; 52 | 53 | if (id != null ? !id.equals(topping.id) : topping.id != null) return false; 54 | return name != null ? name.equals(topping.name) : topping.name == null; 55 | 56 | } 57 | 58 | @Override 59 | public int hashCode() { 60 | int result = id != null ? id.hashCode() : 0; 61 | result = 31 * result + (name != null ? name.hashCode() : 0); 62 | return result; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /demo-ebean/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | logging.level.org.avaje.ebean.SQL=DEBUG 2 | logging.level.org.avaje.ebean.TXN=DEBUG 3 | -------------------------------------------------------------------------------- /demo-ebean/src/main/resources/data.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO topping (name) VALUES ('Veggies'); 2 | INSERT INTO topping (name) VALUES ('Roma Tomatoes'); 3 | INSERT INTO topping (name) VALUES ('Red Onions'); 4 | INSERT INTO topping (name) VALUES ('Spinach'); 5 | INSERT INTO topping (name) VALUES ('Broccoli'); 6 | INSERT INTO topping (name) VALUES ('Pineapple'); 7 | INSERT INTO topping (name) VALUES ('Jalapeno Peppers'); 8 | INSERT INTO topping (name) VALUES ('Kalamata Olives'); 9 | INSERT INTO topping (name) VALUES ('Fresh Mushrooms'); 10 | INSERT INTO topping (name) VALUES ('Meats'); 11 | INSERT INTO topping (name) VALUES ('Anchovies'); 12 | INSERT INTO topping (name) VALUES ('Bacon Crumble'); 13 | INSERT INTO topping (name) VALUES ('Bacon Strips'); 14 | INSERT INTO topping (name) VALUES ('Chicken'); 15 | INSERT INTO topping (name) VALUES ('Ground Beef'); 16 | INSERT INTO topping (name) VALUES ('Italian Ham'); 17 | INSERT INTO topping (name) VALUES ('Spicy Italian Sausage'); 18 | INSERT INTO topping (name) VALUES ('Steak Strips'); 19 | INSERT INTO topping (name) VALUES ('Cheese'); 20 | INSERT INTO topping (name) VALUES ('Feta Cheese'); 21 | INSERT INTO topping (name) VALUES ('Parmesan Cheese'); 22 | INSERT INTO topping (name) VALUES ('Provolone Cheese'); 23 | INSERT INTO topping (name) VALUES ('Extra Cheese'); 24 | INSERT INTO topping (name) VALUES ('Four Cheese Blend Topping'); 25 | INSERT INTO topping (name) VALUES ('Goat Cheese'); 26 | INSERT INTO topping (name) VALUES ('Mozzarella'); 27 | 28 | INSERT INTO base(name) VALUES ('Thin Crust'); 29 | INSERT INTO base(name) VALUES ('Flatbread Crust'); 30 | INSERT INTO base(name) VALUES ('Thick Crust'); 31 | INSERT INTO base(name) VALUES ('Focaccia'); 32 | -------------------------------------------------------------------------------- /demo-ebean/src/main/resources/schema.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE base IF EXISTS; 2 | DROP TABLE pizza IF EXISTS; 3 | DROP TABLE pizza_toppings IF EXISTS; 4 | DROP TABLE topping IF EXISTS; 5 | 6 | CREATE TABLE base ( 7 | id BIGINT generated BY DEFAULT AS IDENTITY, 8 | name VARCHAR(255), 9 | PRIMARY KEY (id) 10 | ); 11 | CREATE TABLE pizza ( 12 | id BIGINT generated BY DEFAULT AS IDENTITY, 13 | name VARCHAR(255), 14 | price DECIMAL(19, 2), 15 | base_id BIGINT, 16 | PRIMARY KEY (id) 17 | ); 18 | CREATE TABLE pizza_toppings ( 19 | pizza_id BIGINT NOT NULL, 20 | toppings_id BIGINT NOT NULL, 21 | PRIMARY KEY (pizza_id, toppings_id) 22 | ); 23 | CREATE TABLE topping ( 24 | id BIGINT generated BY DEFAULT AS IDENTITY, 25 | name VARCHAR(255), 26 | PRIMARY KEY (id) 27 | ); 28 | 29 | ALTER TABLE pizza 30 | ADD CONSTRAINT FKm6kai6c5evr38duyfh06ue29u FOREIGN KEY (base_id) REFERENCES base; 31 | ALTER TABLE pizza_toppings 32 | ADD CONSTRAINT FK7tcti2cxau3csvbuwxn0scb93 FOREIGN KEY (toppings_id) REFERENCES topping; 33 | ALTER TABLE pizza_toppings 34 | ADD CONSTRAINT FK78l5qtferc9qhfkkgyv99lmst FOREIGN KEY (pizza_id) REFERENCES pizza; -------------------------------------------------------------------------------- /demo-ebean/src/test/java/com/example/PizzaControllerTests.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import com.example.model.Base; 4 | import com.example.model.Pizza; 5 | import com.example.model.Topping; 6 | import com.fasterxml.jackson.databind.ObjectMapper; 7 | import org.junit.Before; 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.boot.test.context.SpringBootTest; 12 | import org.springframework.boot.test.json.JacksonTester; 13 | import org.springframework.boot.test.web.client.TestRestTemplate; 14 | import org.springframework.test.context.junit4.SpringRunner; 15 | 16 | import java.math.BigDecimal; 17 | import java.util.Arrays; 18 | import java.util.List; 19 | 20 | import static org.assertj.core.api.Assertions.assertThat; 21 | 22 | @RunWith(SpringRunner.class) 23 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 24 | public class PizzaControllerTests { 25 | 26 | @Autowired 27 | TestRestTemplate restTemplate; 28 | @Autowired 29 | ObjectMapper objectMapper; 30 | 31 | private JacksonTester> json; 32 | 33 | @Before 34 | public void setup() { 35 | JacksonTester.initFields(this, objectMapper); 36 | } 37 | 38 | @Test 39 | public void contextLoads() throws Exception { 40 | Pizza pizza = new Pizza(); 41 | pizza.setBase(new Base(2L)); 42 | pizza.setToppings(Arrays.asList(new Topping(3L), new Topping(10L))); 43 | pizza.setName("Spring Pizza"); 44 | pizza.setPrice(new BigDecimal("1200")); 45 | restTemplate.postForObject("/", pizza, Void.class); 46 | List ret = Arrays.asList(restTemplate.getForObject("/", Pizza[].class)); 47 | assertThat(json.write(ret)).isEqualToJson("expected.json"); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /demo-ebean/src/test/resources/com/example/expected.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "id": 1, 4 | "name": "Hello Pizza", 5 | "price": 1000.00, 6 | "toppings": [ 7 | { 8 | "id": 1, 9 | "name": "Veggies" 10 | }, 11 | { 12 | "id": 2, 13 | "name": "Roma Tomatoes" 14 | }, 15 | { 16 | "id": 3, 17 | "name": "Red Onions" 18 | }, 19 | { 20 | "id": 4, 21 | "name": "Spinach" 22 | }, 23 | { 24 | "id": 5, 25 | "name": "Broccoli" 26 | } 27 | ], 28 | "base": { 29 | "id": 1, 30 | "name": "Thin Crust" 31 | } 32 | }, 33 | { 34 | "id": 2, 35 | "name": "Spring Pizza", 36 | "price": 1200.00, 37 | "toppings": [ 38 | { 39 | "id": 3, 40 | "name": "Red Onions" 41 | }, 42 | { 43 | "id": 10, 44 | "name": "Meats" 45 | } 46 | ], 47 | "base": { 48 | "id": 2, 49 | "name": "Flatbread Crust" 50 | } 51 | } 52 | ] 53 | -------------------------------------------------------------------------------- /demo-iciql/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/making/spring-boot-db-samples/c2d01559bbd8843f4e239b47734c08fe481afdee/demo-iciql/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /demo-iciql/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.3.9/apache-maven-3.3.9-bin.zip 2 | -------------------------------------------------------------------------------- /demo-iciql/README.md: -------------------------------------------------------------------------------- 1 | Get all pizza 2 | 3 | ``` console 4 | curl localhost:8080 5 | ``` 6 | 7 | Add a pizza 8 | 9 | ``` console 10 | curl -d '{"name" : "Spring Pizza", "price" : 1200, "toppings" : [{"id" : 3}, {"id" : 10}], "base" : {"id" : "2"}}' -H 'Content-Type: application/json' localhost:8080 11 | ``` -------------------------------------------------------------------------------- /demo-iciql/src/main/java/com/example/DemoIciqlApplication.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import java.math.BigDecimal; 4 | import java.util.Arrays; 5 | 6 | import org.springframework.boot.CommandLineRunner; 7 | import org.springframework.boot.SpringApplication; 8 | import org.springframework.boot.autoconfigure.SpringBootApplication; 9 | import org.springframework.context.annotation.Bean; 10 | 11 | import com.example.model.Base; 12 | import com.example.model.Pizza; 13 | import com.example.model.Topping; 14 | 15 | @SpringBootApplication 16 | public class DemoIciqlApplication { 17 | 18 | public static void main(String[] args) { 19 | SpringApplication.run(DemoIciqlApplication.class, args); 20 | } 21 | 22 | @Bean 23 | CommandLineRunner runner(PizzaRepository pizzaRepository) { 24 | return a -> { 25 | Pizza pizza = new Pizza(); 26 | pizza.setBase(new Base(1L)); 27 | pizza.setToppings(Arrays.asList(new Topping(1L), new Topping(2L), new Topping(3L), new Topping(4L), new Topping(5L))); 28 | pizza.setName("Hello Pizza"); 29 | pizza.setPrice(new BigDecimal("1000")); 30 | pizzaRepository.save(pizza); 31 | }; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /demo-iciql/src/main/java/com/example/PizzaController.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import com.example.model.Pizza; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | import org.springframework.web.bind.annotation.PostMapping; 6 | import org.springframework.web.bind.annotation.RequestBody; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | import java.util.List; 10 | 11 | @RestController 12 | public class PizzaController { 13 | private final PizzaRepository pizzaRepository; 14 | 15 | public PizzaController(PizzaRepository pizzaRepository) { 16 | this.pizzaRepository = pizzaRepository; 17 | } 18 | 19 | @GetMapping 20 | List list() { 21 | return pizzaRepository.findOrderByIdAsc(); 22 | } 23 | 24 | @PostMapping 25 | Pizza add(@RequestBody Pizza pizza) { 26 | return pizzaRepository.save(pizza); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /demo-iciql/src/main/java/com/example/iciql/Base.java: -------------------------------------------------------------------------------- 1 | package com.example.iciql; 2 | 3 | import com.iciql.Iciql.IQColumn; 4 | import com.iciql.Iciql.IQTable; 5 | 6 | @IQTable(name = "base") 7 | public class Base { 8 | @IQColumn(primaryKey = true, autoIncrement = true) 9 | public Long id; 10 | @IQColumn 11 | public String name; 12 | } 13 | -------------------------------------------------------------------------------- /demo-iciql/src/main/java/com/example/iciql/Pizza.java: -------------------------------------------------------------------------------- 1 | package com.example.iciql; 2 | 3 | import java.math.BigDecimal; 4 | 5 | import com.iciql.Iciql.IQColumn; 6 | import com.iciql.Iciql.IQTable; 7 | 8 | @IQTable(name = "pizza") 9 | public class Pizza { 10 | @IQColumn(primaryKey = true, autoIncrement = true) 11 | public Long id; 12 | @IQColumn 13 | public String name; 14 | @IQColumn 15 | public BigDecimal price; 16 | @IQColumn(name = "base_id") 17 | public Long baseId; 18 | } 19 | -------------------------------------------------------------------------------- /demo-iciql/src/main/java/com/example/iciql/PizzaToppings.java: -------------------------------------------------------------------------------- 1 | package com.example.iciql; 2 | 3 | import com.iciql.Iciql.IQColumn; 4 | import com.iciql.Iciql.IQTable; 5 | 6 | @IQTable(name = "pizza_toppings") 7 | public class PizzaToppings { 8 | @IQColumn(name = "pizza_id", primaryKey = true) 9 | public Long pizzaId; 10 | @IQColumn(name = "toppings_id", primaryKey = true) 11 | public Long toppingsId; 12 | } 13 | -------------------------------------------------------------------------------- /demo-iciql/src/main/java/com/example/iciql/Topping.java: -------------------------------------------------------------------------------- 1 | package com.example.iciql; 2 | 3 | import com.iciql.Iciql.IQColumn; 4 | import com.iciql.Iciql.IQTable; 5 | 6 | @IQTable(name = "topping") 7 | public class Topping { 8 | @IQColumn(primaryKey = true, autoIncrement = true) 9 | public Long id; 10 | @IQColumn 11 | public String name; 12 | } 13 | -------------------------------------------------------------------------------- /demo-iciql/src/main/java/com/example/model/Base.java: -------------------------------------------------------------------------------- 1 | package com.example.model; 2 | 3 | import java.io.Serializable; 4 | 5 | public class Base implements Serializable { 6 | private Long id; 7 | private String name; 8 | 9 | public Base(Long id) { 10 | this.id = id; 11 | } 12 | 13 | Base() { 14 | 15 | } 16 | 17 | public Long getId() { 18 | return id; 19 | } 20 | 21 | public void setId(Long id) { 22 | this.id = id; 23 | } 24 | 25 | public String getName() { 26 | return name; 27 | } 28 | 29 | public void setName(String name) { 30 | this.name = name; 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return "Base{" + 36 | "id=" + id + 37 | ", name='" + name + '\'' + 38 | '}'; 39 | } 40 | 41 | @Override 42 | public boolean equals(Object o) { 43 | if (this == o) return true; 44 | if (!(o instanceof Base)) return false; 45 | 46 | Base base = (Base) o; 47 | 48 | if (id != null ? !id.equals(base.id) : base.id != null) return false; 49 | return name != null ? name.equals(base.name) : base.name == null; 50 | 51 | } 52 | 53 | @Override 54 | public int hashCode() { 55 | int result = id != null ? id.hashCode() : 0; 56 | result = 31 * result + (name != null ? name.hashCode() : 0); 57 | return result; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /demo-iciql/src/main/java/com/example/model/Topping.java: -------------------------------------------------------------------------------- 1 | package com.example.model; 2 | 3 | import java.io.Serializable; 4 | 5 | public class Topping implements Serializable { 6 | private Long id; 7 | private String name; 8 | 9 | public Topping(Long id) { 10 | this.id = id; 11 | } 12 | 13 | Topping() { 14 | 15 | } 16 | 17 | public Long getId() { 18 | return id; 19 | } 20 | 21 | public void setId(Long id) { 22 | this.id = id; 23 | } 24 | 25 | public String getName() { 26 | return name; 27 | } 28 | 29 | public void setName(String name) { 30 | this.name = name; 31 | } 32 | 33 | 34 | @Override 35 | public String toString() { 36 | return "Topping{" + 37 | "id=" + id + 38 | ", name='" + name + '\'' + 39 | '}'; 40 | } 41 | 42 | @Override 43 | public boolean equals(Object o) { 44 | if (this == o) return true; 45 | if (o == null || getClass() != o.getClass()) return false; 46 | 47 | Topping topping = (Topping) o; 48 | 49 | if (id != null ? !id.equals(topping.id) : topping.id != null) return false; 50 | return name != null ? name.equals(topping.name) : topping.name == null; 51 | 52 | } 53 | 54 | @Override 55 | public int hashCode() { 56 | int result = id != null ? id.hashCode() : 0; 57 | result = 31 * result + (name != null ? name.hashCode() : 0); 58 | return result; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /demo-iciql/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | logging.level.org.springframework.jdbc.datasource.DataSourceTransactionManager=DEBUG -------------------------------------------------------------------------------- /demo-iciql/src/main/resources/data.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO topping (name) VALUES ('Veggies'); 2 | INSERT INTO topping (name) VALUES ('Roma Tomatoes'); 3 | INSERT INTO topping (name) VALUES ('Red Onions'); 4 | INSERT INTO topping (name) VALUES ('Spinach'); 5 | INSERT INTO topping (name) VALUES ('Broccoli'); 6 | INSERT INTO topping (name) VALUES ('Pineapple'); 7 | INSERT INTO topping (name) VALUES ('Jalapeno Peppers'); 8 | INSERT INTO topping (name) VALUES ('Kalamata Olives'); 9 | INSERT INTO topping (name) VALUES ('Fresh Mushrooms'); 10 | INSERT INTO topping (name) VALUES ('Meats'); 11 | INSERT INTO topping (name) VALUES ('Anchovies'); 12 | INSERT INTO topping (name) VALUES ('Bacon Crumble'); 13 | INSERT INTO topping (name) VALUES ('Bacon Strips'); 14 | INSERT INTO topping (name) VALUES ('Chicken'); 15 | INSERT INTO topping (name) VALUES ('Ground Beef'); 16 | INSERT INTO topping (name) VALUES ('Italian Ham'); 17 | INSERT INTO topping (name) VALUES ('Spicy Italian Sausage'); 18 | INSERT INTO topping (name) VALUES ('Steak Strips'); 19 | INSERT INTO topping (name) VALUES ('Cheese'); 20 | INSERT INTO topping (name) VALUES ('Feta Cheese'); 21 | INSERT INTO topping (name) VALUES ('Parmesan Cheese'); 22 | INSERT INTO topping (name) VALUES ('Provolone Cheese'); 23 | INSERT INTO topping (name) VALUES ('Extra Cheese'); 24 | INSERT INTO topping (name) VALUES ('Four Cheese Blend Topping'); 25 | INSERT INTO topping (name) VALUES ('Goat Cheese'); 26 | INSERT INTO topping (name) VALUES ('Mozzarella'); 27 | 28 | INSERT INTO base(name) VALUES ('Thin Crust'); 29 | INSERT INTO base(name) VALUES ('Flatbread Crust'); 30 | INSERT INTO base(name) VALUES ('Thick Crust'); 31 | INSERT INTO base(name) VALUES ('Focaccia'); -------------------------------------------------------------------------------- /demo-iciql/src/main/resources/schema.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE base IF EXISTS; 2 | DROP TABLE pizza IF EXISTS; 3 | DROP TABLE pizza_toppings IF EXISTS; 4 | DROP TABLE topping IF EXISTS; 5 | 6 | CREATE TABLE base ( 7 | id BIGINT generated BY DEFAULT AS IDENTITY, 8 | name VARCHAR(255), 9 | PRIMARY KEY (id) 10 | ); 11 | CREATE TABLE pizza ( 12 | id BIGINT generated BY DEFAULT AS IDENTITY, 13 | name VARCHAR(255), 14 | price DECIMAL(19, 2), 15 | base_id BIGINT, 16 | PRIMARY KEY (id) 17 | ); 18 | CREATE TABLE pizza_toppings ( 19 | pizza_id BIGINT NOT NULL, 20 | toppings_id BIGINT NOT NULL, 21 | PRIMARY KEY (pizza_id, toppings_id) 22 | ); 23 | CREATE TABLE topping ( 24 | id BIGINT generated BY DEFAULT AS IDENTITY, 25 | name VARCHAR(255), 26 | PRIMARY KEY (id) 27 | ); 28 | 29 | ALTER TABLE pizza 30 | ADD CONSTRAINT FKm6kai6c5evr38duyfh06ue29u FOREIGN KEY (base_id) REFERENCES base; 31 | ALTER TABLE pizza_toppings 32 | ADD CONSTRAINT FK7tcti2cxau3csvbuwxn0scb93 FOREIGN KEY (toppings_id) REFERENCES topping; 33 | ALTER TABLE pizza_toppings 34 | ADD CONSTRAINT FK78l5qtferc9qhfkkgyv99lmst FOREIGN KEY (pizza_id) REFERENCES pizza; -------------------------------------------------------------------------------- /demo-iciql/src/test/java/com/example/PizzaControllerTests.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import com.example.model.Base; 4 | import com.example.model.Pizza; 5 | import com.example.model.Topping; 6 | import com.fasterxml.jackson.databind.ObjectMapper; 7 | import org.junit.Before; 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.boot.test.context.SpringBootTest; 12 | import org.springframework.boot.test.json.JacksonTester; 13 | import org.springframework.boot.test.web.client.TestRestTemplate; 14 | import org.springframework.test.context.junit4.SpringRunner; 15 | 16 | import java.math.BigDecimal; 17 | import java.util.Arrays; 18 | import java.util.List; 19 | 20 | import static org.assertj.core.api.Assertions.assertThat; 21 | 22 | @RunWith(SpringRunner.class) 23 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 24 | public class PizzaControllerTests { 25 | 26 | @Autowired 27 | TestRestTemplate restTemplate; 28 | @Autowired 29 | ObjectMapper objectMapper; 30 | 31 | private JacksonTester> json; 32 | 33 | @Before 34 | public void setup() { 35 | JacksonTester.initFields(this, objectMapper); 36 | } 37 | 38 | @Test 39 | public void contextLoads() throws Exception { 40 | Pizza pizza = new Pizza(); 41 | pizza.setBase(new Base(2L)); 42 | pizza.setToppings(Arrays.asList(new Topping(3L), new Topping(10L))); 43 | pizza.setName("Spring Pizza"); 44 | pizza.setPrice(new BigDecimal("1200")); 45 | restTemplate.postForObject("/", pizza, Void.class); 46 | List ret = Arrays.asList(restTemplate.getForObject("/", Pizza[].class)); 47 | assertThat(json.write(ret)).isEqualToJson("expected.json"); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /demo-iciql/src/test/resources/com/example/expected.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "id": 1, 4 | "name": "Hello Pizza", 5 | "price": 1000.00, 6 | "toppings": [ 7 | { 8 | "id": 1, 9 | "name": "Veggies" 10 | }, 11 | { 12 | "id": 2, 13 | "name": "Roma Tomatoes" 14 | }, 15 | { 16 | "id": 3, 17 | "name": "Red Onions" 18 | }, 19 | { 20 | "id": 4, 21 | "name": "Spinach" 22 | }, 23 | { 24 | "id": 5, 25 | "name": "Broccoli" 26 | } 27 | ], 28 | "base": { 29 | "id": 1, 30 | "name": "Thin Crust" 31 | } 32 | }, 33 | { 34 | "id": 2, 35 | "name": "Spring Pizza", 36 | "price": 1200.00, 37 | "toppings": [ 38 | { 39 | "id": 3, 40 | "name": "Red Onions" 41 | }, 42 | { 43 | "id": 10, 44 | "name": "Meats" 45 | } 46 | ], 47 | "base": { 48 | "id": 2, 49 | "name": "Flatbread Crust" 50 | } 51 | } 52 | ] 53 | -------------------------------------------------------------------------------- /demo-jdbi/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/making/spring-boot-db-samples/c2d01559bbd8843f4e239b47734c08fe481afdee/demo-jdbi/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /demo-jdbi/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.3.9/apache-maven-3.3.9-bin.zip 2 | -------------------------------------------------------------------------------- /demo-jdbi/README.md: -------------------------------------------------------------------------------- 1 | Get all pizza 2 | 3 | ``` console 4 | curl localhost:8080 5 | ``` 6 | 7 | Add a pizza 8 | 9 | ``` console 10 | curl -d '{"name" : "Spring Pizza", "price" : 1200, "toppings" : [{"id" : 3}, {"id" : 10}], "base" : {"id" : "2"}}' -H 'Content-Type: application/json' localhost:8080 11 | ``` -------------------------------------------------------------------------------- /demo-jdbi/src/main/java/com/example/DemoJdbiApplication.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import com.example.model.Base; 4 | import com.example.model.Pizza; 5 | import com.example.model.Topping; 6 | import org.skife.jdbi.v2.Handle; 7 | import org.skife.jdbi.v2.IDBI; 8 | import org.skife.jdbi.v2.spring.DBIFactoryBean; 9 | import org.springframework.boot.CommandLineRunner; 10 | import org.springframework.boot.SpringApplication; 11 | import org.springframework.boot.autoconfigure.SpringBootApplication; 12 | import org.springframework.context.annotation.Bean; 13 | 14 | import javax.sql.DataSource; 15 | import java.math.BigDecimal; 16 | import java.util.Arrays; 17 | 18 | @SpringBootApplication 19 | public class DemoJdbiApplication { 20 | 21 | public static void main(String[] args) { 22 | SpringApplication.run(DemoJdbiApplication.class, args); 23 | } 24 | 25 | @Bean 26 | CommandLineRunner runner(PizzaRepository pizzaRepository) { 27 | return a -> { 28 | Pizza pizza = new Pizza(); 29 | pizza.setBase(new Base(1L)); 30 | pizza.setToppings(Arrays.asList(new Topping(1L), new Topping(2L), new Topping(3L), new Topping(4L), new Topping(5L))); 31 | pizza.setName("Hello Pizza"); 32 | pizza.setPrice(new BigDecimal("1000")); 33 | pizzaRepository.save(pizza); 34 | }; 35 | } 36 | 37 | @Bean 38 | DBIFactoryBean dbi(DataSource dataSource) { 39 | return new DBIFactoryBean(dataSource); 40 | } 41 | 42 | @Bean(destroyMethod = "close") 43 | Handle handle(IDBI dbi) { 44 | return dbi.open(); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /demo-jdbi/src/main/java/com/example/PizzaController.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import com.example.model.Pizza; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | import org.springframework.web.bind.annotation.PostMapping; 6 | import org.springframework.web.bind.annotation.RequestBody; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | import java.util.List; 10 | 11 | @RestController 12 | public class PizzaController { 13 | private final PizzaRepository pizzaRepository; 14 | 15 | public PizzaController(PizzaRepository pizzaRepository) { 16 | this.pizzaRepository = pizzaRepository; 17 | } 18 | 19 | @GetMapping 20 | List list() { 21 | return pizzaRepository.findOrderByIdAsc(); 22 | } 23 | 24 | @PostMapping 25 | Pizza add(@RequestBody Pizza pizza) { 26 | return pizzaRepository.save(pizza); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /demo-jdbi/src/main/java/com/example/PizzaRepository.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import com.example.jdbi.PizzaDao; 4 | import com.example.model.Base; 5 | import com.example.model.Pizza; 6 | import com.example.model.Topping; 7 | import org.skife.jdbi.v2.Handle; 8 | import org.springframework.stereotype.Repository; 9 | import org.springframework.transaction.annotation.Transactional; 10 | 11 | import java.util.List; 12 | import java.util.stream.Collectors; 13 | 14 | @Repository 15 | public class PizzaRepository { 16 | private final PizzaDao pizzaDao; 17 | 18 | public PizzaRepository(Handle handle) { 19 | this.pizzaDao = handle.attach(PizzaDao.class); 20 | } 21 | 22 | public List findOrderByIdAsc() { 23 | return pizzaDao.findOrderByIdAsc().stream() 24 | .collect(Collectors.groupingBy(com.example.jdbi.Pizza::getId)) 25 | .entrySet() 26 | .stream() 27 | .map(e -> { 28 | com.example.jdbi.Pizza dto = e.getValue().get(0); 29 | Pizza pizza = new Pizza(); 30 | pizza.setId(dto.getId()); 31 | pizza.setName(dto.getName()); 32 | pizza.setPrice(dto.getPrice()); 33 | Base base = new Base(dto.getId()); 34 | base.setName(dto.getBaseName()); 35 | pizza.setBase(base); 36 | pizza.setToppings(e.getValue().stream() 37 | .map(x -> { 38 | Topping topping = new Topping(x.getToppingId()); 39 | topping.setName(x.getToppingName()); 40 | return topping; 41 | }) 42 | .collect(Collectors.toList())); 43 | return pizza; 44 | }) 45 | .collect(Collectors.toList()); 46 | } 47 | 48 | @Transactional 49 | public Pizza save(Pizza pizza) { 50 | long pizzaId = pizzaDao.insertPizza(pizza.getBase().getId(), pizza.getName(), pizza.getPrice()); 51 | pizzaDao.insertPizzaToppings(pizzaId, pizza.getToppings().stream().map(Topping::getId).collect(Collectors.toList())); 52 | pizza.setId(pizzaId); 53 | return pizza; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /demo-jdbi/src/main/java/com/example/jdbi/Pizza.java: -------------------------------------------------------------------------------- 1 | package com.example.jdbi; 2 | 3 | import java.math.BigDecimal; 4 | 5 | public class Pizza { 6 | private Long id; 7 | private String name; 8 | private BigDecimal price; 9 | private Long baseId; 10 | private String baseName; 11 | private Long toppingId; 12 | private String toppingName; 13 | 14 | public Long getId() { 15 | return id; 16 | } 17 | 18 | public void setId(Long id) { 19 | this.id = id; 20 | } 21 | 22 | public String getName() { 23 | return name; 24 | } 25 | 26 | public void setName(String name) { 27 | this.name = name; 28 | } 29 | 30 | public BigDecimal getPrice() { 31 | return price; 32 | } 33 | 34 | public void setPrice(BigDecimal price) { 35 | this.price = price; 36 | } 37 | 38 | public Long getBaseId() { 39 | return baseId; 40 | } 41 | 42 | public void setBaseId(Long baseId) { 43 | this.baseId = baseId; 44 | } 45 | 46 | public String getBaseName() { 47 | return baseName; 48 | } 49 | 50 | public void setBaseName(String baseName) { 51 | this.baseName = baseName; 52 | } 53 | 54 | public Long getToppingId() { 55 | return toppingId; 56 | } 57 | 58 | public void setToppingId(Long toppingId) { 59 | this.toppingId = toppingId; 60 | } 61 | 62 | public String getToppingName() { 63 | return toppingName; 64 | } 65 | 66 | public void setToppingName(String toppingName) { 67 | this.toppingName = toppingName; 68 | } 69 | 70 | @Override 71 | public String toString() { 72 | return "Pizza{" + 73 | "id=" + id + 74 | ", name='" + name + '\'' + 75 | ", price=" + price + 76 | ", baseId=" + baseId + 77 | ", baseName='" + baseName + '\'' + 78 | ", toppingId=" + toppingId + 79 | ", toppingName='" + toppingName + '\'' + 80 | '}'; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /demo-jdbi/src/main/java/com/example/jdbi/PizzaDao.java: -------------------------------------------------------------------------------- 1 | package com.example.jdbi; 2 | 3 | import org.skife.jdbi.v2.sqlobject.*; 4 | import org.skife.jdbi.v2.sqlobject.customizers.Mapper; 5 | 6 | import java.math.BigDecimal; 7 | import java.util.List; 8 | 9 | public interface PizzaDao { 10 | @SqlUpdate("INSERT INTO pizza(base_id, name, price) VALUES (:baseId, :name, :price)") 11 | @GetGeneratedKeys 12 | long insertPizza(@Bind("baseId") Long baseId, @Bind("name") String name, @Bind("price") BigDecimal price); 13 | 14 | @SqlBatch("INSERT INTO pizza_toppings (pizza_id, toppings_id) VALUES (:pizzaId, :toppingsId)") 15 | void insertPizzaToppings(@Bind("pizzaId") Long pizzaId, @Bind("toppingsId") List toppingsIds); 16 | 17 | @SqlQuery("SELECT p.id, p.name, p.price, b.id AS baseId, b.name AS baseName, t.id AS toppingId, t.name AS toppingName FROM pizza p INNER JOIN base b ON (b.id = p.base_id) INNER JOIN pizza_toppings pt ON (pt.pizza_id = p.id) INNER JOIN topping t ON (t.id = pt.toppings_id) ORDER BY p.id ASC") 18 | @Mapper(PizzaMapper.class) 19 | List findOrderByIdAsc(); 20 | 21 | void close(); 22 | } 23 | -------------------------------------------------------------------------------- /demo-jdbi/src/main/java/com/example/jdbi/PizzaMapper.java: -------------------------------------------------------------------------------- 1 | package com.example.jdbi; 2 | 3 | import org.skife.jdbi.v2.BeanMapper; 4 | 5 | 6 | public class PizzaMapper extends BeanMapper { 7 | public PizzaMapper() { 8 | super(Pizza.class); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /demo-jdbi/src/main/java/com/example/model/Base.java: -------------------------------------------------------------------------------- 1 | package com.example.model; 2 | 3 | import java.io.Serializable; 4 | 5 | public class Base implements Serializable { 6 | private Long id; 7 | private String name; 8 | 9 | public Base(Long id) { 10 | this.id = id; 11 | } 12 | 13 | Base() { 14 | 15 | } 16 | 17 | public Long getId() { 18 | return id; 19 | } 20 | 21 | public void setId(Long id) { 22 | this.id = id; 23 | } 24 | 25 | public String getName() { 26 | return name; 27 | } 28 | 29 | public void setName(String name) { 30 | this.name = name; 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return "Base{" + 36 | "id=" + id + 37 | ", name='" + name + '\'' + 38 | '}'; 39 | } 40 | 41 | @Override 42 | public boolean equals(Object o) { 43 | if (this == o) return true; 44 | if (!(o instanceof Base)) return false; 45 | 46 | Base base = (Base) o; 47 | 48 | if (id != null ? !id.equals(base.id) : base.id != null) return false; 49 | return name != null ? name.equals(base.name) : base.name == null; 50 | 51 | } 52 | 53 | @Override 54 | public int hashCode() { 55 | int result = id != null ? id.hashCode() : 0; 56 | result = 31 * result + (name != null ? name.hashCode() : 0); 57 | return result; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /demo-jdbi/src/main/java/com/example/model/Topping.java: -------------------------------------------------------------------------------- 1 | package com.example.model; 2 | 3 | import java.io.Serializable; 4 | 5 | public class Topping implements Serializable { 6 | private Long id; 7 | private String name; 8 | 9 | public Topping(Long id) { 10 | this.id = id; 11 | } 12 | 13 | Topping() { 14 | 15 | } 16 | 17 | public Long getId() { 18 | return id; 19 | } 20 | 21 | public void setId(Long id) { 22 | this.id = id; 23 | } 24 | 25 | public String getName() { 26 | return name; 27 | } 28 | 29 | public void setName(String name) { 30 | this.name = name; 31 | } 32 | 33 | 34 | @Override 35 | public String toString() { 36 | return "Topping{" + 37 | "id=" + id + 38 | ", name='" + name + '\'' + 39 | '}'; 40 | } 41 | 42 | @Override 43 | public boolean equals(Object o) { 44 | if (this == o) return true; 45 | if (o == null || getClass() != o.getClass()) return false; 46 | 47 | Topping topping = (Topping) o; 48 | 49 | if (id != null ? !id.equals(topping.id) : topping.id != null) return false; 50 | return name != null ? name.equals(topping.name) : topping.name == null; 51 | 52 | } 53 | 54 | @Override 55 | public int hashCode() { 56 | int result = id != null ? id.hashCode() : 0; 57 | result = 31 * result + (name != null ? name.hashCode() : 0); 58 | return result; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /demo-jdbi/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | logging.level.org.springframework.jdbc.datasource.DataSourceTransactionManager=DEBUG -------------------------------------------------------------------------------- /demo-jdbi/src/main/resources/data.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO topping (name) VALUES ('Veggies'); 2 | INSERT INTO topping (name) VALUES ('Roma Tomatoes'); 3 | INSERT INTO topping (name) VALUES ('Red Onions'); 4 | INSERT INTO topping (name) VALUES ('Spinach'); 5 | INSERT INTO topping (name) VALUES ('Broccoli'); 6 | INSERT INTO topping (name) VALUES ('Pineapple'); 7 | INSERT INTO topping (name) VALUES ('Jalapeno Peppers'); 8 | INSERT INTO topping (name) VALUES ('Kalamata Olives'); 9 | INSERT INTO topping (name) VALUES ('Fresh Mushrooms'); 10 | INSERT INTO topping (name) VALUES ('Meats'); 11 | INSERT INTO topping (name) VALUES ('Anchovies'); 12 | INSERT INTO topping (name) VALUES ('Bacon Crumble'); 13 | INSERT INTO topping (name) VALUES ('Bacon Strips'); 14 | INSERT INTO topping (name) VALUES ('Chicken'); 15 | INSERT INTO topping (name) VALUES ('Ground Beef'); 16 | INSERT INTO topping (name) VALUES ('Italian Ham'); 17 | INSERT INTO topping (name) VALUES ('Spicy Italian Sausage'); 18 | INSERT INTO topping (name) VALUES ('Steak Strips'); 19 | INSERT INTO topping (name) VALUES ('Cheese'); 20 | INSERT INTO topping (name) VALUES ('Feta Cheese'); 21 | INSERT INTO topping (name) VALUES ('Parmesan Cheese'); 22 | INSERT INTO topping (name) VALUES ('Provolone Cheese'); 23 | INSERT INTO topping (name) VALUES ('Extra Cheese'); 24 | INSERT INTO topping (name) VALUES ('Four Cheese Blend Topping'); 25 | INSERT INTO topping (name) VALUES ('Goat Cheese'); 26 | INSERT INTO topping (name) VALUES ('Mozzarella'); 27 | 28 | INSERT INTO base(name) VALUES ('Thin Crust'); 29 | INSERT INTO base(name) VALUES ('Flatbread Crust'); 30 | INSERT INTO base(name) VALUES ('Thick Crust'); 31 | INSERT INTO base(name) VALUES ('Focaccia'); -------------------------------------------------------------------------------- /demo-jdbi/src/main/resources/schema.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE base IF EXISTS; 2 | DROP TABLE pizza IF EXISTS; 3 | DROP TABLE pizza_toppings IF EXISTS; 4 | DROP TABLE topping IF EXISTS; 5 | 6 | CREATE TABLE base ( 7 | id BIGINT generated BY DEFAULT AS IDENTITY, 8 | name VARCHAR(255), 9 | PRIMARY KEY (id) 10 | ); 11 | CREATE TABLE pizza ( 12 | id BIGINT generated BY DEFAULT AS IDENTITY, 13 | name VARCHAR(255), 14 | price DECIMAL(19, 2), 15 | base_id BIGINT, 16 | PRIMARY KEY (id) 17 | ); 18 | CREATE TABLE pizza_toppings ( 19 | pizza_id BIGINT NOT NULL, 20 | toppings_id BIGINT NOT NULL, 21 | PRIMARY KEY (pizza_id, toppings_id) 22 | ); 23 | CREATE TABLE topping ( 24 | id BIGINT generated BY DEFAULT AS IDENTITY, 25 | name VARCHAR(255), 26 | PRIMARY KEY (id) 27 | ); 28 | 29 | ALTER TABLE pizza 30 | ADD CONSTRAINT FKm6kai6c5evr38duyfh06ue29u FOREIGN KEY (base_id) REFERENCES base; 31 | ALTER TABLE pizza_toppings 32 | ADD CONSTRAINT FK7tcti2cxau3csvbuwxn0scb93 FOREIGN KEY (toppings_id) REFERENCES topping; 33 | ALTER TABLE pizza_toppings 34 | ADD CONSTRAINT FK78l5qtferc9qhfkkgyv99lmst FOREIGN KEY (pizza_id) REFERENCES pizza; -------------------------------------------------------------------------------- /demo-jdbi/src/test/java/com/example/PizzaControllerTests.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import com.example.model.Base; 4 | import com.example.model.Pizza; 5 | import com.example.model.Topping; 6 | import com.fasterxml.jackson.databind.ObjectMapper; 7 | import org.junit.Before; 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.boot.test.context.SpringBootTest; 12 | import org.springframework.boot.test.json.JacksonTester; 13 | import org.springframework.boot.test.web.client.TestRestTemplate; 14 | import org.springframework.test.context.junit4.SpringRunner; 15 | 16 | import java.math.BigDecimal; 17 | import java.util.Arrays; 18 | import java.util.List; 19 | 20 | import static org.assertj.core.api.Assertions.assertThat; 21 | 22 | @RunWith(SpringRunner.class) 23 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 24 | public class PizzaControllerTests { 25 | 26 | @Autowired 27 | TestRestTemplate restTemplate; 28 | @Autowired 29 | ObjectMapper objectMapper; 30 | 31 | private JacksonTester> json; 32 | 33 | @Before 34 | public void setup() { 35 | JacksonTester.initFields(this, objectMapper); 36 | } 37 | 38 | @Test 39 | public void contextLoads() throws Exception { 40 | Pizza pizza = new Pizza(); 41 | pizza.setBase(new Base(2L)); 42 | pizza.setToppings(Arrays.asList(new Topping(3L), new Topping(10L))); 43 | pizza.setName("Spring Pizza"); 44 | pizza.setPrice(new BigDecimal("1200")); 45 | restTemplate.postForObject("/", pizza, Void.class); 46 | List ret = Arrays.asList(restTemplate.getForObject("/", Pizza[].class)); 47 | assertThat(json.write(ret)).isEqualToJson("expected.json"); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /demo-jdbi/src/test/resources/com/example/expected.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "id": 1, 4 | "name": "Hello Pizza", 5 | "price": 1000.00, 6 | "toppings": [ 7 | { 8 | "id": 1, 9 | "name": "Veggies" 10 | }, 11 | { 12 | "id": 2, 13 | "name": "Roma Tomatoes" 14 | }, 15 | { 16 | "id": 3, 17 | "name": "Red Onions" 18 | }, 19 | { 20 | "id": 4, 21 | "name": "Spinach" 22 | }, 23 | { 24 | "id": 5, 25 | "name": "Broccoli" 26 | } 27 | ], 28 | "base": { 29 | "id": 1, 30 | "name": "Thin Crust" 31 | } 32 | }, 33 | { 34 | "id": 2, 35 | "name": "Spring Pizza", 36 | "price": 1200.00, 37 | "toppings": [ 38 | { 39 | "id": 3, 40 | "name": "Red Onions" 41 | }, 42 | { 43 | "id": 10, 44 | "name": "Meats" 45 | } 46 | ], 47 | "base": { 48 | "id": 2, 49 | "name": "Flatbread Crust" 50 | } 51 | } 52 | ] 53 | -------------------------------------------------------------------------------- /demo-jooq/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/making/spring-boot-db-samples/c2d01559bbd8843f4e239b47734c08fe481afdee/demo-jooq/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /demo-jooq/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.3.9/apache-maven-3.3.9-bin.zip 2 | -------------------------------------------------------------------------------- /demo-jooq/README.md: -------------------------------------------------------------------------------- 1 | Get all pizza 2 | 3 | ``` console 4 | curl localhost:8080 5 | ``` 6 | 7 | Add a pizza 8 | 9 | ``` console 10 | curl -d '{"name" : "Spring Pizza", "price" : 1200, "toppings" : [{"id" : 3}, {"id" : 10}], "base" : {"id" : "2"}}' -H 'Content-Type: application/json' localhost:8080 11 | ``` 12 | 13 | ### (Optional) Re-generate Sources 14 | 15 | After start and stop the application 16 | 17 | ``` console 18 | rm -rf src/generated 19 | mvn clean generate-sources -Pgenerate 20 | ``` -------------------------------------------------------------------------------- /demo-jooq/src/generated/java/com/example/jooq/DefaultCatalog.java: -------------------------------------------------------------------------------- 1 | /** 2 | * This class is generated by jOOQ 3 | */ 4 | package com.example.jooq; 5 | 6 | 7 | import java.util.ArrayList; 8 | import java.util.Arrays; 9 | import java.util.List; 10 | 11 | import javax.annotation.Generated; 12 | 13 | import org.jooq.Schema; 14 | import org.jooq.impl.CatalogImpl; 15 | 16 | 17 | /** 18 | * This class is generated by jOOQ. 19 | */ 20 | @Generated( 21 | value = { 22 | "http://www.jooq.org", 23 | "jOOQ version:3.8.2" 24 | }, 25 | comments = "This class is generated by jOOQ" 26 | ) 27 | @SuppressWarnings({ "all", "unchecked", "rawtypes" }) 28 | public class DefaultCatalog extends CatalogImpl { 29 | 30 | private static final long serialVersionUID = 1051560102; 31 | 32 | /** 33 | * The reference instance of 34 | */ 35 | public static final DefaultCatalog DEFAULT_CATALOG = new DefaultCatalog(); 36 | 37 | /** 38 | * The schema PUBLIC. 39 | */ 40 | public final Public PUBLIC = com.example.jooq.Public.PUBLIC; 41 | 42 | /** 43 | * No further instances allowed 44 | */ 45 | private DefaultCatalog() { 46 | super(""); 47 | } 48 | 49 | @Override 50 | public final List getSchemas() { 51 | List result = new ArrayList(); 52 | result.addAll(getSchemas0()); 53 | return result; 54 | } 55 | 56 | private final List getSchemas0() { 57 | return Arrays.asList( 58 | Public.PUBLIC); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /demo-jooq/src/generated/java/com/example/jooq/Sequences.java: -------------------------------------------------------------------------------- 1 | /** 2 | * This class is generated by jOOQ 3 | */ 4 | package com.example.jooq; 5 | 6 | 7 | import javax.annotation.Generated; 8 | 9 | import org.jooq.Sequence; 10 | import org.jooq.impl.SequenceImpl; 11 | 12 | 13 | /** 14 | * Convenience access to all sequences in PUBLIC 15 | */ 16 | @Generated( 17 | value = { 18 | "http://www.jooq.org", 19 | "jOOQ version:3.8.2" 20 | }, 21 | comments = "This class is generated by jOOQ" 22 | ) 23 | @SuppressWarnings({ "all", "unchecked", "rawtypes" }) 24 | public class Sequences { 25 | 26 | /** 27 | * The sequence PUBLIC.SYSTEM_SEQUENCE_323849C2_C132_4586_A3D1_484C30570868 28 | */ 29 | public static final Sequence SYSTEM_SEQUENCE_323849C2_C132_4586_A3D1_484C30570868 = new SequenceImpl("SYSTEM_SEQUENCE_323849C2_C132_4586_A3D1_484C30570868", Public.PUBLIC, org.jooq.impl.SQLDataType.BIGINT); 30 | 31 | /** 32 | * The sequence PUBLIC.SYSTEM_SEQUENCE_4C00A008_A398_49BD_A34B_F3C37950D4C4 33 | */ 34 | public static final Sequence SYSTEM_SEQUENCE_4C00A008_A398_49BD_A34B_F3C37950D4C4 = new SequenceImpl("SYSTEM_SEQUENCE_4C00A008_A398_49BD_A34B_F3C37950D4C4", Public.PUBLIC, org.jooq.impl.SQLDataType.BIGINT); 35 | 36 | /** 37 | * The sequence PUBLIC.SYSTEM_SEQUENCE_9417DF40_8B3D_4085_A9A4_12CFC1049036 38 | */ 39 | public static final Sequence SYSTEM_SEQUENCE_9417DF40_8B3D_4085_A9A4_12CFC1049036 = new SequenceImpl("SYSTEM_SEQUENCE_9417DF40_8B3D_4085_A9A4_12CFC1049036", Public.PUBLIC, org.jooq.impl.SQLDataType.BIGINT); 40 | } 41 | -------------------------------------------------------------------------------- /demo-jooq/src/generated/java/com/example/jooq/Tables.java: -------------------------------------------------------------------------------- 1 | /** 2 | * This class is generated by jOOQ 3 | */ 4 | package com.example.jooq; 5 | 6 | 7 | import com.example.jooq.tables.Base; 8 | import com.example.jooq.tables.Pizza; 9 | import com.example.jooq.tables.PizzaToppings; 10 | import com.example.jooq.tables.Topping; 11 | 12 | import javax.annotation.Generated; 13 | 14 | 15 | /** 16 | * Convenience access to all tables in PUBLIC 17 | */ 18 | @Generated( 19 | value = { 20 | "http://www.jooq.org", 21 | "jOOQ version:3.8.2" 22 | }, 23 | comments = "This class is generated by jOOQ" 24 | ) 25 | @SuppressWarnings({ "all", "unchecked", "rawtypes" }) 26 | public class Tables { 27 | 28 | /** 29 | * The table PUBLIC.BASE. 30 | */ 31 | public static final Base BASE = com.example.jooq.tables.Base.BASE; 32 | 33 | /** 34 | * The table PUBLIC.PIZZA. 35 | */ 36 | public static final Pizza PIZZA = com.example.jooq.tables.Pizza.PIZZA; 37 | 38 | /** 39 | * The table PUBLIC.PIZZA_TOPPINGS. 40 | */ 41 | public static final PizzaToppings PIZZA_TOPPINGS = com.example.jooq.tables.PizzaToppings.PIZZA_TOPPINGS; 42 | 43 | /** 44 | * The table PUBLIC.TOPPING. 45 | */ 46 | public static final Topping TOPPING = com.example.jooq.tables.Topping.TOPPING; 47 | } 48 | -------------------------------------------------------------------------------- /demo-jooq/src/main/java/com/example/DemoJooqApplication.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import com.example.model.Base; 4 | import com.example.model.Pizza; 5 | import com.example.model.Topping; 6 | import org.springframework.boot.CommandLineRunner; 7 | import org.springframework.boot.SpringApplication; 8 | import org.springframework.boot.autoconfigure.SpringBootApplication; 9 | import org.springframework.context.annotation.Bean; 10 | 11 | import java.math.BigDecimal; 12 | import java.util.Arrays; 13 | 14 | @SpringBootApplication 15 | public class DemoJooqApplication { 16 | 17 | public static void main(String[] args) { 18 | SpringApplication.run(DemoJooqApplication.class, args); 19 | } 20 | 21 | @Bean 22 | CommandLineRunner runner(PizzaRepository pizzaRepository) { 23 | return a -> { 24 | Pizza pizza = new Pizza(); 25 | pizza.setBase(new Base(1L)); 26 | pizza.setToppings(Arrays.asList(new Topping(1L), new Topping(2L), new Topping(3L), new Topping(4L), new Topping(5L))); 27 | pizza.setName("Hello Pizza"); 28 | pizza.setPrice(new BigDecimal("1000")); 29 | pizzaRepository.save(pizza); 30 | }; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /demo-jooq/src/main/java/com/example/PizzaController.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import com.example.model.Pizza; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | import org.springframework.web.bind.annotation.PostMapping; 6 | import org.springframework.web.bind.annotation.RequestBody; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | import java.util.List; 10 | 11 | @RestController 12 | public class PizzaController { 13 | private final PizzaRepository pizzaRepository; 14 | 15 | public PizzaController(PizzaRepository pizzaRepository) { 16 | this.pizzaRepository = pizzaRepository; 17 | } 18 | 19 | @GetMapping 20 | List list() { 21 | return pizzaRepository.findOrderByIdAsc(); 22 | } 23 | 24 | @PostMapping 25 | Pizza add(@RequestBody Pizza pizza) { 26 | return pizzaRepository.save(pizza); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /demo-jooq/src/main/java/com/example/model/Base.java: -------------------------------------------------------------------------------- 1 | package com.example.model; 2 | 3 | import java.io.Serializable; 4 | 5 | public class Base implements Serializable { 6 | private Long id; 7 | private String name; 8 | 9 | public Base(Long id) { 10 | this.id = id; 11 | } 12 | 13 | Base() { 14 | 15 | } 16 | 17 | public Long getId() { 18 | return id; 19 | } 20 | 21 | public void setId(Long id) { 22 | this.id = id; 23 | } 24 | 25 | public String getName() { 26 | return name; 27 | } 28 | 29 | public void setName(String name) { 30 | this.name = name; 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return "Base{" + 36 | "id=" + id + 37 | ", name='" + name + '\'' + 38 | '}'; 39 | } 40 | 41 | @Override 42 | public boolean equals(Object o) { 43 | if (this == o) return true; 44 | if (!(o instanceof Base)) return false; 45 | 46 | Base base = (Base) o; 47 | 48 | if (id != null ? !id.equals(base.id) : base.id != null) return false; 49 | return name != null ? name.equals(base.name) : base.name == null; 50 | 51 | } 52 | 53 | @Override 54 | public int hashCode() { 55 | int result = id != null ? id.hashCode() : 0; 56 | result = 31 * result + (name != null ? name.hashCode() : 0); 57 | return result; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /demo-jooq/src/main/java/com/example/model/Topping.java: -------------------------------------------------------------------------------- 1 | package com.example.model; 2 | 3 | import java.io.Serializable; 4 | 5 | public class Topping implements Serializable { 6 | private Long id; 7 | private String name; 8 | 9 | public Topping(Long id) { 10 | this.id = id; 11 | } 12 | 13 | Topping() { 14 | 15 | } 16 | 17 | public Long getId() { 18 | return id; 19 | } 20 | 21 | public void setId(Long id) { 22 | this.id = id; 23 | } 24 | 25 | public String getName() { 26 | return name; 27 | } 28 | 29 | public void setName(String name) { 30 | this.name = name; 31 | } 32 | 33 | 34 | @Override 35 | public String toString() { 36 | return "Topping{" + 37 | "id=" + id + 38 | ", name='" + name + '\'' + 39 | '}'; 40 | } 41 | 42 | @Override 43 | public boolean equals(Object o) { 44 | if (this == o) return true; 45 | if (o == null || getClass() != o.getClass()) return false; 46 | 47 | Topping topping = (Topping) o; 48 | 49 | if (id != null ? !id.equals(topping.id) : topping.id != null) return false; 50 | return name != null ? name.equals(topping.name) : topping.name == null; 51 | 52 | } 53 | 54 | @Override 55 | public int hashCode() { 56 | int result = id != null ? id.hashCode() : 0; 57 | result = 31 * result + (name != null ? name.hashCode() : 0); 58 | return result; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /demo-jooq/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:h2:file:/tmp/db/testdb 2 | logging.level.org.jooq.tools=DEBUG 3 | logging.level.org.springframework.jdbc.datasource.DataSourceTransactionManager=DEBUG -------------------------------------------------------------------------------- /demo-jooq/src/main/resources/data.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO topping (name) VALUES ('Veggies'); 2 | INSERT INTO topping (name) VALUES ('Roma Tomatoes'); 3 | INSERT INTO topping (name) VALUES ('Red Onions'); 4 | INSERT INTO topping (name) VALUES ('Spinach'); 5 | INSERT INTO topping (name) VALUES ('Broccoli'); 6 | INSERT INTO topping (name) VALUES ('Pineapple'); 7 | INSERT INTO topping (name) VALUES ('Jalapeno Peppers'); 8 | INSERT INTO topping (name) VALUES ('Kalamata Olives'); 9 | INSERT INTO topping (name) VALUES ('Fresh Mushrooms'); 10 | INSERT INTO topping (name) VALUES ('Meats'); 11 | INSERT INTO topping (name) VALUES ('Anchovies'); 12 | INSERT INTO topping (name) VALUES ('Bacon Crumble'); 13 | INSERT INTO topping (name) VALUES ('Bacon Strips'); 14 | INSERT INTO topping (name) VALUES ('Chicken'); 15 | INSERT INTO topping (name) VALUES ('Ground Beef'); 16 | INSERT INTO topping (name) VALUES ('Italian Ham'); 17 | INSERT INTO topping (name) VALUES ('Spicy Italian Sausage'); 18 | INSERT INTO topping (name) VALUES ('Steak Strips'); 19 | INSERT INTO topping (name) VALUES ('Cheese'); 20 | INSERT INTO topping (name) VALUES ('Feta Cheese'); 21 | INSERT INTO topping (name) VALUES ('Parmesan Cheese'); 22 | INSERT INTO topping (name) VALUES ('Provolone Cheese'); 23 | INSERT INTO topping (name) VALUES ('Extra Cheese'); 24 | INSERT INTO topping (name) VALUES ('Four Cheese Blend Topping'); 25 | INSERT INTO topping (name) VALUES ('Goat Cheese'); 26 | INSERT INTO topping (name) VALUES ('Mozzarella'); 27 | 28 | INSERT INTO base(name) VALUES ('Thin Crust'); 29 | INSERT INTO base(name) VALUES ('Flatbread Crust'); 30 | INSERT INTO base(name) VALUES ('Thick Crust'); 31 | INSERT INTO base(name) VALUES ('Focaccia'); -------------------------------------------------------------------------------- /demo-jooq/src/main/resources/schema.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE base IF EXISTS; 2 | DROP TABLE pizza IF EXISTS; 3 | DROP TABLE pizza_toppings IF EXISTS; 4 | DROP TABLE topping IF EXISTS; 5 | 6 | CREATE TABLE base ( 7 | id BIGINT generated BY DEFAULT AS IDENTITY, 8 | name VARCHAR(255), 9 | PRIMARY KEY (id) 10 | ); 11 | CREATE TABLE pizza ( 12 | id BIGINT generated BY DEFAULT AS IDENTITY, 13 | name VARCHAR(255), 14 | price DECIMAL(19, 2), 15 | base_id BIGINT, 16 | PRIMARY KEY (id) 17 | ); 18 | CREATE TABLE pizza_toppings ( 19 | pizza_id BIGINT NOT NULL, 20 | toppings_id BIGINT NOT NULL, 21 | PRIMARY KEY (pizza_id, toppings_id) 22 | ); 23 | CREATE TABLE topping ( 24 | id BIGINT generated BY DEFAULT AS IDENTITY, 25 | name VARCHAR(255), 26 | PRIMARY KEY (id) 27 | ); 28 | 29 | ALTER TABLE pizza 30 | ADD CONSTRAINT FKm6kai6c5evr38duyfh06ue29u FOREIGN KEY (base_id) REFERENCES base; 31 | ALTER TABLE pizza_toppings 32 | ADD CONSTRAINT FK7tcti2cxau3csvbuwxn0scb93 FOREIGN KEY (toppings_id) REFERENCES topping; 33 | ALTER TABLE pizza_toppings 34 | ADD CONSTRAINT FK78l5qtferc9qhfkkgyv99lmst FOREIGN KEY (pizza_id) REFERENCES pizza; -------------------------------------------------------------------------------- /demo-jooq/src/test/java/com/example/PizzaControllerTests.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import com.example.model.Base; 4 | import com.example.model.Pizza; 5 | import com.example.model.Topping; 6 | import com.fasterxml.jackson.databind.ObjectMapper; 7 | import org.junit.Before; 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.boot.test.context.SpringBootTest; 12 | import org.springframework.boot.test.json.JacksonTester; 13 | import org.springframework.boot.test.web.client.TestRestTemplate; 14 | import org.springframework.test.context.junit4.SpringRunner; 15 | 16 | import java.math.BigDecimal; 17 | import java.util.Arrays; 18 | import java.util.List; 19 | 20 | import static org.assertj.core.api.Assertions.assertThat; 21 | 22 | @RunWith(SpringRunner.class) 23 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 24 | public class PizzaControllerTests { 25 | 26 | @Autowired 27 | TestRestTemplate restTemplate; 28 | @Autowired 29 | ObjectMapper objectMapper; 30 | 31 | private JacksonTester> json; 32 | 33 | @Before 34 | public void setup() { 35 | JacksonTester.initFields(this, objectMapper); 36 | } 37 | 38 | @Test 39 | public void contextLoads() throws Exception { 40 | Pizza pizza = new Pizza(); 41 | pizza.setBase(new Base(2L)); 42 | pizza.setToppings(Arrays.asList(new Topping(3L), new Topping(10L))); 43 | pizza.setName("Spring Pizza"); 44 | pizza.setPrice(new BigDecimal("1200")); 45 | restTemplate.postForObject("/", pizza, Void.class); 46 | List ret = Arrays.asList(restTemplate.getForObject("/", Pizza[].class)); 47 | assertThat(json.write(ret)).isEqualToJson("expected.json"); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /demo-jooq/src/test/resources/com/example/expected.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "id": 1, 4 | "name": "Hello Pizza", 5 | "price": 1000.00, 6 | "toppings": [ 7 | { 8 | "id": 1, 9 | "name": "Veggies" 10 | }, 11 | { 12 | "id": 2, 13 | "name": "Roma Tomatoes" 14 | }, 15 | { 16 | "id": 3, 17 | "name": "Red Onions" 18 | }, 19 | { 20 | "id": 4, 21 | "name": "Spinach" 22 | }, 23 | { 24 | "id": 5, 25 | "name": "Broccoli" 26 | } 27 | ], 28 | "base": { 29 | "id": 1, 30 | "name": "Thin Crust" 31 | } 32 | }, 33 | { 34 | "id": 2, 35 | "name": "Spring Pizza", 36 | "price": 1200.00, 37 | "toppings": [ 38 | { 39 | "id": 3, 40 | "name": "Red Onions" 41 | }, 42 | { 43 | "id": 10, 44 | "name": "Meats" 45 | } 46 | ], 47 | "base": { 48 | "id": 2, 49 | "name": "Flatbread Crust" 50 | } 51 | } 52 | ] 53 | -------------------------------------------------------------------------------- /demo-mybatis/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/making/spring-boot-db-samples/c2d01559bbd8843f4e239b47734c08fe481afdee/demo-mybatis/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /demo-mybatis/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.3.9/apache-maven-3.3.9-bin.zip 2 | -------------------------------------------------------------------------------- /demo-mybatis/README.md: -------------------------------------------------------------------------------- 1 | Get all pizza 2 | 3 | ``` console 4 | curl localhost:8080 5 | ``` 6 | 7 | Add a pizza 8 | 9 | ``` console 10 | curl -d '{"name" : "Spring Pizza", "price" : 1200, "toppings" : [{"id" : 3}, {"id" : 10}], "base" : {"id" : "2"}}' -H 'Content-Type: application/json' localhost:8080 11 | ``` -------------------------------------------------------------------------------- /demo-mybatis/src/main/java/com/example/DemoMybatisApplication.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import com.example.model.Base; 4 | import com.example.model.Pizza; 5 | import com.example.model.Topping; 6 | import org.springframework.boot.CommandLineRunner; 7 | import org.springframework.boot.SpringApplication; 8 | import org.springframework.boot.autoconfigure.SpringBootApplication; 9 | import org.springframework.context.annotation.Bean; 10 | 11 | import java.math.BigDecimal; 12 | import java.util.Arrays; 13 | 14 | @SpringBootApplication 15 | public class DemoMybatisApplication { 16 | 17 | public static void main(String[] args) { 18 | SpringApplication.run(DemoMybatisApplication.class, args); 19 | } 20 | 21 | @Bean 22 | CommandLineRunner runner(PizzaRepository pizzaRepository) { 23 | return a -> { 24 | Pizza pizza = new Pizza(); 25 | pizza.setBase(new Base(1L)); 26 | pizza.setToppings(Arrays.asList(new Topping(1L), new Topping(2L), new Topping(3L), new Topping(4L), new Topping(5L))); 27 | pizza.setName("Hello Pizza"); 28 | pizza.setPrice(new BigDecimal("1000")); 29 | pizzaRepository.save(pizza); 30 | }; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /demo-mybatis/src/main/java/com/example/PizzaController.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import com.example.model.Pizza; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | import org.springframework.web.bind.annotation.PostMapping; 6 | import org.springframework.web.bind.annotation.RequestBody; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | import java.util.List; 10 | 11 | @RestController 12 | public class PizzaController { 13 | private final PizzaRepository pizzaRepository; 14 | 15 | public PizzaController(PizzaRepository pizzaRepository) { 16 | this.pizzaRepository = pizzaRepository; 17 | } 18 | 19 | @GetMapping 20 | List list() { 21 | return pizzaRepository.findOrderByIdAsc(); 22 | } 23 | 24 | @PostMapping 25 | Pizza add(@RequestBody Pizza pizza) { 26 | return pizzaRepository.save(pizza); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /demo-mybatis/src/main/java/com/example/PizzaRepository.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import com.example.model.Base; 4 | import com.example.model.Pizza; 5 | import com.example.model.Topping; 6 | import com.example.mybatis.PizzaMapper; 7 | import com.example.mybatis.PizzaToppings; 8 | import org.springframework.stereotype.Repository; 9 | import org.springframework.transaction.annotation.Transactional; 10 | 11 | import java.util.List; 12 | import java.util.stream.Collectors; 13 | 14 | @Repository 15 | public class PizzaRepository { 16 | private final PizzaMapper pizzaMapper; 17 | 18 | public PizzaRepository(PizzaMapper pizzaMapper) { 19 | this.pizzaMapper = pizzaMapper; 20 | } 21 | 22 | public List findOrderByIdAsc() { 23 | return pizzaMapper.findOrderByIdAsc().stream() 24 | .collect(Collectors.groupingBy(com.example.mybatis.Pizza::getId)) 25 | .entrySet() 26 | .stream() 27 | .map(e -> { 28 | com.example.mybatis.Pizza dto = e.getValue().get(0); 29 | Pizza pizza = new Pizza(); 30 | pizza.setId(dto.getId()); 31 | pizza.setName(dto.getName()); 32 | pizza.setPrice(dto.getPrice()); 33 | Base base = new Base(dto.getId()); 34 | base.setName(dto.getBaseName()); 35 | pizza.setBase(base); 36 | pizza.setToppings(e.getValue().stream() 37 | .map(x -> { 38 | Topping topping = new Topping(x.getToppingId()); 39 | topping.setName(x.getToppingName()); 40 | return topping; 41 | }) 42 | .collect(Collectors.toList())); 43 | return pizza; 44 | }) 45 | .collect(Collectors.toList()); 46 | } 47 | 48 | @Transactional 49 | public Pizza save(Pizza pizza) { 50 | com.example.mybatis.Pizza dto = new com.example.mybatis.Pizza(); 51 | dto.setName(pizza.getName()); 52 | dto.setPrice(pizza.getPrice()); 53 | dto.setBaseId(pizza.getBase().getId()); 54 | pizzaMapper.insert(dto); 55 | pizzaMapper.flush(); // generate key here in BATCH mode 56 | pizza.getToppings().forEach(t -> pizzaMapper.insertToppings(new PizzaToppings(dto.getId(), t.getId()))); 57 | return pizza; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /demo-mybatis/src/main/java/com/example/model/Base.java: -------------------------------------------------------------------------------- 1 | package com.example.model; 2 | 3 | import java.io.Serializable; 4 | 5 | public class Base implements Serializable { 6 | private Long id; 7 | private String name; 8 | 9 | public Base(Long id) { 10 | this.id = id; 11 | } 12 | 13 | Base() { 14 | 15 | } 16 | 17 | public Long getId() { 18 | return id; 19 | } 20 | 21 | public void setId(Long id) { 22 | this.id = id; 23 | } 24 | 25 | public String getName() { 26 | return name; 27 | } 28 | 29 | public void setName(String name) { 30 | this.name = name; 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return "Base{" + 36 | "id=" + id + 37 | ", name='" + name + '\'' + 38 | '}'; 39 | } 40 | 41 | @Override 42 | public boolean equals(Object o) { 43 | if (this == o) return true; 44 | if (!(o instanceof Base)) return false; 45 | 46 | Base base = (Base) o; 47 | 48 | if (id != null ? !id.equals(base.id) : base.id != null) return false; 49 | return name != null ? name.equals(base.name) : base.name == null; 50 | 51 | } 52 | 53 | @Override 54 | public int hashCode() { 55 | int result = id != null ? id.hashCode() : 0; 56 | result = 31 * result + (name != null ? name.hashCode() : 0); 57 | return result; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /demo-mybatis/src/main/java/com/example/model/Topping.java: -------------------------------------------------------------------------------- 1 | package com.example.model; 2 | 3 | import java.io.Serializable; 4 | 5 | public class Topping implements Serializable { 6 | private Long id; 7 | private String name; 8 | 9 | public Topping(Long id) { 10 | this.id = id; 11 | } 12 | 13 | Topping() { 14 | 15 | } 16 | 17 | public Long getId() { 18 | return id; 19 | } 20 | 21 | public void setId(Long id) { 22 | this.id = id; 23 | } 24 | 25 | public String getName() { 26 | return name; 27 | } 28 | 29 | public void setName(String name) { 30 | this.name = name; 31 | } 32 | 33 | 34 | @Override 35 | public String toString() { 36 | return "Topping{" + 37 | "id=" + id + 38 | ", name='" + name + '\'' + 39 | '}'; 40 | } 41 | 42 | @Override 43 | public boolean equals(Object o) { 44 | if (this == o) return true; 45 | if (o == null || getClass() != o.getClass()) return false; 46 | 47 | Topping topping = (Topping) o; 48 | 49 | if (id != null ? !id.equals(topping.id) : topping.id != null) return false; 50 | return name != null ? name.equals(topping.name) : topping.name == null; 51 | 52 | } 53 | 54 | @Override 55 | public int hashCode() { 56 | int result = id != null ? id.hashCode() : 0; 57 | result = 31 * result + (name != null ? name.hashCode() : 0); 58 | return result; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /demo-mybatis/src/main/java/com/example/mybatis/Pizza.java: -------------------------------------------------------------------------------- 1 | package com.example.mybatis; 2 | 3 | import java.math.BigDecimal; 4 | 5 | public class Pizza { 6 | private Long id; 7 | private String name; 8 | private BigDecimal price; 9 | private Long baseId; 10 | private String baseName; 11 | private Long toppingId; 12 | private String toppingName; 13 | 14 | public Long getId() { 15 | return id; 16 | } 17 | 18 | public void setId(Long id) { 19 | this.id = id; 20 | } 21 | 22 | public String getName() { 23 | return name; 24 | } 25 | 26 | public void setName(String name) { 27 | this.name = name; 28 | } 29 | 30 | public BigDecimal getPrice() { 31 | return price; 32 | } 33 | 34 | public void setPrice(BigDecimal price) { 35 | this.price = price; 36 | } 37 | 38 | public Long getBaseId() { 39 | return baseId; 40 | } 41 | 42 | public void setBaseId(Long baseId) { 43 | this.baseId = baseId; 44 | } 45 | 46 | public String getBaseName() { 47 | return baseName; 48 | } 49 | 50 | public void setBaseName(String baseName) { 51 | this.baseName = baseName; 52 | } 53 | 54 | public Long getToppingId() { 55 | return toppingId; 56 | } 57 | 58 | public void setToppingId(Long toppingId) { 59 | this.toppingId = toppingId; 60 | } 61 | 62 | public String getToppingName() { 63 | return toppingName; 64 | } 65 | 66 | public void setToppingName(String toppingName) { 67 | this.toppingName = toppingName; 68 | } 69 | 70 | @Override 71 | public String toString() { 72 | return "Pizza{" + 73 | "id=" + id + 74 | ", name='" + name + '\'' + 75 | ", price=" + price + 76 | ", baseId=" + baseId + 77 | ", baseName='" + baseName + '\'' + 78 | ", toppingId=" + toppingId + 79 | ", toppingName='" + toppingName + '\'' + 80 | '}'; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /demo-mybatis/src/main/java/com/example/mybatis/PizzaMapper.java: -------------------------------------------------------------------------------- 1 | package com.example.mybatis; 2 | 3 | import org.apache.ibatis.annotations.*; 4 | 5 | import java.util.List; 6 | 7 | @Mapper 8 | public interface PizzaMapper { 9 | @Insert("INSERT INTO pizza (base_id, name, price) VALUES (#{baseId}, #{name}, #{price})") 10 | @Options(useGeneratedKeys = true) 11 | void insert(Pizza pizza); 12 | 13 | @Insert("INSERT INTO pizza_toppings (pizza_id, toppings_id) VALUES (#{pizzaId}, #{toppingsId})") 14 | int insertToppings(PizzaToppings pizzaToppings); 15 | 16 | @Select("SELECT p.id, p.name, p.price, b.id AS baseId, b.name AS baseName, t.id AS toppingId, t.name AS toppingName FROM pizza p INNER JOIN base b ON (b.id = p.base_id) INNER JOIN pizza_toppings pt ON (pt.pizza_id = p.id) INNER JOIN topping t ON (t.id = pt.toppings_id) ORDER BY p.id ASC") 17 | List findOrderByIdAsc(); 18 | 19 | @Flush 20 | void flush(); 21 | } 22 | -------------------------------------------------------------------------------- /demo-mybatis/src/main/java/com/example/mybatis/PizzaToppings.java: -------------------------------------------------------------------------------- 1 | package com.example.mybatis; 2 | 3 | public class PizzaToppings { 4 | private Long pizzaId; 5 | private Long toppingsId; 6 | 7 | public PizzaToppings(Long pizzaId, Long toppingsId) { 8 | this.pizzaId = pizzaId; 9 | this.toppingsId = toppingsId; 10 | } 11 | 12 | public PizzaToppings() { 13 | 14 | } 15 | 16 | public Long getPizzaId() { 17 | return pizzaId; 18 | } 19 | 20 | public void setPizzaId(Long pizzaId) { 21 | this.pizzaId = pizzaId; 22 | } 23 | 24 | public Long getToppingsId() { 25 | return toppingsId; 26 | } 27 | 28 | public void setToppingsId(Long toppingsId) { 29 | this.toppingsId = toppingsId; 30 | } 31 | 32 | @Override 33 | public String toString() { 34 | return "PizzaToppings{" + 35 | "pizzaId=" + pizzaId + 36 | ", toppingsId=" + toppingsId + 37 | '}'; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /demo-mybatis/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | mybatis.executor-type=batch 2 | mybatis.configuration.map-underscore-to-camel-case=true 3 | logging.level.com.example.mybatis=DEBUG 4 | logging.level.org.springframework.jdbc.datasource.DataSourceTransactionManager=DEBUG -------------------------------------------------------------------------------- /demo-mybatis/src/main/resources/data.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO topping (name) VALUES ('Veggies'); 2 | INSERT INTO topping (name) VALUES ('Roma Tomatoes'); 3 | INSERT INTO topping (name) VALUES ('Red Onions'); 4 | INSERT INTO topping (name) VALUES ('Spinach'); 5 | INSERT INTO topping (name) VALUES ('Broccoli'); 6 | INSERT INTO topping (name) VALUES ('Pineapple'); 7 | INSERT INTO topping (name) VALUES ('Jalapeno Peppers'); 8 | INSERT INTO topping (name) VALUES ('Kalamata Olives'); 9 | INSERT INTO topping (name) VALUES ('Fresh Mushrooms'); 10 | INSERT INTO topping (name) VALUES ('Meats'); 11 | INSERT INTO topping (name) VALUES ('Anchovies'); 12 | INSERT INTO topping (name) VALUES ('Bacon Crumble'); 13 | INSERT INTO topping (name) VALUES ('Bacon Strips'); 14 | INSERT INTO topping (name) VALUES ('Chicken'); 15 | INSERT INTO topping (name) VALUES ('Ground Beef'); 16 | INSERT INTO topping (name) VALUES ('Italian Ham'); 17 | INSERT INTO topping (name) VALUES ('Spicy Italian Sausage'); 18 | INSERT INTO topping (name) VALUES ('Steak Strips'); 19 | INSERT INTO topping (name) VALUES ('Cheese'); 20 | INSERT INTO topping (name) VALUES ('Feta Cheese'); 21 | INSERT INTO topping (name) VALUES ('Parmesan Cheese'); 22 | INSERT INTO topping (name) VALUES ('Provolone Cheese'); 23 | INSERT INTO topping (name) VALUES ('Extra Cheese'); 24 | INSERT INTO topping (name) VALUES ('Four Cheese Blend Topping'); 25 | INSERT INTO topping (name) VALUES ('Goat Cheese'); 26 | INSERT INTO topping (name) VALUES ('Mozzarella'); 27 | 28 | INSERT INTO base(name) VALUES ('Thin Crust'); 29 | INSERT INTO base(name) VALUES ('Flatbread Crust'); 30 | INSERT INTO base(name) VALUES ('Thick Crust'); 31 | INSERT INTO base(name) VALUES ('Focaccia'); -------------------------------------------------------------------------------- /demo-mybatis/src/main/resources/schema.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE base IF EXISTS; 2 | DROP TABLE pizza IF EXISTS; 3 | DROP TABLE pizza_toppings IF EXISTS; 4 | DROP TABLE topping IF EXISTS; 5 | 6 | CREATE TABLE base ( 7 | id BIGINT generated BY DEFAULT AS IDENTITY, 8 | name VARCHAR(255), 9 | PRIMARY KEY (id) 10 | ); 11 | CREATE TABLE pizza ( 12 | id BIGINT generated BY DEFAULT AS IDENTITY, 13 | name VARCHAR(255), 14 | price DECIMAL(19, 2), 15 | base_id BIGINT, 16 | PRIMARY KEY (id) 17 | ); 18 | CREATE TABLE pizza_toppings ( 19 | pizza_id BIGINT NOT NULL, 20 | toppings_id BIGINT NOT NULL, 21 | PRIMARY KEY (pizza_id, toppings_id) 22 | ); 23 | CREATE TABLE topping ( 24 | id BIGINT generated BY DEFAULT AS IDENTITY, 25 | name VARCHAR(255), 26 | PRIMARY KEY (id) 27 | ); 28 | 29 | ALTER TABLE pizza 30 | ADD CONSTRAINT FKm6kai6c5evr38duyfh06ue29u FOREIGN KEY (base_id) REFERENCES base; 31 | ALTER TABLE pizza_toppings 32 | ADD CONSTRAINT FK7tcti2cxau3csvbuwxn0scb93 FOREIGN KEY (toppings_id) REFERENCES topping; 33 | ALTER TABLE pizza_toppings 34 | ADD CONSTRAINT FK78l5qtferc9qhfkkgyv99lmst FOREIGN KEY (pizza_id) REFERENCES pizza; -------------------------------------------------------------------------------- /demo-mybatis/src/test/java/com/example/PizzaControllerTests.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import com.example.model.Base; 4 | import com.example.model.Pizza; 5 | import com.example.model.Topping; 6 | import com.fasterxml.jackson.databind.ObjectMapper; 7 | import org.junit.Before; 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.boot.test.context.SpringBootTest; 12 | import org.springframework.boot.test.json.JacksonTester; 13 | import org.springframework.boot.test.web.client.TestRestTemplate; 14 | import org.springframework.test.context.junit4.SpringRunner; 15 | 16 | import java.math.BigDecimal; 17 | import java.util.Arrays; 18 | import java.util.List; 19 | 20 | import static org.assertj.core.api.Assertions.assertThat; 21 | 22 | @RunWith(SpringRunner.class) 23 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 24 | public class PizzaControllerTests { 25 | 26 | @Autowired 27 | TestRestTemplate restTemplate; 28 | @Autowired 29 | ObjectMapper objectMapper; 30 | 31 | private JacksonTester> json; 32 | 33 | @Before 34 | public void setup() { 35 | JacksonTester.initFields(this, objectMapper); 36 | } 37 | 38 | @Test 39 | public void contextLoads() throws Exception { 40 | Pizza pizza = new Pizza(); 41 | pizza.setBase(new Base(2L)); 42 | pizza.setToppings(Arrays.asList(new Topping(3L), new Topping(10L))); 43 | pizza.setName("Spring Pizza"); 44 | pizza.setPrice(new BigDecimal("1200")); 45 | restTemplate.postForObject("/", pizza, Void.class); 46 | List ret = Arrays.asList(restTemplate.getForObject("/", Pizza[].class)); 47 | assertThat(json.write(ret)).isEqualToJson("expected.json"); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /demo-mybatis/src/test/resources/com/example/expected.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "id": 1, 4 | "name": "Hello Pizza", 5 | "price": 1000.00, 6 | "toppings": [ 7 | { 8 | "id": 1, 9 | "name": "Veggies" 10 | }, 11 | { 12 | "id": 2, 13 | "name": "Roma Tomatoes" 14 | }, 15 | { 16 | "id": 3, 17 | "name": "Red Onions" 18 | }, 19 | { 20 | "id": 4, 21 | "name": "Spinach" 22 | }, 23 | { 24 | "id": 5, 25 | "name": "Broccoli" 26 | } 27 | ], 28 | "base": { 29 | "id": 1, 30 | "name": "Thin Crust" 31 | } 32 | }, 33 | { 34 | "id": 2, 35 | "name": "Spring Pizza", 36 | "price": 1200.00, 37 | "toppings": [ 38 | { 39 | "id": 3, 40 | "name": "Red Onions" 41 | }, 42 | { 43 | "id": 10, 44 | "name": "Meats" 45 | } 46 | ], 47 | "base": { 48 | "id": 2, 49 | "name": "Flatbread Crust" 50 | } 51 | } 52 | ] 53 | -------------------------------------------------------------------------------- /demo-querydsl/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/making/spring-boot-db-samples/c2d01559bbd8843f4e239b47734c08fe481afdee/demo-querydsl/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /demo-querydsl/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.3.9/apache-maven-3.3.9-bin.zip 2 | -------------------------------------------------------------------------------- /demo-querydsl/README.md: -------------------------------------------------------------------------------- 1 | Get all pizza 2 | 3 | ``` console 4 | curl localhost:8080 5 | ``` 6 | 7 | Add a pizza 8 | 9 | ``` console 10 | curl -d '{"name" : "Spring Pizza", "price" : 1200, "toppings" : [{"id" : 3}, {"id" : 10}], "base" : {"id" : "2"}}' -H 'Content-Type: application/json' localhost:8080 11 | ``` 12 | 13 | ### (Optional) Re-generate Sources 14 | 15 | After start and stop the application 16 | 17 | ``` console 18 | rm -rf src/generated 19 | mvn clean compile 20 | ``` -------------------------------------------------------------------------------- /demo-querydsl/src/generated/java/com/example/querydsl/Base.java: -------------------------------------------------------------------------------- 1 | package com.example.querydsl; 2 | 3 | import javax.annotation.Generated; 4 | import java.io.Serializable; 5 | 6 | /** 7 | * Base is a Querydsl bean type 8 | */ 9 | @Generated("com.querydsl.codegen.BeanSerializer") 10 | public class Base implements Serializable { 11 | 12 | private Long id; 13 | 14 | private String name; 15 | 16 | public Long getId() { 17 | return id; 18 | } 19 | 20 | public void setId(Long id) { 21 | this.id = id; 22 | } 23 | 24 | public String getName() { 25 | return name; 26 | } 27 | 28 | public void setName(String name) { 29 | this.name = name; 30 | } 31 | 32 | @Override 33 | public String toString() { 34 | return "id = " + id + ", name = " + name; 35 | } 36 | 37 | } 38 | 39 | -------------------------------------------------------------------------------- /demo-querydsl/src/generated/java/com/example/querydsl/Pizza.java: -------------------------------------------------------------------------------- 1 | package com.example.querydsl; 2 | 3 | import javax.annotation.Generated; 4 | import java.io.Serializable; 5 | 6 | /** 7 | * Pizza is a Querydsl bean type 8 | */ 9 | @Generated("com.querydsl.codegen.BeanSerializer") 10 | public class Pizza implements Serializable { 11 | 12 | private Long baseId; 13 | 14 | private Long id; 15 | 16 | private String name; 17 | 18 | private java.math.BigDecimal price; 19 | 20 | public Long getBaseId() { 21 | return baseId; 22 | } 23 | 24 | public void setBaseId(Long baseId) { 25 | this.baseId = baseId; 26 | } 27 | 28 | public Long getId() { 29 | return id; 30 | } 31 | 32 | public void setId(Long id) { 33 | this.id = id; 34 | } 35 | 36 | public String getName() { 37 | return name; 38 | } 39 | 40 | public void setName(String name) { 41 | this.name = name; 42 | } 43 | 44 | public java.math.BigDecimal getPrice() { 45 | return price; 46 | } 47 | 48 | public void setPrice(java.math.BigDecimal price) { 49 | this.price = price; 50 | } 51 | 52 | @Override 53 | public String toString() { 54 | return "baseId = " + baseId + ", id = " + id + ", name = " + name + ", price = " + price; 55 | } 56 | 57 | } 58 | 59 | -------------------------------------------------------------------------------- /demo-querydsl/src/generated/java/com/example/querydsl/PizzaToppings.java: -------------------------------------------------------------------------------- 1 | package com.example.querydsl; 2 | 3 | import javax.annotation.Generated; 4 | import java.io.Serializable; 5 | 6 | /** 7 | * PizzaToppings is a Querydsl bean type 8 | */ 9 | @Generated("com.querydsl.codegen.BeanSerializer") 10 | public class PizzaToppings implements Serializable { 11 | 12 | private Long pizzaId; 13 | 14 | private Long toppingsId; 15 | 16 | public Long getPizzaId() { 17 | return pizzaId; 18 | } 19 | 20 | public void setPizzaId(Long pizzaId) { 21 | this.pizzaId = pizzaId; 22 | } 23 | 24 | public Long getToppingsId() { 25 | return toppingsId; 26 | } 27 | 28 | public void setToppingsId(Long toppingsId) { 29 | this.toppingsId = toppingsId; 30 | } 31 | 32 | @Override 33 | public String toString() { 34 | return "pizzaId = " + pizzaId + ", toppingsId = " + toppingsId; 35 | } 36 | 37 | } 38 | 39 | -------------------------------------------------------------------------------- /demo-querydsl/src/generated/java/com/example/querydsl/QBase.java: -------------------------------------------------------------------------------- 1 | package com.example.querydsl; 2 | 3 | import static com.querydsl.core.types.PathMetadataFactory.*; 4 | 5 | import com.querydsl.core.types.dsl.*; 6 | 7 | import com.querydsl.core.types.PathMetadata; 8 | import javax.annotation.Generated; 9 | import com.querydsl.core.types.Path; 10 | 11 | import com.querydsl.sql.ColumnMetadata; 12 | import java.sql.Types; 13 | 14 | 15 | 16 | 17 | /** 18 | * QBase is a Querydsl query type for Base 19 | */ 20 | @Generated("com.querydsl.sql.codegen.MetaDataSerializer") 21 | public class QBase extends com.querydsl.sql.RelationalPathBase { 22 | 23 | private static final long serialVersionUID = 1229658905; 24 | 25 | public static final QBase base = new QBase("BASE"); 26 | 27 | public final NumberPath id = createNumber("id", Long.class); 28 | 29 | public final StringPath name = createString("name"); 30 | 31 | public final com.querydsl.sql.PrimaryKey constraint1 = createPrimaryKey(id); 32 | 33 | public final com.querydsl.sql.ForeignKey _fkm6kai6c5evr38duyfh06ue29u = createInvForeignKey(id, "BASE_ID"); 34 | 35 | public QBase(String variable) { 36 | super(Base.class, forVariable(variable), "PUBLIC", "BASE"); 37 | addMetadata(); 38 | } 39 | 40 | public QBase(String variable, String schema, String table) { 41 | super(Base.class, forVariable(variable), schema, table); 42 | addMetadata(); 43 | } 44 | 45 | public QBase(Path path) { 46 | super(path.getType(), path.getMetadata(), "PUBLIC", "BASE"); 47 | addMetadata(); 48 | } 49 | 50 | public QBase(PathMetadata metadata) { 51 | super(Base.class, metadata, "PUBLIC", "BASE"); 52 | addMetadata(); 53 | } 54 | 55 | public void addMetadata() { 56 | addMetadata(id, ColumnMetadata.named("ID").withIndex(1).ofType(Types.BIGINT).withSize(19).notNull()); 57 | addMetadata(name, ColumnMetadata.named("NAME").withIndex(2).ofType(Types.VARCHAR).withSize(255)); 58 | } 59 | 60 | } 61 | 62 | -------------------------------------------------------------------------------- /demo-querydsl/src/generated/java/com/example/querydsl/QPizza.java: -------------------------------------------------------------------------------- 1 | package com.example.querydsl; 2 | 3 | import static com.querydsl.core.types.PathMetadataFactory.*; 4 | 5 | import com.querydsl.core.types.dsl.*; 6 | 7 | import com.querydsl.core.types.PathMetadata; 8 | import javax.annotation.Generated; 9 | import com.querydsl.core.types.Path; 10 | 11 | import com.querydsl.sql.ColumnMetadata; 12 | import java.sql.Types; 13 | 14 | 15 | 16 | 17 | /** 18 | * QPizza is a Querydsl query type for Pizza 19 | */ 20 | @Generated("com.querydsl.sql.codegen.MetaDataSerializer") 21 | public class QPizza extends com.querydsl.sql.RelationalPathBase { 22 | 23 | private static final long serialVersionUID = -522104512; 24 | 25 | public static final QPizza pizza = new QPizza("PIZZA"); 26 | 27 | public final NumberPath baseId = createNumber("baseId", Long.class); 28 | 29 | public final NumberPath id = createNumber("id", Long.class); 30 | 31 | public final StringPath name = createString("name"); 32 | 33 | public final NumberPath price = createNumber("price", java.math.BigDecimal.class); 34 | 35 | public final com.querydsl.sql.PrimaryKey constraint4 = createPrimaryKey(id); 36 | 37 | public final com.querydsl.sql.ForeignKey fkm6kai6c5evr38duyfh06ue29u = createForeignKey(baseId, "ID"); 38 | 39 | public final com.querydsl.sql.ForeignKey _fk78l5qtferc9qhfkkgyv99lmst = createInvForeignKey(id, "PIZZA_ID"); 40 | 41 | public QPizza(String variable) { 42 | super(Pizza.class, forVariable(variable), "PUBLIC", "PIZZA"); 43 | addMetadata(); 44 | } 45 | 46 | public QPizza(String variable, String schema, String table) { 47 | super(Pizza.class, forVariable(variable), schema, table); 48 | addMetadata(); 49 | } 50 | 51 | public QPizza(Path path) { 52 | super(path.getType(), path.getMetadata(), "PUBLIC", "PIZZA"); 53 | addMetadata(); 54 | } 55 | 56 | public QPizza(PathMetadata metadata) { 57 | super(Pizza.class, metadata, "PUBLIC", "PIZZA"); 58 | addMetadata(); 59 | } 60 | 61 | public void addMetadata() { 62 | addMetadata(baseId, ColumnMetadata.named("BASE_ID").withIndex(4).ofType(Types.BIGINT).withSize(19)); 63 | addMetadata(id, ColumnMetadata.named("ID").withIndex(1).ofType(Types.BIGINT).withSize(19).notNull()); 64 | addMetadata(name, ColumnMetadata.named("NAME").withIndex(2).ofType(Types.VARCHAR).withSize(255)); 65 | addMetadata(price, ColumnMetadata.named("PRICE").withIndex(3).ofType(Types.DECIMAL).withSize(19).withDigits(2)); 66 | } 67 | 68 | } 69 | 70 | -------------------------------------------------------------------------------- /demo-querydsl/src/generated/java/com/example/querydsl/QPizzaToppings.java: -------------------------------------------------------------------------------- 1 | package com.example.querydsl; 2 | 3 | import static com.querydsl.core.types.PathMetadataFactory.*; 4 | 5 | import com.querydsl.core.types.dsl.*; 6 | 7 | import com.querydsl.core.types.PathMetadata; 8 | import javax.annotation.Generated; 9 | import com.querydsl.core.types.Path; 10 | 11 | import com.querydsl.sql.ColumnMetadata; 12 | import java.sql.Types; 13 | 14 | 15 | 16 | 17 | /** 18 | * QPizzaToppings is a Querydsl query type for PizzaToppings 19 | */ 20 | @Generated("com.querydsl.sql.codegen.MetaDataSerializer") 21 | public class QPizzaToppings extends com.querydsl.sql.RelationalPathBase { 22 | 23 | private static final long serialVersionUID = -213062036; 24 | 25 | public static final QPizzaToppings pizzaToppings = new QPizzaToppings("PIZZA_TOPPINGS"); 26 | 27 | public final NumberPath pizzaId = createNumber("pizzaId", Long.class); 28 | 29 | public final NumberPath toppingsId = createNumber("toppingsId", Long.class); 30 | 31 | public final com.querydsl.sql.PrimaryKey constraintA = createPrimaryKey(pizzaId, toppingsId); 32 | 33 | public final com.querydsl.sql.ForeignKey fk7tcti2cxau3csvbuwxn0scb93 = createForeignKey(toppingsId, "ID"); 34 | 35 | public final com.querydsl.sql.ForeignKey fk78l5qtferc9qhfkkgyv99lmst = createForeignKey(pizzaId, "ID"); 36 | 37 | public QPizzaToppings(String variable) { 38 | super(PizzaToppings.class, forVariable(variable), "PUBLIC", "PIZZA_TOPPINGS"); 39 | addMetadata(); 40 | } 41 | 42 | public QPizzaToppings(String variable, String schema, String table) { 43 | super(PizzaToppings.class, forVariable(variable), schema, table); 44 | addMetadata(); 45 | } 46 | 47 | public QPizzaToppings(Path path) { 48 | super(path.getType(), path.getMetadata(), "PUBLIC", "PIZZA_TOPPINGS"); 49 | addMetadata(); 50 | } 51 | 52 | public QPizzaToppings(PathMetadata metadata) { 53 | super(PizzaToppings.class, metadata, "PUBLIC", "PIZZA_TOPPINGS"); 54 | addMetadata(); 55 | } 56 | 57 | public void addMetadata() { 58 | addMetadata(pizzaId, ColumnMetadata.named("PIZZA_ID").withIndex(1).ofType(Types.BIGINT).withSize(19).notNull()); 59 | addMetadata(toppingsId, ColumnMetadata.named("TOPPINGS_ID").withIndex(2).ofType(Types.BIGINT).withSize(19).notNull()); 60 | } 61 | 62 | } 63 | 64 | -------------------------------------------------------------------------------- /demo-querydsl/src/generated/java/com/example/querydsl/QTopping.java: -------------------------------------------------------------------------------- 1 | package com.example.querydsl; 2 | 3 | import static com.querydsl.core.types.PathMetadataFactory.*; 4 | 5 | import com.querydsl.core.types.dsl.*; 6 | 7 | import com.querydsl.core.types.PathMetadata; 8 | import javax.annotation.Generated; 9 | import com.querydsl.core.types.Path; 10 | 11 | import com.querydsl.sql.ColumnMetadata; 12 | import java.sql.Types; 13 | 14 | 15 | 16 | 17 | /** 18 | * QTopping is a Querydsl query type for Topping 19 | */ 20 | @Generated("com.querydsl.sql.codegen.MetaDataSerializer") 21 | public class QTopping extends com.querydsl.sql.RelationalPathBase { 22 | 23 | private static final long serialVersionUID = 186038015; 24 | 25 | public static final QTopping topping = new QTopping("TOPPING"); 26 | 27 | public final NumberPath id = createNumber("id", Long.class); 28 | 29 | public final StringPath name = createString("name"); 30 | 31 | public final com.querydsl.sql.PrimaryKey constraintE = createPrimaryKey(id); 32 | 33 | public final com.querydsl.sql.ForeignKey _fk7tcti2cxau3csvbuwxn0scb93 = createInvForeignKey(id, "TOPPINGS_ID"); 34 | 35 | public QTopping(String variable) { 36 | super(Topping.class, forVariable(variable), "PUBLIC", "TOPPING"); 37 | addMetadata(); 38 | } 39 | 40 | public QTopping(String variable, String schema, String table) { 41 | super(Topping.class, forVariable(variable), schema, table); 42 | addMetadata(); 43 | } 44 | 45 | public QTopping(Path path) { 46 | super(path.getType(), path.getMetadata(), "PUBLIC", "TOPPING"); 47 | addMetadata(); 48 | } 49 | 50 | public QTopping(PathMetadata metadata) { 51 | super(Topping.class, metadata, "PUBLIC", "TOPPING"); 52 | addMetadata(); 53 | } 54 | 55 | public void addMetadata() { 56 | addMetadata(id, ColumnMetadata.named("ID").withIndex(1).ofType(Types.BIGINT).withSize(19).notNull()); 57 | addMetadata(name, ColumnMetadata.named("NAME").withIndex(2).ofType(Types.VARCHAR).withSize(255)); 58 | } 59 | 60 | } 61 | 62 | -------------------------------------------------------------------------------- /demo-querydsl/src/generated/java/com/example/querydsl/Topping.java: -------------------------------------------------------------------------------- 1 | package com.example.querydsl; 2 | 3 | import javax.annotation.Generated; 4 | import java.io.Serializable; 5 | 6 | /** 7 | * Topping is a Querydsl bean type 8 | */ 9 | @Generated("com.querydsl.codegen.BeanSerializer") 10 | public class Topping implements Serializable { 11 | 12 | private Long id; 13 | 14 | private String name; 15 | 16 | public Long getId() { 17 | return id; 18 | } 19 | 20 | public void setId(Long id) { 21 | this.id = id; 22 | } 23 | 24 | public String getName() { 25 | return name; 26 | } 27 | 28 | public void setName(String name) { 29 | this.name = name; 30 | } 31 | 32 | @Override 33 | public String toString() { 34 | return "id = " + id + ", name = " + name; 35 | } 36 | 37 | } 38 | 39 | -------------------------------------------------------------------------------- /demo-querydsl/src/main/java/com/example/DemoQueryDslApplication.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import com.example.model.Base; 4 | import com.example.model.Pizza; 5 | import com.example.model.Topping; 6 | import com.querydsl.sql.H2Templates; 7 | import com.querydsl.sql.SQLQueryFactory; 8 | import com.querydsl.sql.SQLTemplates; 9 | import com.querydsl.sql.spring.SpringExceptionTranslator; 10 | import org.springframework.boot.CommandLineRunner; 11 | import org.springframework.boot.SpringApplication; 12 | import org.springframework.boot.autoconfigure.SpringBootApplication; 13 | import org.springframework.context.annotation.Bean; 14 | import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy; 15 | 16 | import javax.sql.DataSource; 17 | import java.math.BigDecimal; 18 | import java.util.Arrays; 19 | 20 | @SpringBootApplication 21 | public class DemoQueryDslApplication { 22 | 23 | public static void main(String[] args) { 24 | SpringApplication.run(DemoQueryDslApplication.class, args); 25 | } 26 | 27 | @Bean 28 | CommandLineRunner runner(PizzaRepository pizzaRepository) { 29 | return a -> { 30 | Pizza pizza = new Pizza(); 31 | pizza.setBase(new Base(1L)); 32 | pizza.setToppings(Arrays.asList(new Topping(1L), new Topping(2L), new Topping(3L), new Topping(4L), new Topping(5L))); 33 | pizza.setName("Hello Pizza"); 34 | pizza.setPrice(new BigDecimal("1000")); 35 | pizzaRepository.save(pizza); 36 | }; 37 | } 38 | 39 | @Bean 40 | com.querydsl.sql.Configuration querydslConfiguration() { 41 | SQLTemplates templates = H2Templates.builder().build(); 42 | com.querydsl.sql.Configuration configuration = new com.querydsl.sql.Configuration(templates); 43 | configuration.setExceptionTranslator(new SpringExceptionTranslator()); 44 | return configuration; 45 | } 46 | 47 | @Bean 48 | SQLQueryFactory sqlQueryFactory(DataSource dataSource) { 49 | return new SQLQueryFactory(querydslConfiguration(), new TransactionAwareDataSourceProxy(dataSource)); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /demo-querydsl/src/main/java/com/example/PizzaController.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import com.example.model.Pizza; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | import org.springframework.web.bind.annotation.PostMapping; 6 | import org.springframework.web.bind.annotation.RequestBody; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | import java.util.List; 10 | 11 | @RestController 12 | public class PizzaController { 13 | private final PizzaRepository pizzaRepository; 14 | 15 | public PizzaController(PizzaRepository pizzaRepository) { 16 | this.pizzaRepository = pizzaRepository; 17 | } 18 | 19 | @GetMapping 20 | List list() { 21 | return pizzaRepository.findOrderByIdAsc(); 22 | } 23 | 24 | @PostMapping 25 | Pizza add(@RequestBody Pizza pizza) { 26 | return pizzaRepository.save(pizza); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /demo-querydsl/src/main/java/com/example/model/Base.java: -------------------------------------------------------------------------------- 1 | package com.example.model; 2 | 3 | import java.io.Serializable; 4 | 5 | public class Base implements Serializable { 6 | private Long id; 7 | private String name; 8 | 9 | public Base(Long id) { 10 | this.id = id; 11 | } 12 | 13 | Base() { 14 | 15 | } 16 | 17 | public Long getId() { 18 | return id; 19 | } 20 | 21 | public void setId(Long id) { 22 | this.id = id; 23 | } 24 | 25 | public String getName() { 26 | return name; 27 | } 28 | 29 | public void setName(String name) { 30 | this.name = name; 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return "Base{" + 36 | "id=" + id + 37 | ", name='" + name + '\'' + 38 | '}'; 39 | } 40 | 41 | @Override 42 | public boolean equals(Object o) { 43 | if (this == o) return true; 44 | if (!(o instanceof Base)) return false; 45 | 46 | Base base = (Base) o; 47 | 48 | if (id != null ? !id.equals(base.id) : base.id != null) return false; 49 | return name != null ? name.equals(base.name) : base.name == null; 50 | 51 | } 52 | 53 | @Override 54 | public int hashCode() { 55 | int result = id != null ? id.hashCode() : 0; 56 | result = 31 * result + (name != null ? name.hashCode() : 0); 57 | return result; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /demo-querydsl/src/main/java/com/example/model/Topping.java: -------------------------------------------------------------------------------- 1 | package com.example.model; 2 | 3 | import java.io.Serializable; 4 | 5 | public class Topping implements Serializable { 6 | private Long id; 7 | private String name; 8 | 9 | public Topping(Long id) { 10 | this.id = id; 11 | } 12 | 13 | Topping() { 14 | 15 | } 16 | 17 | public Long getId() { 18 | return id; 19 | } 20 | 21 | public void setId(Long id) { 22 | this.id = id; 23 | } 24 | 25 | public String getName() { 26 | return name; 27 | } 28 | 29 | public void setName(String name) { 30 | this.name = name; 31 | } 32 | 33 | 34 | @Override 35 | public String toString() { 36 | return "Topping{" + 37 | "id=" + id + 38 | ", name='" + name + '\'' + 39 | '}'; 40 | } 41 | 42 | @Override 43 | public boolean equals(Object o) { 44 | if (this == o) return true; 45 | if (o == null || getClass() != o.getClass()) return false; 46 | 47 | Topping topping = (Topping) o; 48 | 49 | if (id != null ? !id.equals(topping.id) : topping.id != null) return false; 50 | return name != null ? name.equals(topping.name) : topping.name == null; 51 | 52 | } 53 | 54 | @Override 55 | public int hashCode() { 56 | int result = id != null ? id.hashCode() : 0; 57 | result = 31 * result + (name != null ? name.hashCode() : 0); 58 | return result; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /demo-querydsl/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:h2:file:/tmp/db/testdb 2 | logging.level.org.springframework.jdbc.datasource.DataSourceTransactionManager=DEBUG -------------------------------------------------------------------------------- /demo-querydsl/src/main/resources/data.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO topping (name) VALUES ('Veggies'); 2 | INSERT INTO topping (name) VALUES ('Roma Tomatoes'); 3 | INSERT INTO topping (name) VALUES ('Red Onions'); 4 | INSERT INTO topping (name) VALUES ('Spinach'); 5 | INSERT INTO topping (name) VALUES ('Broccoli'); 6 | INSERT INTO topping (name) VALUES ('Pineapple'); 7 | INSERT INTO topping (name) VALUES ('Jalapeno Peppers'); 8 | INSERT INTO topping (name) VALUES ('Kalamata Olives'); 9 | INSERT INTO topping (name) VALUES ('Fresh Mushrooms'); 10 | INSERT INTO topping (name) VALUES ('Meats'); 11 | INSERT INTO topping (name) VALUES ('Anchovies'); 12 | INSERT INTO topping (name) VALUES ('Bacon Crumble'); 13 | INSERT INTO topping (name) VALUES ('Bacon Strips'); 14 | INSERT INTO topping (name) VALUES ('Chicken'); 15 | INSERT INTO topping (name) VALUES ('Ground Beef'); 16 | INSERT INTO topping (name) VALUES ('Italian Ham'); 17 | INSERT INTO topping (name) VALUES ('Spicy Italian Sausage'); 18 | INSERT INTO topping (name) VALUES ('Steak Strips'); 19 | INSERT INTO topping (name) VALUES ('Cheese'); 20 | INSERT INTO topping (name) VALUES ('Feta Cheese'); 21 | INSERT INTO topping (name) VALUES ('Parmesan Cheese'); 22 | INSERT INTO topping (name) VALUES ('Provolone Cheese'); 23 | INSERT INTO topping (name) VALUES ('Extra Cheese'); 24 | INSERT INTO topping (name) VALUES ('Four Cheese Blend Topping'); 25 | INSERT INTO topping (name) VALUES ('Goat Cheese'); 26 | INSERT INTO topping (name) VALUES ('Mozzarella'); 27 | 28 | INSERT INTO base(name) VALUES ('Thin Crust'); 29 | INSERT INTO base(name) VALUES ('Flatbread Crust'); 30 | INSERT INTO base(name) VALUES ('Thick Crust'); 31 | INSERT INTO base(name) VALUES ('Focaccia'); -------------------------------------------------------------------------------- /demo-querydsl/src/main/resources/schema.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE base IF EXISTS; 2 | DROP TABLE pizza IF EXISTS; 3 | DROP TABLE pizza_toppings IF EXISTS; 4 | DROP TABLE topping IF EXISTS; 5 | 6 | CREATE TABLE base ( 7 | id BIGINT generated BY DEFAULT AS IDENTITY, 8 | name VARCHAR(255), 9 | PRIMARY KEY (id) 10 | ); 11 | CREATE TABLE pizza ( 12 | id BIGINT generated BY DEFAULT AS IDENTITY, 13 | name VARCHAR(255), 14 | price DECIMAL(19, 2), 15 | base_id BIGINT, 16 | PRIMARY KEY (id) 17 | ); 18 | CREATE TABLE pizza_toppings ( 19 | pizza_id BIGINT NOT NULL, 20 | toppings_id BIGINT NOT NULL, 21 | PRIMARY KEY (pizza_id, toppings_id) 22 | ); 23 | CREATE TABLE topping ( 24 | id BIGINT generated BY DEFAULT AS IDENTITY, 25 | name VARCHAR(255), 26 | PRIMARY KEY (id) 27 | ); 28 | 29 | ALTER TABLE pizza 30 | ADD CONSTRAINT FKm6kai6c5evr38duyfh06ue29u FOREIGN KEY (base_id) REFERENCES base; 31 | ALTER TABLE pizza_toppings 32 | ADD CONSTRAINT FK7tcti2cxau3csvbuwxn0scb93 FOREIGN KEY (toppings_id) REFERENCES topping; 33 | ALTER TABLE pizza_toppings 34 | ADD CONSTRAINT FK78l5qtferc9qhfkkgyv99lmst FOREIGN KEY (pizza_id) REFERENCES pizza; -------------------------------------------------------------------------------- /demo-querydsl/src/test/java/com/example/PizzaControllerTests.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import com.example.model.Base; 4 | import com.example.model.Pizza; 5 | import com.example.model.Topping; 6 | import com.fasterxml.jackson.databind.ObjectMapper; 7 | import org.junit.Before; 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.boot.test.context.SpringBootTest; 12 | import org.springframework.boot.test.json.JacksonTester; 13 | import org.springframework.boot.test.web.client.TestRestTemplate; 14 | import org.springframework.test.context.junit4.SpringRunner; 15 | 16 | import java.math.BigDecimal; 17 | import java.util.Arrays; 18 | import java.util.List; 19 | 20 | import static org.assertj.core.api.Assertions.assertThat; 21 | 22 | @RunWith(SpringRunner.class) 23 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 24 | public class PizzaControllerTests { 25 | 26 | @Autowired 27 | TestRestTemplate restTemplate; 28 | @Autowired 29 | ObjectMapper objectMapper; 30 | 31 | private JacksonTester> json; 32 | 33 | @Before 34 | public void setup() { 35 | JacksonTester.initFields(this, objectMapper); 36 | } 37 | 38 | @Test 39 | public void contextLoads() throws Exception { 40 | Pizza pizza = new Pizza(); 41 | pizza.setBase(new Base(2L)); 42 | pizza.setToppings(Arrays.asList(new Topping(3L), new Topping(10L))); 43 | pizza.setName("Spring Pizza"); 44 | pizza.setPrice(new BigDecimal("1200")); 45 | restTemplate.postForObject("/", pizza, Void.class); 46 | List ret = Arrays.asList(restTemplate.getForObject("/", Pizza[].class)); 47 | assertThat(json.write(ret)).isEqualToJson("expected.json"); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /demo-querydsl/src/test/resources/com/example/expected.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "id": 1, 4 | "name": "Hello Pizza", 5 | "price": 1000.00, 6 | "toppings": [ 7 | { 8 | "id": 1, 9 | "name": "Veggies" 10 | }, 11 | { 12 | "id": 2, 13 | "name": "Roma Tomatoes" 14 | }, 15 | { 16 | "id": 3, 17 | "name": "Red Onions" 18 | }, 19 | { 20 | "id": 4, 21 | "name": "Spinach" 22 | }, 23 | { 24 | "id": 5, 25 | "name": "Broccoli" 26 | } 27 | ], 28 | "base": { 29 | "id": 1, 30 | "name": "Thin Crust" 31 | } 32 | }, 33 | { 34 | "id": 2, 35 | "name": "Spring Pizza", 36 | "price": 1200.00, 37 | "toppings": [ 38 | { 39 | "id": 3, 40 | "name": "Red Onions" 41 | }, 42 | { 43 | "id": 10, 44 | "name": "Meats" 45 | } 46 | ], 47 | "base": { 48 | "id": 2, 49 | "name": "Flatbread Crust" 50 | } 51 | } 52 | ] 53 | -------------------------------------------------------------------------------- /demo-s2jdbc/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/making/spring-boot-db-samples/c2d01559bbd8843f4e239b47734c08fe481afdee/demo-s2jdbc/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /demo-s2jdbc/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.3.9/apache-maven-3.3.9-bin.zip 2 | -------------------------------------------------------------------------------- /demo-s2jdbc/README.md: -------------------------------------------------------------------------------- 1 | Get all pizza 2 | 3 | ``` console 4 | curl localhost:8080 5 | ``` 6 | 7 | Add a pizza 8 | 9 | ``` console 10 | curl -d '{"name" : "Spring Pizza", "price" : 1200, "toppings" : [{"id" : 3}, {"id" : 10}], "base" : {"id" : "2"}}' -H 'Content-Type: application/json' localhost:8080 11 | ``` -------------------------------------------------------------------------------- /demo-s2jdbc/src/main/java/com/example/PizzaController.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import com.example.model.Pizza; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | import org.springframework.web.bind.annotation.PostMapping; 6 | import org.springframework.web.bind.annotation.RequestBody; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | import java.util.List; 10 | 11 | @RestController 12 | public class PizzaController { 13 | private final PizzaRepository pizzaRepository; 14 | 15 | public PizzaController(PizzaRepository pizzaRepository) { 16 | this.pizzaRepository = pizzaRepository; 17 | } 18 | 19 | @GetMapping 20 | List list() { 21 | return pizzaRepository.findOrderByIdAsc(); 22 | } 23 | 24 | @PostMapping 25 | Pizza add(@RequestBody Pizza pizza) { 26 | return pizzaRepository.save(pizza); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /demo-s2jdbc/src/main/java/com/example/PizzaRepository.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import com.example.model.Pizza; 4 | import com.example.model.PizzaToppings; 5 | import com.example.model.Topping; 6 | import org.seasar.extension.jdbc.JdbcManager; 7 | import org.springframework.stereotype.Repository; 8 | import org.springframework.transaction.annotation.Transactional; 9 | 10 | import java.util.List; 11 | import java.util.stream.Collectors; 12 | 13 | @Repository 14 | public class PizzaRepository { 15 | private final JdbcManager jdbcManager; 16 | 17 | public PizzaRepository(JdbcManager jdbcManager) { 18 | this.jdbcManager = jdbcManager; 19 | } 20 | 21 | public List findOrderByIdAsc() { 22 | return jdbcManager.from(Pizza.class) 23 | .innerJoin("base") 24 | .innerJoin("pizzaToppings") 25 | .innerJoin("pizzaToppings.toppings") 26 | .getResultList() 27 | .stream() 28 | .map(p -> { 29 | p.setToppings(p.getPizzaToppings().stream() 30 | .map(pt -> { 31 | Topping t = new Topping(pt.getToppingsId()); 32 | t.setName(pt.getToppings().getName()); 33 | return t; 34 | }) 35 | .collect(Collectors.toList())); 36 | return p; 37 | }) 38 | .collect(Collectors.toList()); 39 | } 40 | 41 | @Transactional 42 | public Pizza save(Pizza pizza) { 43 | pizza.setBaseId(pizza.getBase().getId()); 44 | jdbcManager.insert(pizza).execute(); 45 | jdbcManager.insertBatch(pizza.getToppings().stream().map(t -> new PizzaToppings(pizza.getId(), t.getId())).collect(Collectors.toList())).execute(); 46 | return pizza; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /demo-s2jdbc/src/main/java/com/example/model/Base.java: -------------------------------------------------------------------------------- 1 | package com.example.model; 2 | 3 | import javax.persistence.*; 4 | import java.io.Serializable; 5 | 6 | @Entity 7 | public class Base implements Serializable { 8 | @Id 9 | @GeneratedValue(strategy = GenerationType.IDENTITY) 10 | private Long id; 11 | private String name; 12 | 13 | public Base(Long id) { 14 | this.id = id; 15 | } 16 | 17 | public Base() { 18 | 19 | } 20 | 21 | public Long getId() { 22 | return id; 23 | } 24 | 25 | public void setId(Long id) { 26 | this.id = id; 27 | } 28 | 29 | public String getName() { 30 | return name; 31 | } 32 | 33 | public void setName(String name) { 34 | this.name = name; 35 | } 36 | 37 | @Override 38 | public String toString() { 39 | return "Base{" + 40 | "id=" + id + 41 | ", name='" + name + '\'' + 42 | '}'; 43 | } 44 | 45 | @Override 46 | public boolean equals(Object o) { 47 | if (this == o) return true; 48 | if (!(o instanceof Base)) return false; 49 | 50 | Base base = (Base) o; 51 | 52 | if (id != null ? !id.equals(base.id) : base.id != null) return false; 53 | return name != null ? name.equals(base.name) : base.name == null; 54 | 55 | } 56 | 57 | @Override 58 | public int hashCode() { 59 | int result = id != null ? id.hashCode() : 0; 60 | result = 31 * result + (name != null ? name.hashCode() : 0); 61 | return result; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /demo-s2jdbc/src/main/java/com/example/model/PizzaToppings.java: -------------------------------------------------------------------------------- 1 | package com.example.model; 2 | 3 | import javax.persistence.Entity; 4 | import javax.persistence.Id; 5 | import javax.persistence.ManyToOne; 6 | 7 | @Entity 8 | public class PizzaToppings { 9 | @Id 10 | private Long pizzaId; 11 | @Id 12 | private Long toppingsId; 13 | 14 | @ManyToOne 15 | private Pizza pizza; 16 | @ManyToOne 17 | private Topping toppings; 18 | 19 | public PizzaToppings(Long pizzaId, Long toppingsId) { 20 | this.pizzaId = pizzaId; 21 | this.toppingsId = toppingsId; 22 | } 23 | 24 | public PizzaToppings() { 25 | 26 | } 27 | 28 | public Long getPizzaId() { 29 | return pizzaId; 30 | } 31 | 32 | public void setPizzaId(Long pizzaId) { 33 | this.pizzaId = pizzaId; 34 | } 35 | 36 | public Long getToppingsId() { 37 | return toppingsId; 38 | } 39 | 40 | public void setToppingsId(Long toppingsId) { 41 | this.toppingsId = toppingsId; 42 | } 43 | 44 | public Pizza getPizza() { 45 | return pizza; 46 | } 47 | 48 | public void setPizza(Pizza pizza) { 49 | this.pizza = pizza; 50 | } 51 | 52 | public Topping getToppings() { 53 | return toppings; 54 | } 55 | 56 | public void setToppings(Topping toppings) { 57 | this.toppings = toppings; 58 | } 59 | 60 | @Override 61 | public String toString() { 62 | return "PizzaToppings{" + 63 | "pizzaId=" + pizzaId + 64 | ", toppingsId=" + toppingsId + 65 | '}'; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /demo-s2jdbc/src/main/java/com/example/model/Topping.java: -------------------------------------------------------------------------------- 1 | package com.example.model; 2 | 3 | import com.fasterxml.jackson.annotation.JsonIgnore; 4 | 5 | import javax.persistence.*; 6 | import java.io.Serializable; 7 | import java.util.List; 8 | 9 | @Entity 10 | public class Topping implements Serializable { 11 | @Id 12 | @GeneratedValue(strategy = GenerationType.IDENTITY) 13 | private Long id; 14 | private String name; 15 | @OneToMany(mappedBy = "toppings") 16 | @JsonIgnore 17 | private List pizzaToppings; 18 | 19 | public Topping(Long id) { 20 | this.id = id; 21 | } 22 | 23 | public Topping() { 24 | 25 | } 26 | 27 | public Long getId() { 28 | return id; 29 | } 30 | 31 | public void setId(Long id) { 32 | this.id = id; 33 | } 34 | 35 | public String getName() { 36 | return name; 37 | } 38 | 39 | public void setName(String name) { 40 | this.name = name; 41 | } 42 | 43 | public List getPizzaToppings() { 44 | return pizzaToppings; 45 | } 46 | 47 | public void setPizzaToppings(List pizzaToppings) { 48 | this.pizzaToppings = pizzaToppings; 49 | } 50 | 51 | @Override 52 | public String toString() { 53 | return "Topping{" + 54 | "id=" + id + 55 | ", name='" + name + '\'' + 56 | '}'; 57 | } 58 | 59 | @Override 60 | public boolean equals(Object o) { 61 | if (this == o) return true; 62 | if (o == null || getClass() != o.getClass()) return false; 63 | 64 | Topping topping = (Topping) o; 65 | 66 | if (id != null ? !id.equals(topping.id) : topping.id != null) return false; 67 | return name != null ? name.equals(topping.name) : topping.name == null; 68 | 69 | } 70 | 71 | @Override 72 | public int hashCode() { 73 | int result = id != null ? id.hashCode() : 0; 74 | result = 31 * result + (name != null ? name.hashCode() : 0); 75 | return result; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /demo-s2jdbc/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | logging.level.org.seasar.extension.jdbc=DEBUG 2 | logging.level.org.springframework.transaction.jta=DEBUG -------------------------------------------------------------------------------- /demo-s2jdbc/src/main/resources/data.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO topping (name) VALUES ('Veggies'); 2 | INSERT INTO topping (name) VALUES ('Roma Tomatoes'); 3 | INSERT INTO topping (name) VALUES ('Red Onions'); 4 | INSERT INTO topping (name) VALUES ('Spinach'); 5 | INSERT INTO topping (name) VALUES ('Broccoli'); 6 | INSERT INTO topping (name) VALUES ('Pineapple'); 7 | INSERT INTO topping (name) VALUES ('Jalapeno Peppers'); 8 | INSERT INTO topping (name) VALUES ('Kalamata Olives'); 9 | INSERT INTO topping (name) VALUES ('Fresh Mushrooms'); 10 | INSERT INTO topping (name) VALUES ('Meats'); 11 | INSERT INTO topping (name) VALUES ('Anchovies'); 12 | INSERT INTO topping (name) VALUES ('Bacon Crumble'); 13 | INSERT INTO topping (name) VALUES ('Bacon Strips'); 14 | INSERT INTO topping (name) VALUES ('Chicken'); 15 | INSERT INTO topping (name) VALUES ('Ground Beef'); 16 | INSERT INTO topping (name) VALUES ('Italian Ham'); 17 | INSERT INTO topping (name) VALUES ('Spicy Italian Sausage'); 18 | INSERT INTO topping (name) VALUES ('Steak Strips'); 19 | INSERT INTO topping (name) VALUES ('Cheese'); 20 | INSERT INTO topping (name) VALUES ('Feta Cheese'); 21 | INSERT INTO topping (name) VALUES ('Parmesan Cheese'); 22 | INSERT INTO topping (name) VALUES ('Provolone Cheese'); 23 | INSERT INTO topping (name) VALUES ('Extra Cheese'); 24 | INSERT INTO topping (name) VALUES ('Four Cheese Blend Topping'); 25 | INSERT INTO topping (name) VALUES ('Goat Cheese'); 26 | INSERT INTO topping (name) VALUES ('Mozzarella'); 27 | 28 | INSERT INTO base(name) VALUES ('Thin Crust'); 29 | INSERT INTO base(name) VALUES ('Flatbread Crust'); 30 | INSERT INTO base(name) VALUES ('Thick Crust'); 31 | INSERT INTO base(name) VALUES ('Focaccia'); -------------------------------------------------------------------------------- /demo-s2jdbc/src/main/resources/schema.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE base IF EXISTS; 2 | DROP TABLE pizza IF EXISTS; 3 | DROP TABLE pizza_toppings IF EXISTS; 4 | DROP TABLE topping IF EXISTS; 5 | 6 | CREATE TABLE base ( 7 | id BIGINT generated BY DEFAULT AS IDENTITY, 8 | name VARCHAR(255), 9 | PRIMARY KEY (id) 10 | ); 11 | CREATE TABLE pizza ( 12 | id BIGINT generated BY DEFAULT AS IDENTITY, 13 | name VARCHAR(255), 14 | price DECIMAL(19, 2), 15 | base_id BIGINT, 16 | PRIMARY KEY (id) 17 | ); 18 | CREATE TABLE pizza_toppings ( 19 | pizza_id BIGINT NOT NULL, 20 | toppings_id BIGINT NOT NULL, 21 | PRIMARY KEY (pizza_id, toppings_id) 22 | ); 23 | CREATE TABLE topping ( 24 | id BIGINT generated BY DEFAULT AS IDENTITY, 25 | name VARCHAR(255), 26 | PRIMARY KEY (id) 27 | ); 28 | 29 | ALTER TABLE pizza 30 | ADD CONSTRAINT FKm6kai6c5evr38duyfh06ue29u FOREIGN KEY (base_id) REFERENCES base; 31 | ALTER TABLE pizza_toppings 32 | ADD CONSTRAINT FK7tcti2cxau3csvbuwxn0scb93 FOREIGN KEY (toppings_id) REFERENCES topping; 33 | ALTER TABLE pizza_toppings 34 | ADD CONSTRAINT FK78l5qtferc9qhfkkgyv99lmst FOREIGN KEY (pizza_id) REFERENCES pizza; -------------------------------------------------------------------------------- /demo-s2jdbc/src/test/java/com/example/PizzaControllerTests.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import com.example.model.Base; 4 | import com.example.model.Pizza; 5 | import com.example.model.Topping; 6 | import com.fasterxml.jackson.databind.ObjectMapper; 7 | import org.junit.Before; 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.boot.test.context.SpringBootTest; 12 | import org.springframework.boot.test.json.JacksonTester; 13 | import org.springframework.boot.test.web.client.TestRestTemplate; 14 | import org.springframework.test.context.junit4.SpringRunner; 15 | 16 | import java.math.BigDecimal; 17 | import java.util.Arrays; 18 | import java.util.List; 19 | 20 | import static org.assertj.core.api.Assertions.assertThat; 21 | 22 | @RunWith(SpringRunner.class) 23 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 24 | public class PizzaControllerTests { 25 | 26 | @Autowired 27 | TestRestTemplate restTemplate; 28 | @Autowired 29 | ObjectMapper objectMapper; 30 | 31 | private JacksonTester> json; 32 | 33 | @Before 34 | public void setup() { 35 | JacksonTester.initFields(this, objectMapper); 36 | } 37 | 38 | @Test 39 | public void contextLoads() throws Exception { 40 | Pizza pizza = new Pizza(); 41 | pizza.setBase(new Base(2L)); 42 | pizza.setToppings(Arrays.asList(new Topping(3L), new Topping(10L))); 43 | pizza.setName("Spring Pizza"); 44 | pizza.setPrice(new BigDecimal("1200")); 45 | restTemplate.postForObject("/", pizza, Void.class); 46 | List ret = Arrays.asList(restTemplate.getForObject("/", Pizza[].class)); 47 | assertThat(json.write(ret)).isEqualToJson("expected.json"); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /demo-s2jdbc/src/test/resources/com/example/expected.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "id": 1, 4 | "name": "Hello Pizza", 5 | "price": 1000.00, 6 | "toppings": [ 7 | { 8 | "id": 1, 9 | "name": "Veggies" 10 | }, 11 | { 12 | "id": 2, 13 | "name": "Roma Tomatoes" 14 | }, 15 | { 16 | "id": 3, 17 | "name": "Red Onions" 18 | }, 19 | { 20 | "id": 4, 21 | "name": "Spinach" 22 | }, 23 | { 24 | "id": 5, 25 | "name": "Broccoli" 26 | } 27 | ], 28 | "base": { 29 | "id": 1, 30 | "name": "Thin Crust" 31 | } 32 | }, 33 | { 34 | "id": 2, 35 | "name": "Spring Pizza", 36 | "price": 1200.00, 37 | "toppings": [ 38 | { 39 | "id": 3, 40 | "name": "Red Onions" 41 | }, 42 | { 43 | "id": 10, 44 | "name": "Meats" 45 | } 46 | ], 47 | "base": { 48 | "id": 2, 49 | "name": "Flatbread Crust" 50 | } 51 | } 52 | ] 53 | -------------------------------------------------------------------------------- /demo-spring-data-jpa/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/making/spring-boot-db-samples/c2d01559bbd8843f4e239b47734c08fe481afdee/demo-spring-data-jpa/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /demo-spring-data-jpa/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.3.9/apache-maven-3.3.9-bin.zip 2 | -------------------------------------------------------------------------------- /demo-spring-data-jpa/README.md: -------------------------------------------------------------------------------- 1 | Get all pizza 2 | 3 | ``` console 4 | curl localhost:8080 5 | ``` 6 | 7 | Add a pizza 8 | 9 | ``` console 10 | curl -d '{"name" : "Spring Pizza", "price" : 1200, "toppings" : [{"id" : 3}, {"id" : 10}], "base" : {"id" : "2"}}' -H 'Content-Type: application/json' localhost:8080 11 | ``` -------------------------------------------------------------------------------- /demo-spring-data-jpa/src/main/java/com/example/DemoSpringDataJpaApplication.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import com.example.model.Base; 4 | import com.example.model.Pizza; 5 | import com.example.model.Topping; 6 | import org.springframework.boot.CommandLineRunner; 7 | import org.springframework.boot.SpringApplication; 8 | import org.springframework.boot.autoconfigure.SpringBootApplication; 9 | import org.springframework.context.annotation.Bean; 10 | import org.springframework.web.bind.annotation.RestController; 11 | 12 | import java.math.BigDecimal; 13 | import java.util.Arrays; 14 | import java.util.HashSet; 15 | 16 | @SpringBootApplication 17 | public class DemoSpringDataJpaApplication { 18 | 19 | public static void main(String[] args) { 20 | SpringApplication.run(DemoSpringDataJpaApplication.class, args); 21 | } 22 | 23 | @Bean 24 | CommandLineRunner runner(PizzaRepository pizzaRepository) { 25 | return a -> { 26 | Pizza pizza = new Pizza(); 27 | pizza.setBase(new Base(1L)); 28 | pizza.setToppings(Arrays.asList(new Topping(1L), new Topping(2L), new Topping(3L), new Topping(4L), new Topping(5L))); 29 | pizza.setName("Hello Pizza"); 30 | pizza.setPrice(new BigDecimal("1000")); 31 | pizzaRepository.save(pizza); 32 | }; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /demo-spring-data-jpa/src/main/java/com/example/PizzaController.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import com.example.model.Pizza; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | import org.springframework.web.bind.annotation.PostMapping; 6 | import org.springframework.web.bind.annotation.RequestBody; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | import java.util.List; 10 | 11 | @RestController 12 | public class PizzaController { 13 | private final PizzaRepository pizzaRepository; 14 | 15 | public PizzaController(PizzaRepository pizzaRepository) { 16 | this.pizzaRepository = pizzaRepository; 17 | } 18 | 19 | @GetMapping 20 | List list() { 21 | return pizzaRepository.findOrderByIdAsc(); 22 | } 23 | 24 | @PostMapping 25 | Pizza add(@RequestBody Pizza pizza) { 26 | return pizzaRepository.save(pizza); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /demo-spring-data-jpa/src/main/java/com/example/PizzaRepository.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import com.example.model.Pizza; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | import org.springframework.data.jpa.repository.Query; 6 | 7 | import java.util.List; 8 | 9 | public interface PizzaRepository extends JpaRepository { 10 | @Query("SELECT x FROM Pizza x ORDER BY x.id ASC") 11 | List findOrderByIdAsc(); 12 | } 13 | -------------------------------------------------------------------------------- /demo-spring-data-jpa/src/main/java/com/example/model/Base.java: -------------------------------------------------------------------------------- 1 | package com.example.model; 2 | 3 | import javax.persistence.*; 4 | import java.io.Serializable; 5 | 6 | @Entity 7 | public class Base implements Serializable { 8 | @Id 9 | @GeneratedValue(strategy = GenerationType.IDENTITY) 10 | private Long id; 11 | private String name; 12 | 13 | public Base(Long id) { 14 | this.id = id; 15 | } 16 | 17 | Base() { 18 | 19 | } 20 | 21 | public Long getId() { 22 | return id; 23 | } 24 | 25 | public void setId(Long id) { 26 | this.id = id; 27 | } 28 | 29 | public String getName() { 30 | return name; 31 | } 32 | 33 | public void setName(String name) { 34 | this.name = name; 35 | } 36 | 37 | @Override 38 | public String toString() { 39 | return "Base{" + 40 | "id=" + id + 41 | ", name='" + name + '\'' + 42 | '}'; 43 | } 44 | 45 | @Override 46 | public boolean equals(Object o) { 47 | if (this == o) return true; 48 | if (!(o instanceof Base)) return false; 49 | 50 | Base base = (Base) o; 51 | 52 | if (id != null ? !id.equals(base.id) : base.id != null) return false; 53 | return name != null ? name.equals(base.name) : base.name == null; 54 | 55 | } 56 | 57 | @Override 58 | public int hashCode() { 59 | int result = id != null ? id.hashCode() : 0; 60 | result = 31 * result + (name != null ? name.hashCode() : 0); 61 | return result; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /demo-spring-data-jpa/src/main/java/com/example/model/Topping.java: -------------------------------------------------------------------------------- 1 | package com.example.model; 2 | 3 | import javax.persistence.*; 4 | import java.io.Serializable; 5 | 6 | @Entity 7 | public class Topping implements Serializable { 8 | @Id 9 | @GeneratedValue(strategy = GenerationType.IDENTITY) 10 | private Long id; 11 | private String name; 12 | 13 | public Topping(Long id) { 14 | this.id = id; 15 | } 16 | 17 | Topping() { 18 | 19 | } 20 | 21 | public Long getId() { 22 | return id; 23 | } 24 | 25 | public void setId(Long id) { 26 | this.id = id; 27 | } 28 | 29 | public String getName() { 30 | return name; 31 | } 32 | 33 | public void setName(String name) { 34 | this.name = name; 35 | } 36 | 37 | 38 | @Override 39 | public String toString() { 40 | return "Topping{" + 41 | "id=" + id + 42 | ", name='" + name + '\'' + 43 | '}'; 44 | } 45 | 46 | @Override 47 | public boolean equals(Object o) { 48 | if (this == o) return true; 49 | if (o == null || getClass() != o.getClass()) return false; 50 | 51 | Topping topping = (Topping) o; 52 | 53 | if (id != null ? !id.equals(topping.id) : topping.id != null) return false; 54 | return name != null ? name.equals(topping.name) : topping.name == null; 55 | 56 | } 57 | 58 | @Override 59 | public int hashCode() { 60 | int result = id != null ? id.hashCode() : 0; 61 | result = 31 * result + (name != null ? name.hashCode() : 0); 62 | return result; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /demo-spring-data-jpa/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | logging.level.org.hibernate.SQL=DEBUG 2 | logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE 3 | logging.level.org.springframework.orm.jpa.JpaTransactionManager=DEBUG 4 | spring.jpa.hibernate.ddl-auto=validate -------------------------------------------------------------------------------- /demo-spring-data-jpa/src/main/resources/data.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO topping (name) VALUES ('Veggies'); 2 | INSERT INTO topping (name) VALUES ('Roma Tomatoes'); 3 | INSERT INTO topping (name) VALUES ('Red Onions'); 4 | INSERT INTO topping (name) VALUES ('Spinach'); 5 | INSERT INTO topping (name) VALUES ('Broccoli'); 6 | INSERT INTO topping (name) VALUES ('Pineapple'); 7 | INSERT INTO topping (name) VALUES ('Jalapeno Peppers'); 8 | INSERT INTO topping (name) VALUES ('Kalamata Olives'); 9 | INSERT INTO topping (name) VALUES ('Fresh Mushrooms'); 10 | INSERT INTO topping (name) VALUES ('Meats'); 11 | INSERT INTO topping (name) VALUES ('Anchovies'); 12 | INSERT INTO topping (name) VALUES ('Bacon Crumble'); 13 | INSERT INTO topping (name) VALUES ('Bacon Strips'); 14 | INSERT INTO topping (name) VALUES ('Chicken'); 15 | INSERT INTO topping (name) VALUES ('Ground Beef'); 16 | INSERT INTO topping (name) VALUES ('Italian Ham'); 17 | INSERT INTO topping (name) VALUES ('Spicy Italian Sausage'); 18 | INSERT INTO topping (name) VALUES ('Steak Strips'); 19 | INSERT INTO topping (name) VALUES ('Cheese'); 20 | INSERT INTO topping (name) VALUES ('Feta Cheese'); 21 | INSERT INTO topping (name) VALUES ('Parmesan Cheese'); 22 | INSERT INTO topping (name) VALUES ('Provolone Cheese'); 23 | INSERT INTO topping (name) VALUES ('Extra Cheese'); 24 | INSERT INTO topping (name) VALUES ('Four Cheese Blend Topping'); 25 | INSERT INTO topping (name) VALUES ('Goat Cheese'); 26 | INSERT INTO topping (name) VALUES ('Mozzarella'); 27 | 28 | INSERT INTO base(name) VALUES ('Thin Crust'); 29 | INSERT INTO base(name) VALUES ('Flatbread Crust'); 30 | INSERT INTO base(name) VALUES ('Thick Crust'); 31 | INSERT INTO base(name) VALUES ('Focaccia'); 32 | -------------------------------------------------------------------------------- /demo-spring-data-jpa/src/main/resources/schema.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE base IF EXISTS; 2 | DROP TABLE pizza IF EXISTS; 3 | DROP TABLE pizza_toppings IF EXISTS; 4 | DROP TABLE topping IF EXISTS; 5 | 6 | CREATE TABLE base ( 7 | id BIGINT generated BY DEFAULT AS IDENTITY, 8 | name VARCHAR(255), 9 | PRIMARY KEY (id) 10 | ); 11 | CREATE TABLE pizza ( 12 | id BIGINT generated BY DEFAULT AS IDENTITY, 13 | name VARCHAR(255), 14 | price DECIMAL(19, 2), 15 | base_id BIGINT, 16 | PRIMARY KEY (id) 17 | ); 18 | CREATE TABLE pizza_toppings ( 19 | pizza_id BIGINT NOT NULL, 20 | toppings_id BIGINT NOT NULL, 21 | PRIMARY KEY (pizza_id, toppings_id) 22 | ); 23 | CREATE TABLE topping ( 24 | id BIGINT generated BY DEFAULT AS IDENTITY, 25 | name VARCHAR(255), 26 | PRIMARY KEY (id) 27 | ); 28 | 29 | ALTER TABLE pizza 30 | ADD CONSTRAINT FKm6kai6c5evr38duyfh06ue29u FOREIGN KEY (base_id) REFERENCES base; 31 | ALTER TABLE pizza_toppings 32 | ADD CONSTRAINT FK7tcti2cxau3csvbuwxn0scb93 FOREIGN KEY (toppings_id) REFERENCES topping; 33 | ALTER TABLE pizza_toppings 34 | ADD CONSTRAINT FK78l5qtferc9qhfkkgyv99lmst FOREIGN KEY (pizza_id) REFERENCES pizza; -------------------------------------------------------------------------------- /demo-spring-data-jpa/src/test/java/com/example/PizzaControllerTests.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import com.example.model.Base; 4 | import com.example.model.Pizza; 5 | import com.example.model.Topping; 6 | import com.fasterxml.jackson.databind.ObjectMapper; 7 | import org.junit.Before; 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.boot.test.context.SpringBootTest; 12 | import org.springframework.boot.test.json.JacksonTester; 13 | import org.springframework.boot.test.web.client.TestRestTemplate; 14 | import org.springframework.test.context.junit4.SpringRunner; 15 | 16 | import java.math.BigDecimal; 17 | import java.util.Arrays; 18 | import java.util.List; 19 | 20 | import static org.assertj.core.api.Assertions.*; 21 | 22 | @RunWith(SpringRunner.class) 23 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 24 | public class PizzaControllerTests { 25 | 26 | @Autowired 27 | TestRestTemplate restTemplate; 28 | @Autowired 29 | ObjectMapper objectMapper; 30 | 31 | private JacksonTester> json; 32 | 33 | @Before 34 | public void setup() { 35 | JacksonTester.initFields(this, objectMapper); 36 | } 37 | 38 | @Test 39 | public void contextLoads() throws Exception { 40 | Pizza pizza = new Pizza(); 41 | pizza.setBase(new Base(2L)); 42 | pizza.setToppings(Arrays.asList(new Topping(3L), new Topping(10L))); 43 | pizza.setName("Spring Pizza"); 44 | pizza.setPrice(new BigDecimal("1200")); 45 | restTemplate.postForObject("/", pizza, Void.class); 46 | List ret = Arrays.asList(restTemplate.getForObject("/", Pizza[].class)); 47 | assertThat(json.write(ret)).isEqualToJson("expected.json"); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /demo-spring-data-jpa/src/test/resources/com/example/expected.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "id": 1, 4 | "name": "Hello Pizza", 5 | "price": 1000.00, 6 | "toppings": [ 7 | { 8 | "id": 1, 9 | "name": "Veggies" 10 | }, 11 | { 12 | "id": 2, 13 | "name": "Roma Tomatoes" 14 | }, 15 | { 16 | "id": 3, 17 | "name": "Red Onions" 18 | }, 19 | { 20 | "id": 4, 21 | "name": "Spinach" 22 | }, 23 | { 24 | "id": 5, 25 | "name": "Broccoli" 26 | } 27 | ], 28 | "base": { 29 | "id": 1, 30 | "name": "Thin Crust" 31 | } 32 | }, 33 | { 34 | "id": 2, 35 | "name": "Spring Pizza", 36 | "price": 1200.00, 37 | "toppings": [ 38 | { 39 | "id": 3, 40 | "name": "Red Onions" 41 | }, 42 | { 43 | "id": 10, 44 | "name": "Meats" 45 | } 46 | ], 47 | "base": { 48 | "id": 2, 49 | "name": "Flatbread Crust" 50 | } 51 | } 52 | ] 53 | -------------------------------------------------------------------------------- /demo-spring-jdbc/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/making/spring-boot-db-samples/c2d01559bbd8843f4e239b47734c08fe481afdee/demo-spring-jdbc/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /demo-spring-jdbc/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.3.9/apache-maven-3.3.9-bin.zip 2 | -------------------------------------------------------------------------------- /demo-spring-jdbc/README.md: -------------------------------------------------------------------------------- 1 | Get all pizza 2 | 3 | ``` console 4 | curl localhost:8080 5 | ``` 6 | 7 | Add a pizza 8 | 9 | ``` console 10 | curl -d '{"name" : "Spring Pizza", "price" : 1200, "toppings" : [{"id" : 3}, {"id" : 10}], "base" : {"id" : "2"}}' -H 'Content-Type: application/json' localhost:8080 11 | ``` -------------------------------------------------------------------------------- /demo-spring-jdbc/src/main/java/com/example/DemoSpringJdbcApplication.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import com.example.model.Base; 4 | import com.example.model.Pizza; 5 | import com.example.model.Topping; 6 | import org.springframework.boot.CommandLineRunner; 7 | import org.springframework.boot.SpringApplication; 8 | import org.springframework.boot.autoconfigure.SpringBootApplication; 9 | import org.springframework.context.annotation.Bean; 10 | 11 | import java.math.BigDecimal; 12 | import java.util.Arrays; 13 | 14 | @SpringBootApplication 15 | public class DemoSpringJdbcApplication { 16 | 17 | public static void main(String[] args) { 18 | SpringApplication.run(DemoSpringJdbcApplication.class, args); 19 | } 20 | 21 | @Bean 22 | CommandLineRunner runner(PizzaRepository pizzaRepository) { 23 | return a -> { 24 | Pizza pizza = new Pizza(); 25 | pizza.setBase(new Base(1L)); 26 | pizza.setToppings(Arrays.asList(new Topping(1L), new Topping(2L), new Topping(3L), new Topping(4L), new Topping(5L))); 27 | pizza.setName("Hello Pizza"); 28 | pizza.setPrice(new BigDecimal("1000")); 29 | pizzaRepository.save(pizza); 30 | }; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /demo-spring-jdbc/src/main/java/com/example/PizzaController.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import com.example.model.Pizza; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | import org.springframework.web.bind.annotation.PostMapping; 6 | import org.springframework.web.bind.annotation.RequestBody; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | import java.util.List; 10 | 11 | @RestController 12 | public class PizzaController { 13 | private final PizzaRepository pizzaRepository; 14 | 15 | public PizzaController(PizzaRepository pizzaRepository) { 16 | this.pizzaRepository = pizzaRepository; 17 | } 18 | 19 | @GetMapping 20 | List list() { 21 | return pizzaRepository.findOrderByIdAsc(); 22 | } 23 | 24 | @PostMapping 25 | Pizza add(@RequestBody Pizza pizza) { 26 | return pizzaRepository.save(pizza); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /demo-spring-jdbc/src/main/java/com/example/model/Base.java: -------------------------------------------------------------------------------- 1 | package com.example.model; 2 | 3 | import java.io.Serializable; 4 | 5 | public class Base implements Serializable { 6 | private Long id; 7 | private String name; 8 | 9 | public Base(Long id) { 10 | this.id = id; 11 | } 12 | 13 | Base() { 14 | 15 | } 16 | 17 | public Long getId() { 18 | return id; 19 | } 20 | 21 | public void setId(Long id) { 22 | this.id = id; 23 | } 24 | 25 | public String getName() { 26 | return name; 27 | } 28 | 29 | public void setName(String name) { 30 | this.name = name; 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return "Base{" + 36 | "id=" + id + 37 | ", name='" + name + '\'' + 38 | '}'; 39 | } 40 | 41 | @Override 42 | public boolean equals(Object o) { 43 | if (this == o) return true; 44 | if (!(o instanceof Base)) return false; 45 | 46 | Base base = (Base) o; 47 | 48 | if (id != null ? !id.equals(base.id) : base.id != null) return false; 49 | return name != null ? name.equals(base.name) : base.name == null; 50 | 51 | } 52 | 53 | @Override 54 | public int hashCode() { 55 | int result = id != null ? id.hashCode() : 0; 56 | result = 31 * result + (name != null ? name.hashCode() : 0); 57 | return result; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /demo-spring-jdbc/src/main/java/com/example/model/Topping.java: -------------------------------------------------------------------------------- 1 | package com.example.model; 2 | 3 | import java.io.Serializable; 4 | 5 | public class Topping implements Serializable { 6 | private Long id; 7 | private String name; 8 | 9 | public Topping(Long id) { 10 | this.id = id; 11 | } 12 | 13 | Topping() { 14 | 15 | } 16 | 17 | public Long getId() { 18 | return id; 19 | } 20 | 21 | public void setId(Long id) { 22 | this.id = id; 23 | } 24 | 25 | public String getName() { 26 | return name; 27 | } 28 | 29 | public void setName(String name) { 30 | this.name = name; 31 | } 32 | 33 | 34 | @Override 35 | public String toString() { 36 | return "Topping{" + 37 | "id=" + id + 38 | ", name='" + name + '\'' + 39 | '}'; 40 | } 41 | 42 | @Override 43 | public boolean equals(Object o) { 44 | if (this == o) return true; 45 | if (o == null || getClass() != o.getClass()) return false; 46 | 47 | Topping topping = (Topping) o; 48 | 49 | if (id != null ? !id.equals(topping.id) : topping.id != null) return false; 50 | return name != null ? name.equals(topping.name) : topping.name == null; 51 | 52 | } 53 | 54 | @Override 55 | public int hashCode() { 56 | int result = id != null ? id.hashCode() : 0; 57 | result = 31 * result + (name != null ? name.hashCode() : 0); 58 | return result; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /demo-spring-jdbc/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | logging.level.org.springframework.jdbc.datasource.DataSourceTransactionManager=DEBUG -------------------------------------------------------------------------------- /demo-spring-jdbc/src/main/resources/data.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO topping (name) VALUES ('Veggies'); 2 | INSERT INTO topping (name) VALUES ('Roma Tomatoes'); 3 | INSERT INTO topping (name) VALUES ('Red Onions'); 4 | INSERT INTO topping (name) VALUES ('Spinach'); 5 | INSERT INTO topping (name) VALUES ('Broccoli'); 6 | INSERT INTO topping (name) VALUES ('Pineapple'); 7 | INSERT INTO topping (name) VALUES ('Jalapeno Peppers'); 8 | INSERT INTO topping (name) VALUES ('Kalamata Olives'); 9 | INSERT INTO topping (name) VALUES ('Fresh Mushrooms'); 10 | INSERT INTO topping (name) VALUES ('Meats'); 11 | INSERT INTO topping (name) VALUES ('Anchovies'); 12 | INSERT INTO topping (name) VALUES ('Bacon Crumble'); 13 | INSERT INTO topping (name) VALUES ('Bacon Strips'); 14 | INSERT INTO topping (name) VALUES ('Chicken'); 15 | INSERT INTO topping (name) VALUES ('Ground Beef'); 16 | INSERT INTO topping (name) VALUES ('Italian Ham'); 17 | INSERT INTO topping (name) VALUES ('Spicy Italian Sausage'); 18 | INSERT INTO topping (name) VALUES ('Steak Strips'); 19 | INSERT INTO topping (name) VALUES ('Cheese'); 20 | INSERT INTO topping (name) VALUES ('Feta Cheese'); 21 | INSERT INTO topping (name) VALUES ('Parmesan Cheese'); 22 | INSERT INTO topping (name) VALUES ('Provolone Cheese'); 23 | INSERT INTO topping (name) VALUES ('Extra Cheese'); 24 | INSERT INTO topping (name) VALUES ('Four Cheese Blend Topping'); 25 | INSERT INTO topping (name) VALUES ('Goat Cheese'); 26 | INSERT INTO topping (name) VALUES ('Mozzarella'); 27 | 28 | INSERT INTO base(name) VALUES ('Thin Crust'); 29 | INSERT INTO base(name) VALUES ('Flatbread Crust'); 30 | INSERT INTO base(name) VALUES ('Thick Crust'); 31 | INSERT INTO base(name) VALUES ('Focaccia'); -------------------------------------------------------------------------------- /demo-spring-jdbc/src/main/resources/schema.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE base IF EXISTS; 2 | DROP TABLE pizza IF EXISTS; 3 | DROP TABLE pizza_toppings IF EXISTS; 4 | DROP TABLE topping IF EXISTS; 5 | 6 | CREATE TABLE base ( 7 | id BIGINT generated BY DEFAULT AS IDENTITY, 8 | name VARCHAR(255), 9 | PRIMARY KEY (id) 10 | ); 11 | CREATE TABLE pizza ( 12 | id BIGINT generated BY DEFAULT AS IDENTITY, 13 | name VARCHAR(255), 14 | price DECIMAL(19, 2), 15 | base_id BIGINT, 16 | PRIMARY KEY (id) 17 | ); 18 | CREATE TABLE pizza_toppings ( 19 | pizza_id BIGINT NOT NULL, 20 | toppings_id BIGINT NOT NULL, 21 | PRIMARY KEY (pizza_id, toppings_id) 22 | ); 23 | CREATE TABLE topping ( 24 | id BIGINT generated BY DEFAULT AS IDENTITY, 25 | name VARCHAR(255), 26 | PRIMARY KEY (id) 27 | ); 28 | 29 | ALTER TABLE pizza 30 | ADD CONSTRAINT FKm6kai6c5evr38duyfh06ue29u FOREIGN KEY (base_id) REFERENCES base; 31 | ALTER TABLE pizza_toppings 32 | ADD CONSTRAINT FK7tcti2cxau3csvbuwxn0scb93 FOREIGN KEY (toppings_id) REFERENCES topping; 33 | ALTER TABLE pizza_toppings 34 | ADD CONSTRAINT FK78l5qtferc9qhfkkgyv99lmst FOREIGN KEY (pizza_id) REFERENCES pizza; -------------------------------------------------------------------------------- /demo-spring-jdbc/src/test/java/com/example/PizzaControllerTests.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import com.example.model.Base; 4 | import com.example.model.Pizza; 5 | import com.example.model.Topping; 6 | import com.fasterxml.jackson.databind.ObjectMapper; 7 | import org.junit.Before; 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.boot.test.context.SpringBootTest; 12 | import org.springframework.boot.test.json.JacksonTester; 13 | import org.springframework.boot.test.web.client.TestRestTemplate; 14 | import org.springframework.test.context.junit4.SpringRunner; 15 | 16 | import java.math.BigDecimal; 17 | import java.util.Arrays; 18 | import java.util.List; 19 | 20 | import static org.assertj.core.api.Assertions.assertThat; 21 | 22 | @RunWith(SpringRunner.class) 23 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 24 | public class PizzaControllerTests { 25 | 26 | @Autowired 27 | TestRestTemplate restTemplate; 28 | @Autowired 29 | ObjectMapper objectMapper; 30 | 31 | private JacksonTester> json; 32 | 33 | @Before 34 | public void setup() { 35 | JacksonTester.initFields(this, objectMapper); 36 | } 37 | 38 | @Test 39 | public void contextLoads() throws Exception { 40 | Pizza pizza = new Pizza(); 41 | pizza.setBase(new Base(2L)); 42 | pizza.setToppings(Arrays.asList(new Topping(3L), new Topping(10L))); 43 | pizza.setName("Spring Pizza"); 44 | pizza.setPrice(new BigDecimal("1200")); 45 | restTemplate.postForObject("/", pizza, Void.class); 46 | List ret = Arrays.asList(restTemplate.getForObject("/", Pizza[].class)); 47 | assertThat(json.write(ret)).isEqualToJson("expected.json"); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /demo-spring-jdbc/src/test/resources/com/example/expected.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "id": 1, 4 | "name": "Hello Pizza", 5 | "price": 1000.00, 6 | "toppings": [ 7 | { 8 | "id": 1, 9 | "name": "Veggies" 10 | }, 11 | { 12 | "id": 2, 13 | "name": "Roma Tomatoes" 14 | }, 15 | { 16 | "id": 3, 17 | "name": "Red Onions" 18 | }, 19 | { 20 | "id": 4, 21 | "name": "Spinach" 22 | }, 23 | { 24 | "id": 5, 25 | "name": "Broccoli" 26 | } 27 | ], 28 | "base": { 29 | "id": 1, 30 | "name": "Thin Crust" 31 | } 32 | }, 33 | { 34 | "id": 2, 35 | "name": "Spring Pizza", 36 | "price": 1200.00, 37 | "toppings": [ 38 | { 39 | "id": 3, 40 | "name": "Red Onions" 41 | }, 42 | { 43 | "id": 10, 44 | "name": "Meats" 45 | } 46 | ], 47 | "base": { 48 | "id": 2, 49 | "name": "Flatbread Crust" 50 | } 51 | } 52 | ] 53 | -------------------------------------------------------------------------------- /demo-sql2o/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/making/spring-boot-db-samples/c2d01559bbd8843f4e239b47734c08fe481afdee/demo-sql2o/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /demo-sql2o/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.3.9/apache-maven-3.3.9-bin.zip 2 | -------------------------------------------------------------------------------- /demo-sql2o/README.md: -------------------------------------------------------------------------------- 1 | Get all pizza 2 | 3 | ``` console 4 | curl localhost:8080 5 | ``` 6 | 7 | Add a pizza 8 | 9 | ``` console 10 | curl -d '{"name" : "Spring Pizza", "price" : 1200, "toppings" : [{"id" : 3}, {"id" : 10}], "base" : {"id" : "2"}}' -H 'Content-Type: application/json' localhost:8080 11 | ``` -------------------------------------------------------------------------------- /demo-sql2o/src/main/java/com/example/DemoSql2oApplication.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import com.example.model.Base; 4 | import com.example.model.Pizza; 5 | import com.example.model.Topping; 6 | import org.springframework.boot.CommandLineRunner; 7 | import org.springframework.boot.SpringApplication; 8 | import org.springframework.boot.autoconfigure.SpringBootApplication; 9 | import org.springframework.context.annotation.Bean; 10 | import org.sql2o.Sql2o; 11 | 12 | import javax.sql.DataSource; 13 | import java.math.BigDecimal; 14 | import java.util.Arrays; 15 | 16 | @SpringBootApplication 17 | public class DemoSql2oApplication { 18 | 19 | public static void main(String[] args) { 20 | SpringApplication.run(DemoSql2oApplication.class, args); 21 | } 22 | 23 | @Bean 24 | CommandLineRunner runner(PizzaRepository pizzaRepository) { 25 | return a -> { 26 | Pizza pizza = new Pizza(); 27 | pizza.setBase(new Base(1L)); 28 | pizza.setToppings(Arrays.asList(new Topping(1L), new Topping(2L), new Topping(3L), new Topping(4L), new Topping(5L))); 29 | pizza.setName("Hello Pizza"); 30 | pizza.setPrice(new BigDecimal("1000")); 31 | pizzaRepository.save(pizza); 32 | }; 33 | } 34 | 35 | @Bean 36 | Sql2o sql2o(DataSource dataSource) { 37 | return new Sql2o(dataSource); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /demo-sql2o/src/main/java/com/example/PizzaController.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import com.example.model.Pizza; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | import org.springframework.web.bind.annotation.PostMapping; 6 | import org.springframework.web.bind.annotation.RequestBody; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | import java.util.List; 10 | 11 | @RestController 12 | public class PizzaController { 13 | private final PizzaRepository pizzaRepository; 14 | 15 | public PizzaController(PizzaRepository pizzaRepository) { 16 | this.pizzaRepository = pizzaRepository; 17 | } 18 | 19 | @GetMapping 20 | List list() { 21 | return pizzaRepository.findOrderByIdAsc(); 22 | } 23 | 24 | @PostMapping 25 | Pizza add(@RequestBody Pizza pizza) { 26 | return pizzaRepository.save(pizza); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /demo-sql2o/src/main/java/com/example/model/Base.java: -------------------------------------------------------------------------------- 1 | package com.example.model; 2 | 3 | import java.io.Serializable; 4 | 5 | public class Base implements Serializable { 6 | private Long id; 7 | private String name; 8 | 9 | public Base(Long id) { 10 | this.id = id; 11 | } 12 | 13 | Base() { 14 | 15 | } 16 | 17 | public Long getId() { 18 | return id; 19 | } 20 | 21 | public void setId(Long id) { 22 | this.id = id; 23 | } 24 | 25 | public String getName() { 26 | return name; 27 | } 28 | 29 | public void setName(String name) { 30 | this.name = name; 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return "Base{" + 36 | "id=" + id + 37 | ", name='" + name + '\'' + 38 | '}'; 39 | } 40 | 41 | @Override 42 | public boolean equals(Object o) { 43 | if (this == o) return true; 44 | if (!(o instanceof Base)) return false; 45 | 46 | Base base = (Base) o; 47 | 48 | if (id != null ? !id.equals(base.id) : base.id != null) return false; 49 | return name != null ? name.equals(base.name) : base.name == null; 50 | 51 | } 52 | 53 | @Override 54 | public int hashCode() { 55 | int result = id != null ? id.hashCode() : 0; 56 | result = 31 * result + (name != null ? name.hashCode() : 0); 57 | return result; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /demo-sql2o/src/main/java/com/example/model/Topping.java: -------------------------------------------------------------------------------- 1 | package com.example.model; 2 | 3 | import java.io.Serializable; 4 | 5 | public class Topping implements Serializable { 6 | private Long id; 7 | private String name; 8 | 9 | public Topping(Long id) { 10 | this.id = id; 11 | } 12 | 13 | Topping() { 14 | 15 | } 16 | 17 | public Long getId() { 18 | return id; 19 | } 20 | 21 | public void setId(Long id) { 22 | this.id = id; 23 | } 24 | 25 | public String getName() { 26 | return name; 27 | } 28 | 29 | public void setName(String name) { 30 | this.name = name; 31 | } 32 | 33 | 34 | @Override 35 | public String toString() { 36 | return "Topping{" + 37 | "id=" + id + 38 | ", name='" + name + '\'' + 39 | '}'; 40 | } 41 | 42 | @Override 43 | public boolean equals(Object o) { 44 | if (this == o) return true; 45 | if (o == null || getClass() != o.getClass()) return false; 46 | 47 | Topping topping = (Topping) o; 48 | 49 | if (id != null ? !id.equals(topping.id) : topping.id != null) return false; 50 | return name != null ? name.equals(topping.name) : topping.name == null; 51 | 52 | } 53 | 54 | @Override 55 | public int hashCode() { 56 | int result = id != null ? id.hashCode() : 0; 57 | result = 31 * result + (name != null ? name.hashCode() : 0); 58 | return result; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /demo-sql2o/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | logging.level.org.springframework.jdbc.datasource.DataSourceTransactionManager=DEBUG -------------------------------------------------------------------------------- /demo-sql2o/src/main/resources/data.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO topping (name) VALUES ('Veggies'); 2 | INSERT INTO topping (name) VALUES ('Roma Tomatoes'); 3 | INSERT INTO topping (name) VALUES ('Red Onions'); 4 | INSERT INTO topping (name) VALUES ('Spinach'); 5 | INSERT INTO topping (name) VALUES ('Broccoli'); 6 | INSERT INTO topping (name) VALUES ('Pineapple'); 7 | INSERT INTO topping (name) VALUES ('Jalapeno Peppers'); 8 | INSERT INTO topping (name) VALUES ('Kalamata Olives'); 9 | INSERT INTO topping (name) VALUES ('Fresh Mushrooms'); 10 | INSERT INTO topping (name) VALUES ('Meats'); 11 | INSERT INTO topping (name) VALUES ('Anchovies'); 12 | INSERT INTO topping (name) VALUES ('Bacon Crumble'); 13 | INSERT INTO topping (name) VALUES ('Bacon Strips'); 14 | INSERT INTO topping (name) VALUES ('Chicken'); 15 | INSERT INTO topping (name) VALUES ('Ground Beef'); 16 | INSERT INTO topping (name) VALUES ('Italian Ham'); 17 | INSERT INTO topping (name) VALUES ('Spicy Italian Sausage'); 18 | INSERT INTO topping (name) VALUES ('Steak Strips'); 19 | INSERT INTO topping (name) VALUES ('Cheese'); 20 | INSERT INTO topping (name) VALUES ('Feta Cheese'); 21 | INSERT INTO topping (name) VALUES ('Parmesan Cheese'); 22 | INSERT INTO topping (name) VALUES ('Provolone Cheese'); 23 | INSERT INTO topping (name) VALUES ('Extra Cheese'); 24 | INSERT INTO topping (name) VALUES ('Four Cheese Blend Topping'); 25 | INSERT INTO topping (name) VALUES ('Goat Cheese'); 26 | INSERT INTO topping (name) VALUES ('Mozzarella'); 27 | 28 | INSERT INTO base(name) VALUES ('Thin Crust'); 29 | INSERT INTO base(name) VALUES ('Flatbread Crust'); 30 | INSERT INTO base(name) VALUES ('Thick Crust'); 31 | INSERT INTO base(name) VALUES ('Focaccia'); -------------------------------------------------------------------------------- /demo-sql2o/src/main/resources/schema.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE base IF EXISTS; 2 | DROP TABLE pizza IF EXISTS; 3 | DROP TABLE pizza_toppings IF EXISTS; 4 | DROP TABLE topping IF EXISTS; 5 | 6 | CREATE TABLE base ( 7 | id BIGINT generated BY DEFAULT AS IDENTITY, 8 | name VARCHAR(255), 9 | PRIMARY KEY (id) 10 | ); 11 | CREATE TABLE pizza ( 12 | id BIGINT generated BY DEFAULT AS IDENTITY, 13 | name VARCHAR(255), 14 | price DECIMAL(19, 2), 15 | base_id BIGINT, 16 | PRIMARY KEY (id) 17 | ); 18 | CREATE TABLE pizza_toppings ( 19 | pizza_id BIGINT NOT NULL, 20 | toppings_id BIGINT NOT NULL, 21 | PRIMARY KEY (pizza_id, toppings_id) 22 | ); 23 | CREATE TABLE topping ( 24 | id BIGINT generated BY DEFAULT AS IDENTITY, 25 | name VARCHAR(255), 26 | PRIMARY KEY (id) 27 | ); 28 | 29 | ALTER TABLE pizza 30 | ADD CONSTRAINT FKm6kai6c5evr38duyfh06ue29u FOREIGN KEY (base_id) REFERENCES base; 31 | ALTER TABLE pizza_toppings 32 | ADD CONSTRAINT FK7tcti2cxau3csvbuwxn0scb93 FOREIGN KEY (toppings_id) REFERENCES topping; 33 | ALTER TABLE pizza_toppings 34 | ADD CONSTRAINT FK78l5qtferc9qhfkkgyv99lmst FOREIGN KEY (pizza_id) REFERENCES pizza; -------------------------------------------------------------------------------- /demo-sql2o/src/test/java/com/example/PizzaControllerTests.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import com.example.model.Base; 4 | import com.example.model.Pizza; 5 | import com.example.model.Topping; 6 | import com.fasterxml.jackson.databind.ObjectMapper; 7 | import org.junit.Before; 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.boot.test.context.SpringBootTest; 12 | import org.springframework.boot.test.json.JacksonTester; 13 | import org.springframework.boot.test.web.client.TestRestTemplate; 14 | import org.springframework.test.context.junit4.SpringRunner; 15 | 16 | import java.math.BigDecimal; 17 | import java.util.Arrays; 18 | import java.util.List; 19 | 20 | import static org.assertj.core.api.Assertions.assertThat; 21 | 22 | @RunWith(SpringRunner.class) 23 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 24 | public class PizzaControllerTests { 25 | 26 | @Autowired 27 | TestRestTemplate restTemplate; 28 | @Autowired 29 | ObjectMapper objectMapper; 30 | 31 | private JacksonTester> json; 32 | 33 | @Before 34 | public void setup() { 35 | JacksonTester.initFields(this, objectMapper); 36 | } 37 | 38 | @Test 39 | public void contextLoads() throws Exception { 40 | Pizza pizza = new Pizza(); 41 | pizza.setBase(new Base(2L)); 42 | pizza.setToppings(Arrays.asList(new Topping(3L), new Topping(10L))); 43 | pizza.setName("Spring Pizza"); 44 | pizza.setPrice(new BigDecimal("1200")); 45 | restTemplate.postForObject("/", pizza, Void.class); 46 | List ret = Arrays.asList(restTemplate.getForObject("/", Pizza[].class)); 47 | assertThat(json.write(ret)).isEqualToJson("expected.json"); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /demo-sql2o/src/test/resources/com/example/expected.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "id": 1, 4 | "name": "Hello Pizza", 5 | "price": 1000.00, 6 | "toppings": [ 7 | { 8 | "id": 1, 9 | "name": "Veggies" 10 | }, 11 | { 12 | "id": 2, 13 | "name": "Roma Tomatoes" 14 | }, 15 | { 16 | "id": 3, 17 | "name": "Red Onions" 18 | }, 19 | { 20 | "id": 4, 21 | "name": "Spinach" 22 | }, 23 | { 24 | "id": 5, 25 | "name": "Broccoli" 26 | } 27 | ], 28 | "base": { 29 | "id": 1, 30 | "name": "Thin Crust" 31 | } 32 | }, 33 | { 34 | "id": 2, 35 | "name": "Spring Pizza", 36 | "price": 1200.00, 37 | "toppings": [ 38 | { 39 | "id": 3, 40 | "name": "Red Onions" 41 | }, 42 | { 43 | "id": 10, 44 | "name": "Meats" 45 | } 46 | ], 47 | "base": { 48 | "id": 2, 49 | "name": "Flatbread Crust" 50 | } 51 | } 52 | ] 53 | --------------------------------------------------------------------------------