Ş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.urlspring.datasource.usernamespring.datasource.passwordSpring boot will automatically create DataSource and JdbcTemplate bean.
application.properties şöyledir
Veri tabanı cümlelerini bir sınıfa okuruz. Şöyle yaparız#databasespring.datasource.url=jdbc:postgresql://localhost:5432/carsspring.datasource.username=postgresspring.datasource.password=adminspring.datasource.driver-class-name=org.postgresql.Driver#Queriescar.query.find.by.id=SELECT * FROM CAR WHERE ID = :idcar.query.delete.by.id=DELETE FROM CAR WHERE ID =:idcar.query.update=update CAR set name = :name, city = :city, model= :model, color =:color
where id = :idcar.query.find.all=select * from CAR ORDER BY id LIMIT :limit OFFSET :offset
@Componentpublic 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
@Repositorypublic class CarDAO {private final NamedParameterJdbcTemplate template;private final CarQueries queries;private final RowMapper<Car> rowMapper;private final SimpleJdbcInsert insert;@Autowiredpublic 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
@Transactionalpublic Car insert(Car car) {//Number id = insert.executeAndReturnKey(new BeanPropertySqlParameterSource(car));Number id = insert.executeAndReturnKey(car.toMap());return findBy(id.longValue()).orElseThrow(() -> new IllegalStateException(""));}@Transactionalpublic Optional<Car> findBy(Long id) {String sql = queries.getFindById();Map<String, Object> parameters = Collections.singletonMap("id", id);return template.queryForStream(sql, parameters, rowMapper).findFirst();}@Transactionalpublic boolean delete(Long id) {String sql = queries.getDeleteById();Map<String, Object> paramMap = Collections.singletonMap("id", id);return template.update(sql, paramMap) == 1;}@Transactionalpublic boolean update(Car car) {String sql = queries.getUpdate();Map<String, Object> paramMap = car.toMap();return template.update(sql, paramMap) == 1;}@Transactionalpublic 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