[BAEL-3164] Rename module to spring-boot-persistence-2
This commit is contained in:
+57
@@ -0,0 +1,57 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
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);
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
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 );
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
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;
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
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;
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user