[BAEL-3164] Rename module to spring-boot-persistence-2

This commit is contained in:
Philippe 2020-01-26 22:14:04 -03:00
parent 45de2abf96
commit a9eb0eaf8f
21 changed files with 1 additions and 569 deletions

View File

@ -42,7 +42,7 @@
<module>redis</module> <module>redis</module>
<!-- <module>sirix</module> --> <!-- We haven't upgraded to java 11. Fixing in BAEL-10841 --> <!-- <module>sirix</module> --> <!-- We haven't upgraded to java 11. Fixing in BAEL-10841 -->
<module>solr</module> <module>solr</module>
<module>spring-boot-jdbi</module> <module>spring-boot-persistence-2</module>
<module>spring-boot-mysql</module> <module>spring-boot-mysql</module>
<module>spring-boot-persistence</module> <module>spring-boot-persistence</module>
<module>spring-boot-persistence-h2</module> <module>spring-boot-persistence-h2</module>

View File

@ -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)

View File

@ -1,3 +0,0 @@
### Relevant Articles:
- [Using JDBI with Spring Boot](https://www.baeldung.com/spring-boot-jdbi)

View File

@ -1,102 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung.boot.jdbi</groupId>
<artifactId>spring-boot-jdbi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-jdbi</name>
<description>Sample SpringBoot JDBI Project</description>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.8.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.jdbi</groupId>
<artifactId>jdbi3-spring4</artifactId>
<version>${jdbi.version}</version>
</dependency>
<dependency>
<groupId>org.jdbi</groupId>
<artifactId>jdbi3-sqlobject</artifactId>
<version>${jdbi.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.jdbi</groupId>
<artifactId>jdbi3-spring4</artifactId>
</dependency>
<dependency>
<groupId>org.jdbi</groupId>
<artifactId>jdbi3-sqlobject</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<properties>
<java.version>1.8</java.version>
<jdbi.version>3.9.1</jdbi.version>
</properties>
</project>

View File

@ -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<JdbiPlugin> jdbiPlugins, List<RowMapper<?>> 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);
}
}

View File

@ -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);
}
}

View File

@ -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<Long> bulkInsert(@BindBean List<CarMaker> carMakers);
@SqlQuery
CarMaker findById(@Bind("id") Long id);
}

View File

@ -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<Long> bulkInsert(@BindBean List<CarModel> models);
@SqlQuery
CarModel findByMakerIdAndSku(@Bind("makerId") Long makerId, @Bind("sku") String sku );
}

View File

@ -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<CarModel> models;
}

View File

@ -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;
}

View File

@ -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<CarMaker> {
@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<CarModel>())
.build();
return maker;
}
}

View File

@ -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<CarModel>{
@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();
}
}

View File

@ -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();
}
}

View File

@ -1,11 +0,0 @@
--
-- findById
--
select
id,
name
from
car_maker
where
id = :id
;

View File

@ -1,4 +0,0 @@
--
-- Insert
--
insert into car_maker(id,name) values (:id,:name);

View File

@ -1,10 +0,0 @@
--
-- Insert
--
select *
from
car_model
where
maker_fk = :makerId and
sku = :sku
;

View File

@ -1,8 +0,0 @@
--
-- Insert
--
insert into car_model(maker_fk,name,sku,year) values (
:makerId,
:name,
:sku,
:year );

View File

@ -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<CarMaker> makers = new ArrayList<>();
makers.add(carMaker1);
makers.add(carMaker2);
List<Long> 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();
}
}

View File

@ -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);

View File

@ -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);