diff --git a/persistence-modules/pom.xml b/persistence-modules/pom.xml index c0f7c07b70..7ab9f33085 100644 --- a/persistence-modules/pom.xml +++ b/persistence-modules/pom.xml @@ -42,7 +42,7 @@ redis solr - spring-boot-jdbi + spring-boot-persistence-2 spring-boot-mysql spring-boot-persistence spring-boot-persistence-h2 diff --git a/persistence-modules/spring-boot-jdbi/HELP.md b/persistence-modules/spring-boot-jdbi/HELP.md deleted file mode 100644 index d5a5463718..0000000000 --- a/persistence-modules/spring-boot-jdbi/HELP.md +++ /dev/null @@ -1,9 +0,0 @@ -# Getting Started - -### Reference Documentation -For further reference, please consider the following sections: - -* [Official Apache Maven documentation](https://maven.apache.org/guides/index.html) -* [Spring Boot DevTools](https://docs.spring.io/spring-boot/docs/{bootVersion}/reference/htmlsingle/#using-boot-devtools) -* [Spring Configuration Processor](https://docs.spring.io/spring-boot/docs/{bootVersion}/reference/htmlsingle/#configuration-metadata-annotation-processor) - diff --git a/persistence-modules/spring-boot-jdbi/README.md b/persistence-modules/spring-boot-jdbi/README.md deleted file mode 100644 index 5d171fb2ca..0000000000 --- a/persistence-modules/spring-boot-jdbi/README.md +++ /dev/null @@ -1,3 +0,0 @@ -### Relevant Articles: - -- [Using JDBI with Spring Boot](https://www.baeldung.com/spring-boot-jdbi) diff --git a/persistence-modules/spring-boot-jdbi/pom.xml b/persistence-modules/spring-boot-jdbi/pom.xml deleted file mode 100644 index db36185acd..0000000000 --- a/persistence-modules/spring-boot-jdbi/pom.xml +++ /dev/null @@ -1,102 +0,0 @@ - - - 4.0.0 - com.baeldung.boot.jdbi - spring-boot-jdbi - 0.0.1-SNAPSHOT - spring-boot-jdbi - Sample SpringBoot JDBI Project - - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - ../../pom.xml - - - - - - org.springframework.boot - spring-boot-dependencies - 2.1.8.RELEASE - pom - import - - - - org.jdbi - jdbi3-spring4 - ${jdbi.version} - - - - org.jdbi - jdbi3-sqlobject - ${jdbi.version} - - - - - - - - org.springframework.boot - spring-boot-starter-jdbc - - - - org.jdbi - jdbi3-spring4 - - - - org.jdbi - jdbi3-sqlobject - - - - org.springframework.boot - spring-boot-devtools - runtime - true - - - com.h2database - h2 - runtime - - - org.springframework.boot - spring-boot-configuration-processor - true - - - org.projectlombok - lombok - true - - - org.springframework.boot - spring-boot-starter-test - test - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - - - - 1.8 - 3.9.1 - - - diff --git a/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/JdbiConfiguration.java b/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/JdbiConfiguration.java deleted file mode 100644 index ddbe6cc118..0000000000 --- a/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/JdbiConfiguration.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.baeldung.boot.jdbi; - -import java.util.List; - -import javax.sql.DataSource; - -import org.jdbi.v3.core.Jdbi; -import org.jdbi.v3.core.mapper.RowMapper; -import org.jdbi.v3.core.spi.JdbiPlugin; -import org.jdbi.v3.sqlobject.SqlObjectPlugin; -import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Lazy; -import org.springframework.jdbc.datasource.DataSourceTransactionManager; -import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy; -import org.springframework.transaction.PlatformTransactionManager; - -import com.baeldung.boot.jdbi.dao.CarMakerDao; -import com.baeldung.boot.jdbi.dao.CarModelDao; - -import lombok.extern.slf4j.Slf4j; - -@Configuration -@Slf4j -public class JdbiConfiguration { - @Bean - public Jdbi jdbi(DataSource ds,List jdbiPlugins, List> rowMappers) { - TransactionAwareDataSourceProxy proxy = new TransactionAwareDataSourceProxy(ds); - Jdbi jdbi = Jdbi.create(proxy); - - // Register all available plugins - log.info("[I27] Installing plugins... ({} found)", jdbiPlugins.size()); - jdbiPlugins.forEach(plugin -> jdbi.installPlugin(plugin)); - - // Register all available rowMappers - log.info("[I31] Installing rowMappers... ({} found)", rowMappers.size()); - rowMappers.forEach(mapper -> jdbi.registerRowMapper(mapper)); - - return jdbi; - } - - @Bean - public JdbiPlugin sqlObjectPlugin() { - return new SqlObjectPlugin(); - } - - @Bean - public CarMakerDao carMakerDao(Jdbi jdbi) { - return jdbi.onDemand(CarMakerDao.class); - } - - @Bean - public CarModelDao carModelDao(Jdbi jdbi) { - return jdbi.onDemand(CarModelDao.class); - } -} diff --git a/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/SpringBootJdbiApplication.java b/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/SpringBootJdbiApplication.java deleted file mode 100644 index 63afe3a3bf..0000000000 --- a/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/SpringBootJdbiApplication.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.baeldung.boot.jdbi; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.transaction.annotation.EnableTransactionManagement; - -@SpringBootApplication -@EnableTransactionManagement -public class SpringBootJdbiApplication { - - public static void main(String[] args) { - SpringApplication.run(SpringBootJdbiApplication.class, args); - } - -} diff --git a/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/dao/CarMakerDao.java b/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/dao/CarMakerDao.java deleted file mode 100644 index 6cc7268144..0000000000 --- a/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/dao/CarMakerDao.java +++ /dev/null @@ -1,35 +0,0 @@ -/** - * - */ -package com.baeldung.boot.jdbi.dao; - -import java.util.List; - -import org.jdbi.v3.sqlobject.customizer.Bind; -import org.jdbi.v3.sqlobject.customizer.BindBean; -import org.jdbi.v3.sqlobject.locator.UseClasspathSqlLocator; -import org.jdbi.v3.sqlobject.statement.GetGeneratedKeys; -import org.jdbi.v3.sqlobject.statement.SqlBatch; -import org.jdbi.v3.sqlobject.statement.SqlQuery; -import org.jdbi.v3.sqlobject.statement.SqlUpdate; - -import com.baeldung.boot.jdbi.domain.CarMaker; - -/** - * @author Philippe - * - */ -@UseClasspathSqlLocator -public interface CarMakerDao { - - @SqlUpdate - @GetGeneratedKeys - Long insert(@BindBean CarMaker carMaker); - - @SqlBatch("insert") - @GetGeneratedKeys - List bulkInsert(@BindBean List carMakers); - - @SqlQuery - CarMaker findById(@Bind("id") Long id); -} diff --git a/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/dao/CarModelDao.java b/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/dao/CarModelDao.java deleted file mode 100644 index 18a05c6108..0000000000 --- a/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/dao/CarModelDao.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.baeldung.boot.jdbi.dao; - -import java.util.List; - -import org.jdbi.v3.sqlobject.customizer.Bind; -import org.jdbi.v3.sqlobject.customizer.BindBean; -import org.jdbi.v3.sqlobject.locator.UseClasspathSqlLocator; -import org.jdbi.v3.sqlobject.statement.GetGeneratedKeys; -import org.jdbi.v3.sqlobject.statement.SqlBatch; -import org.jdbi.v3.sqlobject.statement.SqlQuery; -import org.jdbi.v3.sqlobject.statement.SqlUpdate; - -import com.baeldung.boot.jdbi.domain.CarModel; - -@UseClasspathSqlLocator -public interface CarModelDao { - - @SqlUpdate("insert") - @GetGeneratedKeys - Long insert(@BindBean CarModel carModel); - - @SqlBatch("insert") - @GetGeneratedKeys - List bulkInsert(@BindBean List models); - - @SqlQuery - CarModel findByMakerIdAndSku(@Bind("makerId") Long makerId, @Bind("sku") String sku ); -} diff --git a/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/domain/CarMaker.java b/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/domain/CarMaker.java deleted file mode 100644 index c32b0c30db..0000000000 --- a/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/domain/CarMaker.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.baeldung.boot.jdbi.domain; - -import java.util.List; - -import lombok.Builder; -import lombok.Data; - -@Data -@Builder -public class CarMaker { - private Long id; - private String name; - private List models; -} diff --git a/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/domain/CarModel.java b/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/domain/CarModel.java deleted file mode 100644 index 80b615801b..0000000000 --- a/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/domain/CarModel.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.baeldung.boot.jdbi.domain; - -import lombok.Builder; -import lombok.Data; - -@Builder -@Data -public class CarModel { - private Long id; - private String name; - private Integer year; - private String sku; - private Long makerId; -} diff --git a/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/mapper/CarMakerMapper.java b/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/mapper/CarMakerMapper.java deleted file mode 100644 index 54fc80d4ab..0000000000 --- a/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/mapper/CarMakerMapper.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.baeldung.boot.jdbi.mapper; - -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.ArrayList; - -import org.jdbi.v3.core.mapper.RowMapper; -import org.jdbi.v3.core.statement.StatementContext; -import org.springframework.stereotype.Component; - -import com.baeldung.boot.jdbi.domain.CarMaker; -import com.baeldung.boot.jdbi.domain.CarModel; - -@Component -public class CarMakerMapper implements RowMapper { - - @Override - public CarMaker map(ResultSet rs, StatementContext ctx) throws SQLException { - CarMaker maker = CarMaker.builder() - .id(rs.getLong("id")) - .name(rs.getString("name")) - .models(new ArrayList()) - .build(); - - return maker; - } -} diff --git a/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/mapper/CarModelMapper.java b/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/mapper/CarModelMapper.java deleted file mode 100644 index eeceafd649..0000000000 --- a/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/mapper/CarModelMapper.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.baeldung.boot.jdbi.mapper; - -import java.sql.ResultSet; -import java.sql.SQLException; - -import org.jdbi.v3.core.mapper.RowMapper; -import org.jdbi.v3.core.statement.StatementContext; -import org.springframework.stereotype.Component; - -import com.baeldung.boot.jdbi.domain.CarModel; - -@Component -public class CarModelMapper implements RowMapper{ - - @Override - public CarModel map(ResultSet rs, StatementContext ctx) throws SQLException { - return CarModel.builder() - .id(rs.getLong("id")) - .name(rs.getString("name")) - .sku(rs.getString("sku")) - .year(rs.getInt("year")) - .build(); - } - -} diff --git a/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/service/CarMakerService.java b/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/service/CarMakerService.java deleted file mode 100644 index a058130563..0000000000 --- a/persistence-modules/spring-boot-jdbi/src/main/java/com/baeldung/boot/jdbi/service/CarMakerService.java +++ /dev/null @@ -1,48 +0,0 @@ -/** - * - */ -package com.baeldung.boot.jdbi.service; - -import java.util.List; - -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - -import com.baeldung.boot.jdbi.dao.CarMakerDao; -import com.baeldung.boot.jdbi.dao.CarModelDao; -import com.baeldung.boot.jdbi.domain.CarMaker; -import com.baeldung.boot.jdbi.domain.CarModel; - -/** - * @author Philippe - * - */ -@Service -public class CarMakerService { - - private CarMakerDao carMakerDao; - private CarModelDao carModelDao; - - public CarMakerService(CarMakerDao carMakerDao,CarModelDao carModelDao) { - - this.carMakerDao = carMakerDao; - this.carModelDao = carModelDao; - } - - @Transactional - public int bulkInsert(CarMaker carMaker) { - Long carMakerId; - if (carMaker.getId() == null ) { - carMakerId = carMakerDao.insert(carMaker); - carMaker.setId(carMakerId); - } - - // Make sure all models belong to the same maker - carMaker.getModels().forEach(m -> { - m.setMakerId(carMaker.getId()); - carModelDao.insert(m); - }); - - return carMaker.getModels().size(); - } -} diff --git a/persistence-modules/spring-boot-jdbi/src/main/resources/application.yml b/persistence-modules/spring-boot-jdbi/src/main/resources/application.yml deleted file mode 100644 index 8b13789179..0000000000 --- a/persistence-modules/spring-boot-jdbi/src/main/resources/application.yml +++ /dev/null @@ -1 +0,0 @@ - diff --git a/persistence-modules/spring-boot-jdbi/src/main/resources/com/baeldung/boot/jdbi/dao/CarMakerDao/findById.sql b/persistence-modules/spring-boot-jdbi/src/main/resources/com/baeldung/boot/jdbi/dao/CarMakerDao/findById.sql deleted file mode 100644 index b36659110a..0000000000 --- a/persistence-modules/spring-boot-jdbi/src/main/resources/com/baeldung/boot/jdbi/dao/CarMakerDao/findById.sql +++ /dev/null @@ -1,11 +0,0 @@ --- --- findById --- -select - id, - name -from - car_maker -where - id = :id -; diff --git a/persistence-modules/spring-boot-jdbi/src/main/resources/com/baeldung/boot/jdbi/dao/CarMakerDao/insert.sql b/persistence-modules/spring-boot-jdbi/src/main/resources/com/baeldung/boot/jdbi/dao/CarMakerDao/insert.sql deleted file mode 100644 index 0e045d7274..0000000000 --- a/persistence-modules/spring-boot-jdbi/src/main/resources/com/baeldung/boot/jdbi/dao/CarMakerDao/insert.sql +++ /dev/null @@ -1,4 +0,0 @@ --- --- Insert --- -insert into car_maker(id,name) values (:id,:name); diff --git a/persistence-modules/spring-boot-jdbi/src/main/resources/com/baeldung/boot/jdbi/dao/CarModelDao/findByMakerIdAndSku.sql b/persistence-modules/spring-boot-jdbi/src/main/resources/com/baeldung/boot/jdbi/dao/CarModelDao/findByMakerIdAndSku.sql deleted file mode 100644 index 270d9baaa8..0000000000 --- a/persistence-modules/spring-boot-jdbi/src/main/resources/com/baeldung/boot/jdbi/dao/CarModelDao/findByMakerIdAndSku.sql +++ /dev/null @@ -1,10 +0,0 @@ --- --- Insert --- -select * -from - car_model -where - maker_fk = :makerId and - sku = :sku -; \ No newline at end of file diff --git a/persistence-modules/spring-boot-jdbi/src/main/resources/com/baeldung/boot/jdbi/dao/CarModelDao/insert.sql b/persistence-modules/spring-boot-jdbi/src/main/resources/com/baeldung/boot/jdbi/dao/CarModelDao/insert.sql deleted file mode 100644 index b277213584..0000000000 --- a/persistence-modules/spring-boot-jdbi/src/main/resources/com/baeldung/boot/jdbi/dao/CarModelDao/insert.sql +++ /dev/null @@ -1,8 +0,0 @@ --- --- Insert --- -insert into car_model(maker_fk,name,sku,year) values ( - :makerId, - :name, - :sku, - :year ); diff --git a/persistence-modules/spring-boot-jdbi/src/test/java/com/baeldung/boot/jdbi/SpringBootJdbiApplicationUnitTest.java b/persistence-modules/spring-boot-jdbi/src/test/java/com/baeldung/boot/jdbi/SpringBootJdbiApplicationUnitTest.java deleted file mode 100644 index e4b623ee2b..0000000000 --- a/persistence-modules/spring-boot-jdbi/src/test/java/com/baeldung/boot/jdbi/SpringBootJdbiApplicationUnitTest.java +++ /dev/null @@ -1,121 +0,0 @@ -package com.baeldung.boot.jdbi; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -import org.jdbi.v3.core.Jdbi; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; - -import com.baeldung.boot.jdbi.dao.CarMakerDao; -import com.baeldung.boot.jdbi.dao.CarModelDao; -import com.baeldung.boot.jdbi.domain.CarMaker; -import com.baeldung.boot.jdbi.domain.CarModel; -import com.baeldung.boot.jdbi.service.CarMakerService; - -import lombok.extern.slf4j.Slf4j; - -@RunWith(SpringRunner.class) -@SpringBootTest -@Slf4j -public class SpringBootJdbiApplicationUnitTest { - - - @Autowired - private CarMakerDao carMakerDao; - - @Autowired - private CarModelDao carModelDao; - - @Autowired - private CarMakerService carMakerService; - - @Test - public void givenNewCarMaker_whenInsertNewCarMaker_thenSuccess() { - - assertNotNull(carMakerDao); - - CarMaker carMaker = CarMaker.builder() - .name("Diamond Motors") - .build(); - - Long generatedId = carMakerDao.insert(carMaker); - log.info("[I37] generatedId = {}", generatedId); - assertThat(generatedId).isGreaterThan(0); - } - - @Test - public void givenNewCarMakers_whenInsertNewCarMakers_thenSuccess() { - - assertNotNull(carMakerDao); - - CarMaker carMaker1 = CarMaker.builder() - .name("maker1") - .build(); - - CarMaker carMaker2 = CarMaker.builder() - .name("maker2") - .build(); - - List makers = new ArrayList<>(); - makers.add(carMaker1); - makers.add(carMaker2); - - List generatedIds = carMakerDao.bulkInsert(makers); - log.info("[I37] generatedIds = {}", generatedIds); - assertThat(generatedIds).size().isEqualTo(makers.size()); - } - - - @Test - public void givenExistingCarMaker_whenFindById_thenReturnExistingCarMaker() { - - CarMaker maker = carMakerDao.findById(1l); - assertThat(maker).isNotNull(); - assertThat(maker.getId()).isEqualTo(1); - - } - - @Test - public void givenExistingCarMaker_whenBulkInsertFails_thenRollback() { - - CarMaker maker = carMakerDao.findById(1l); - CarModel m1 = CarModel.builder() - .makerId(maker.getId()) - .name("Model X1") - .sku("1-M1") - .year(2019) - .build(); - maker.getModels().add(m1); - - CarModel m2 = CarModel.builder() - .makerId(maker.getId()) - .name("Model X1") - .sku("1-M1") - .year(2019) - .build(); - maker.getModels().add(m2); - - // This insert fails because we have the same SKU - try { - carMakerService.bulkInsert(maker); - assertTrue("Insert must fail", true); - } - catch(Exception ex) { - log.info("[I113] Exception: {}", ex.getMessage()); - } - - CarModel m = carModelDao.findByMakerIdAndSku(maker.getId(), "1-M1"); - assertThat(m).isNull(); - - } - -} diff --git a/persistence-modules/spring-boot-jdbi/src/test/resources/data.sql b/persistence-modules/spring-boot-jdbi/src/test/resources/data.sql deleted file mode 100644 index e3e1f4ae32..0000000000 --- a/persistence-modules/spring-boot-jdbi/src/test/resources/data.sql +++ /dev/null @@ -1,12 +0,0 @@ - -insert into car_maker(id,name) values (1,'Special Motors'); -insert into car_maker(id,name) values (2,'BWM'); -insert into car_maker(id,name) values (3,'Dolores'); - -insert into car_model(id,maker_fk,name,sku,year) values(1,1,'Muze','SM001',2018); -insert into car_model(id,maker_fk,name,sku,year) values(2,1,'Empada','SM002',2008); - -insert into car_model(id,maker_fk,name,sku,year) values(4,2,'BWM-100','BWM100',2008); -insert into car_model(id,maker_fk,name,sku,year) values(5,2,'BWM-200','BWM200',2009); -insert into car_model(id,maker_fk,name,sku,year) values(6,2,'BWM-300','BWM300',2008); - diff --git a/persistence-modules/spring-boot-jdbi/src/test/resources/schema.sql b/persistence-modules/spring-boot-jdbi/src/test/resources/schema.sql deleted file mode 100644 index a0d0eaf62e..0000000000 --- a/persistence-modules/spring-boot-jdbi/src/test/resources/schema.sql +++ /dev/null @@ -1,24 +0,0 @@ --- --- Car makers table --- -create table car_maker( - id identity, - name varchar(128) not null -); - -create unique index ui_car_maker_01 on car_maker(name); - --- --- Car models table --- -create table car_model( - id identity, - maker_fk int not null, - name varchar(128) not null, - sku varchar(128) not null, - year int not null -); - -create unique index ui_car_model_01 on car_model(maker_fk,sku); -create unique index ui_car_model_02 on car_model(maker_fk,name,year); -