10 Ağustos 2021 Salı

SpringData Jdbc Kullanımı

Maven
Şu satırı dahil ederiz
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency> 
  <groupId>org.postgresql</groupId>
  <artifactId>postgresql</artifactId>
  <scope>runtime</scope>
</dependency> 
<dependency> 
  <groupId>com.h2database</groupId>
  <artifactId>h2</artifactId>
  <scope>test</scope>
</dependency> 
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
</dependency>
Açıklaması şöyle
When we use Spring Boot, we don’t need to create DataSource and JdbcTemplate/NamedParameterJdbcTemplate.

We just need to provide following details in application.properties and include corresponding starter poms in pom.xml:

spring.datasource.url
spring.datasource.username
spring.datasource.password

Spring boot will automatically create DataSource and JdbcTemplate bean.
Örnek
application.properties şöyledir
#database
spring.datasource.url=jdbc:postgresql://localhost:5432/cars
spring.datasource.username=postgres
spring.datasource.password=admin
spring.datasource.driver-class-name=org.postgresql.Driver

#Queries
car.query.find.by.id=SELECT * FROM CAR WHERE ID = :id
car.query.delete.by.id=DELETE FROM CAR WHERE ID =:id
car.query.update=update CAR set name = :name, city = :city, model= :model, color =:color
  where id = :id
car.query.find.all=select * from CAR ORDER BY id LIMIT :limit OFFSET :offset
Veri tabanı cümlelerini bir sınıfa okuruz. Şöyle yaparız
@Component
public class CarQueries {

    @Value("${car.query.find.by.id}")
    private String findById;
    @Value("${car.query.delete.by.id}")
    private String deleteById;
    @Value("${car.query.update}")
    private String update;
    @Value("${car.query.find.all}")
    private String findAll;
    //...
}
Repository sınıfımız için constructor şöyledir
@Repository
public class CarDAO {

  private final NamedParameterJdbcTemplate template;
  private final CarQueries queries;
  private final RowMapper<Car> rowMapper;
  private final SimpleJdbcInsert insert;

  @Autowired
  public CarDAO(NamedParameterJdbcTemplate template, CarQueries queries) {
    this.template = template;
    //this.rowMapper = new BeanPropertyRowMapper<>(Car.class);
    this.rowMapper = new CarRowMapper();
    this.queries = queries;
    this.insert = new SimpleJdbcInsert(template.getJdbcTemplate());
    this.insert.setTableName("car");
    this.insert.usingGeneratedKeyColumns("id");
  }
}
Repository sınıfımızın metodları şöyledir
@Transactional
public Car insert(Car car) {
  //Number id = insert.executeAndReturnKey(new BeanPropertySqlParameterSource(car));
  Number id = insert.executeAndReturnKey(car.toMap());
  return findBy(id.longValue()).orElseThrow(() -> new IllegalStateException(""));
}
@Transactional
public Optional<Car> findBy(Long id) {
  String sql = queries.getFindById();
  Map<String, Object> parameters = Collections.singletonMap("id", id);
  return template.queryForStream(sql, parameters, rowMapper).findFirst();
}

@Transactional
public boolean delete(Long id) {
  String sql = queries.getDeleteById();
  Map<String, Object> paramMap = Collections.singletonMap("id", id);
  return template.update(sql, paramMap) == 1;
}

@Transactional
public boolean update(Car car) {
  String sql = queries.getUpdate();
  Map<String, Object> paramMap = car.toMap();
  return template.update(sql, paramMap) == 1;
}

@Transactional
public Stream<Car> findAll(Page page) {
  String sql = queries.getFindAll();
  Map<String, Object> paramMap = new HashMap<>();
  paramMap.put("limit", page.getLimit());
  paramMap.put("offset", page.getOffset());
  return template.queryForStream(sql, paramMap, rowMapper);
}

Hiç yorum yok:

Yorum Gönder