2021-11 spring-07-jdbc updated

This commit is contained in:
stvort
2021-12-15 09:00:47 +04:00
parent b5c7e261e1
commit 60ee7e3f75
54 changed files with 96 additions and 69 deletions
@@ -1,9 +0,0 @@
spring:
datasource:
url: jdbc:h2:mem:testdb
h2:
console:
path: /h2-console
settings:
web-allow-others: true
@@ -1,9 +0,0 @@
spring:
datasource:
url: jdbc:h2:mem:testdb
h2:
console:
path: /h2-console
settings:
web-allow-others: true
@@ -1,9 +0,0 @@
spring:
datasource:
url: jdbc:h2:mem:testdb
h2:
console:
path: /h2-console
settings:
web-allow-others: true
@@ -1,9 +0,0 @@
spring:
datasource:
url: jdbc:h2:mem:testdb
h2:
console:
path: /h2-console
settings:
web-allow-others: true
@@ -1,9 +0,0 @@
spring:
datasource:
url: jdbc:h2:mem:testdb
h2:
console:
path: /h2-console
settings:
web-allow-others: true
@@ -3,7 +3,6 @@ package ru.otus.spring.dao;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.stereotype.Repository;
@SuppressWarnings({"SqlNoDataSourceInspection", "ConstantConditions", "SqlDialectInspection"})
@Repository
public class PersonDaoJdbc implements PersonDao {
private final JdbcOperations jdbc;
@@ -0,0 +1,16 @@
spring:
datasource:
url: jdbc:h2:mem:testdb
#initialization-mode: always
#schema: schema.sql
#data: data.sql
sql:
init:
mode: always
data-locations: data.sql
schema-locations: schema.sql
h2:
console:
path: /h2-console
settings:
web-allow-others: true
@@ -2,9 +2,7 @@ package ru.otus.spring.dao;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.stereotype.Repository;
import ru.otus.spring.domain.Person;
@SuppressWarnings({"SqlNoDataSourceInspection", "ConstantConditions", "SqlDialectInspection"})
@Repository
public class PersonDaoJdbc implements PersonDao {
@@ -17,7 +15,8 @@ public class PersonDaoJdbc implements PersonDao {
@Override
public int count() {
return jdbc.queryForObject("select count(*) from persons", Integer.class);
Integer count = jdbc.queryForObject("select count(*) from persons", Integer.class);
return count == null? 0: count;
}
}
@@ -0,0 +1,16 @@
spring:
datasource:
url: jdbc:h2:mem:testdb
#initialization-mode: always
#schema: schema.sql
#data: data.sql
sql:
init:
mode: always
data-locations: data.sql
schema-locations: schema.sql
h2:
console:
path: /h2-console
settings:
web-allow-others: true
@@ -1,15 +1,10 @@
package ru.otus.spring.dao;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import ru.otus.spring.domain.Person;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
@SuppressWarnings({"SqlNoDataSourceInspection", "ConstantConditions", "SqlDialectInspection"})
@Repository
public class PersonDaoJdbc implements PersonDao {
@@ -22,12 +17,13 @@ public class PersonDaoJdbc implements PersonDao {
@Override
public int count() {
return jdbc.queryForObject("select count(*) from persons", Integer.class);
Integer count = jdbc.queryForObject("select count(*) from persons", Integer.class);
return count == null? 0: count;
}
@Override
public void insert(Person person) {
jdbc.update("insert into persons (id, `name`) values (?, ?)", person.getId(), person.getName());
jdbc.update("insert into persons (id, name) values (?, ?)", person.getId(), person.getName());
}
}
@@ -0,0 +1,16 @@
spring:
datasource:
url: jdbc:h2:mem:testdb
#initialization-mode: always
#schema: schema.sql
#data: data.sql
sql:
init:
mode: always
data-locations: data.sql
schema-locations: schema.sql
h2:
console:
path: /h2-console
settings:
web-allow-others: true
@@ -2,17 +2,13 @@ package ru.otus.spring.dao;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import org.springframework.stereotype.Repository;
import ru.otus.spring.domain.Person;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@SuppressWarnings({"SqlNoDataSourceInspection", "ConstantConditions", "SqlDialectInspection"})
@Repository
public class PersonDaoJdbc implements PersonDao {
@@ -25,22 +21,23 @@ public class PersonDaoJdbc implements PersonDao {
@Override
public int count() {
return jdbc.queryForObject("select count(*) from persons", Integer.class);
Integer count = jdbc.queryForObject("select count(*) from persons", Integer.class);
return count == null? 0: count;
}
@Override
public void insert(Person person) {
jdbc.update("insert into persons (id, `name`) values (?, ?)", person.getId(), person.getName());
jdbc.update("insert into persons (id, name) values (?, ?)", person.getId(), person.getName());
}
@Override
public Person getById(long id) {
return jdbc.queryForObject("select * from persons where id = ?", new Object[] {id}, new PersonMapper());
return jdbc.queryForObject("select id, name from persons where id = ?", new PersonMapper(), id);
}
@Override
public List<Person> getAll() {
return jdbc.query("select * from persons", new PersonMapper());
return jdbc.query("select id, name from persons", new PersonMapper());
}
@@ -0,0 +1,16 @@
spring:
datasource:
url: jdbc:h2:mem:testdb
#initialization-mode: always
#schema: schema.sql
#data: data.sql
sql:
init:
mode: always
data-locations: data.sql
schema-locations: schema.sql
h2:
console:
path: /h2-console
settings:
web-allow-others: true
@@ -12,7 +12,6 @@ import java.util.Collections;
import java.util.List;
import java.util.Map;
@SuppressWarnings({"SqlNoDataSourceInspection", "ConstantConditions", "SqlDialectInspection"})
@Repository
public class PersonDaoJdbc implements PersonDao {
@@ -29,12 +28,13 @@ public class PersonDaoJdbc implements PersonDao {
@Override
public int count() {
return jdbc.queryForObject("select count(*) from persons", Integer.class);
Integer count = jdbc.queryForObject("select count(*) from persons", Integer.class);
return count == null? 0: count;
}
@Override
public void insert(Person person) {
namedParameterJdbcOperations.update("insert into persons (id, `name`) values (:id, :name)",
namedParameterJdbcOperations.update("insert into persons (id, name) values (:id, :name)",
Map.of("id", person.getId(), "name", person.getName()));
}
@@ -42,13 +42,13 @@ public class PersonDaoJdbc implements PersonDao {
public Person getById(long id) {
Map<String, Object> params = Collections.singletonMap("id", id);
return namedParameterJdbcOperations.queryForObject(
"select * from persons where id = :id", params, new PersonMapper()
"select id, name from persons where id = :id", params, new PersonMapper()
);
}
@Override
public List<Person> getAll() {
return jdbc.query("select * from persons", new PersonMapper());
return jdbc.query("select id, name from persons", new PersonMapper());
}
@Override
@@ -0,0 +1,17 @@
spring:
datasource:
url: jdbc:h2:mem:testdb
#initialization-mode: always
#schema: schema.sql
#data: data.sql
sql:
init:
mode: always
data-locations: data.sql
schema-locations: schema.sql
h2:
console:
path: /h2-console
settings:
web-allow-others: true