mirror of
https://github.com/OtusTeam/Spring.git
synced 2026-05-30 10:50:42 +00:00
Spring 2019-11 07 lesson classwork edited
This commit is contained in:
+7
@@ -1,8 +1,15 @@
|
||||
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;
|
||||
|
||||
public PersonDaoJdbc(JdbcOperations jdbcOperations)
|
||||
{
|
||||
this.jdbc = jdbcOperations;
|
||||
}
|
||||
}
|
||||
|
||||
+3
-3
@@ -2,15 +2,15 @@ package ru.otus.spring.domain;
|
||||
|
||||
public class Person {
|
||||
|
||||
private final int id;
|
||||
private final long id;
|
||||
private final String name;
|
||||
|
||||
public Person(int id, String name) {
|
||||
public Person(long id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
DROP TABLE IF EXISTS PERSONS;
|
||||
CREATE TABLE PERSONS(ID INT PRIMARY KEY, NAME VARCHAR(255));
|
||||
CREATE TABLE PERSONS(ID BIGINT PRIMARY KEY, NAME VARCHAR(255));
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>ru.otus</groupId>
|
||||
<artifactId>jdbc-demo-solution-step-1</artifactId>
|
||||
<artifactId>jdbc-demo-solution-3</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
+3
-3
@@ -2,15 +2,15 @@ package ru.otus.spring.domain;
|
||||
|
||||
public class Person {
|
||||
|
||||
private final int id;
|
||||
private final long id;
|
||||
private final String name;
|
||||
|
||||
public Person(int id, String name) {
|
||||
public Person(long id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
DROP TABLE IF EXISTS PERSONS;
|
||||
CREATE TABLE PERSONS(ID BIGINT PRIMARY KEY, NAME VARCHAR(255));
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>ru.otus</groupId>
|
||||
<artifactId>jdbc-demo-solution-step-2</artifactId>
|
||||
<artifactId>jdbc-demo-solution-4</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
+3
-3
@@ -2,15 +2,15 @@ package ru.otus.spring.domain;
|
||||
|
||||
public class Person {
|
||||
|
||||
private final int id;
|
||||
private final long id;
|
||||
private final String name;
|
||||
|
||||
public Person(int id, String name) {
|
||||
public Person(long id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
DROP TABLE IF EXISTS PERSONS;
|
||||
CREATE TABLE PERSONS(ID BIGINT PRIMARY KEY, NAME VARCHAR(255));
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>ru.otus</groupId>
|
||||
<artifactId>jdbc-demo-solution-step-3</artifactId>
|
||||
<artifactId>jdbc-demo-solution-5</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
+1
-1
@@ -10,7 +10,7 @@ public interface PersonDao {
|
||||
|
||||
void insert(Person person);
|
||||
|
||||
Person getById(int id);
|
||||
Person getById(long id);
|
||||
|
||||
List<Person> getAll();
|
||||
}
|
||||
+2
-2
@@ -34,7 +34,7 @@ public class PersonDaoJdbc implements PersonDao {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Person getById(int id) {
|
||||
public Person getById(long id) {
|
||||
return jdbc.queryForObject("select * from persons where id = ?", new Object[] {id}, new PersonMapper());
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ public class PersonDaoJdbc implements PersonDao {
|
||||
|
||||
@Override
|
||||
public Person mapRow(ResultSet resultSet, int i) throws SQLException {
|
||||
int id = resultSet.getInt("id");
|
||||
long id = resultSet.getLong("id");
|
||||
String name = resultSet.getString("name");
|
||||
return new Person(id, name);
|
||||
}
|
||||
+3
-3
@@ -2,15 +2,15 @@ package ru.otus.spring.domain;
|
||||
|
||||
public class Person {
|
||||
|
||||
private final int id;
|
||||
private final long id;
|
||||
private final String name;
|
||||
|
||||
public Person(int id, String name) {
|
||||
public Person(long id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
DROP TABLE IF EXISTS PERSONS;
|
||||
CREATE TABLE PERSONS(ID BIGINT PRIMARY KEY, NAME VARCHAR(255));
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>ru.otus</groupId>
|
||||
<artifactId>jdbc-demo-solution-step-4</artifactId>
|
||||
<artifactId>jdbc-demo-solution-final</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
+2
-2
@@ -10,9 +10,9 @@ public interface PersonDao {
|
||||
|
||||
void insert(Person person);
|
||||
|
||||
Person getById(int id);
|
||||
Person getById(long id);
|
||||
|
||||
List<Person> getAll();
|
||||
|
||||
void deleteById(int id);
|
||||
void deleteById(long id);
|
||||
}
|
||||
+3
-3
@@ -38,7 +38,7 @@ public class PersonDaoJdbc implements PersonDao {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Person getById(int id) {
|
||||
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()
|
||||
@@ -51,7 +51,7 @@ public class PersonDaoJdbc implements PersonDao {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteById(int id) {
|
||||
public void deleteById(long id) {
|
||||
Map<String, Object> params = Collections.singletonMap("id", id);
|
||||
namedParameterJdbcOperations.update(
|
||||
"delete from persons where id = :id", params
|
||||
@@ -62,7 +62,7 @@ public class PersonDaoJdbc implements PersonDao {
|
||||
|
||||
@Override
|
||||
public Person mapRow(ResultSet resultSet, int i) throws SQLException {
|
||||
int id = resultSet.getInt("id");
|
||||
long id = resultSet.getLong("id");
|
||||
String name = resultSet.getString("name");
|
||||
return new Person(id, name);
|
||||
}
|
||||
+3
-3
@@ -2,15 +2,15 @@ package ru.otus.spring.domain;
|
||||
|
||||
public class Person {
|
||||
|
||||
private final int id;
|
||||
private final long id;
|
||||
private final String name;
|
||||
|
||||
public Person(int id, String name) {
|
||||
public Person(long id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
DROP TABLE IF EXISTS PERSONS;
|
||||
CREATE TABLE PERSONS(ID BIGINT PRIMARY KEY, NAME VARCHAR(255));
|
||||
-20
@@ -1,20 +0,0 @@
|
||||
package ru.otus.spring;
|
||||
|
||||
import org.h2.tools.Console;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import ru.otus.spring.dao.PersonDao;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
ApplicationContext context = SpringApplication.run(Main.class);
|
||||
|
||||
PersonDao dao = context.getBean(PersonDao.class);
|
||||
|
||||
Console.main(args);
|
||||
}
|
||||
}
|
||||
-4
@@ -1,4 +0,0 @@
|
||||
package ru.otus.spring.dao;
|
||||
|
||||
public interface PersonDao {
|
||||
}
|
||||
-17
@@ -1,17 +0,0 @@
|
||||
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;
|
||||
|
||||
public PersonDaoJdbc(JdbcOperations jdbcOperations)
|
||||
{
|
||||
this.jdbc = jdbcOperations;
|
||||
}
|
||||
|
||||
}
|
||||
-2
@@ -1,2 +0,0 @@
|
||||
DROP TABLE IF EXISTS PERSONS;
|
||||
CREATE TABLE PERSONS(ID INT PRIMARY KEY, NAME VARCHAR(255));
|
||||
-2
@@ -1,2 +0,0 @@
|
||||
DROP TABLE IF EXISTS PERSONS;
|
||||
CREATE TABLE PERSONS(ID INT PRIMARY KEY, NAME VARCHAR(255));
|
||||
-2
@@ -1,2 +0,0 @@
|
||||
DROP TABLE IF EXISTS PERSONS;
|
||||
CREATE TABLE PERSONS(ID INT PRIMARY KEY, NAME VARCHAR(255));
|
||||
-2
@@ -1,2 +0,0 @@
|
||||
DROP TABLE IF EXISTS PERSONS;
|
||||
CREATE TABLE PERSONS(ID INT PRIMARY KEY, NAME VARCHAR(255));
|
||||
@@ -1,4 +0,0 @@
|
||||
.idea/
|
||||
*.iml
|
||||
|
||||
target/
|
||||
@@ -1,46 +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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>ru.otus</groupId>
|
||||
<artifactId>jdbc-demo-solution-step-final</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.2.1.RELEASE</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-jdbc</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>1.4.200</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
-20
@@ -1,20 +0,0 @@
|
||||
package ru.otus.spring.domain;
|
||||
|
||||
public class Person {
|
||||
|
||||
private final int id;
|
||||
private final String name;
|
||||
|
||||
public Person(int id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
-1
@@ -1 +0,0 @@
|
||||
insert into persons (id, `name`) values (1, 'masha');
|
||||
-2
@@ -1,2 +0,0 @@
|
||||
DROP TABLE IF EXISTS PERSONS;
|
||||
CREATE TABLE PERSONS(ID INT PRIMARY KEY, NAME VARCHAR(255));
|
||||
@@ -12,10 +12,9 @@
|
||||
|
||||
<modules>
|
||||
<module>jdbc-demo-exercise</module>
|
||||
<module>jdbc-demo-solution-step-1</module>
|
||||
<module>jdbc-demo-solution-step-2</module>
|
||||
<module>jdbc-demo-solution-step-3</module>
|
||||
<module>jdbc-demo-solution-step-4</module>
|
||||
<module>jdbc-demo-solution-step-final</module>
|
||||
<module>jdbc-demo-solution-3</module>
|
||||
<module>jdbc-demo-solution-4</module>
|
||||
<module>jdbc-demo-solution-5</module>
|
||||
<module>jdbc-demo-solution-final</module>
|
||||
</modules>
|
||||
</project>
|
||||
|
||||
Reference in New Issue
Block a user