forked from JavaOPs/topjava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3_10_db_implementation.patch
More file actions
196 lines (195 loc) · 7.78 KB
/
3_10_db_implementation.patch
File metadata and controls
196 lines (195 loc) · 7.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
Index: src/main/java/ru/javawebinar/topjava/repository/jdbc/JdbcUserRepositoryImpl.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/main/java/ru/javawebinar/topjava/repository/jdbc/JdbcUserRepositoryImpl.java (date 1531053669342)
+++ src/main/java/ru/javawebinar/topjava/repository/jdbc/JdbcUserRepositoryImpl.java (date 1531053669342)
@@ -0,0 +1,82 @@
+package ru.javawebinar.topjava.repository.jdbc;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.dao.support.DataAccessUtils;
+import org.springframework.jdbc.core.BeanPropertyRowMapper;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
+import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
+import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
+import org.springframework.stereotype.Repository;
+import ru.javawebinar.topjava.model.User;
+import ru.javawebinar.topjava.repository.UserRepository;
+
+import javax.sql.DataSource;
+import java.util.List;
+
+@Repository
+public class JdbcUserRepositoryImpl implements UserRepository {
+
+ private static final BeanPropertyRowMapper<User> ROW_MAPPER = BeanPropertyRowMapper.newInstance(User.class);
+
+ private final JdbcTemplate jdbcTemplate;
+
+ private final NamedParameterJdbcTemplate namedParameterJdbcTemplate;
+
+ private final SimpleJdbcInsert insertUser;
+
+ @Autowired
+ public JdbcUserRepositoryImpl(DataSource dataSource, JdbcTemplate jdbcTemplate, NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
+ this.insertUser = new SimpleJdbcInsert(dataSource)
+ .withTableName("users")
+ .usingGeneratedKeyColumns("id");
+
+ this.jdbcTemplate = jdbcTemplate;
+ this.namedParameterJdbcTemplate = namedParameterJdbcTemplate;
+ }
+
+ @Override
+ public User save(User user) {
+ MapSqlParameterSource map = new MapSqlParameterSource()
+ .addValue("id", user.getId())
+ .addValue("name", user.getName())
+ .addValue("email", user.getEmail())
+ .addValue("password", user.getPassword())
+ .addValue("registered", user.getRegistered())
+ .addValue("enabled", user.isEnabled())
+ .addValue("caloriesPerDay", user.getCaloriesPerDay());
+
+ if (user.isNew()) {
+ Number newKey = insertUser.executeAndReturnKey(map);
+ user.setId(newKey.intValue());
+ } else if (namedParameterJdbcTemplate.update(
+ "UPDATE users SET name=:name, email=:email, password=:password, " +
+ "registered=:registered, enabled=:enabled, calories_per_day=:caloriesPerDay WHERE id=:id", map) == 0) {
+ return null;
+ }
+ return user;
+ }
+
+ @Override
+ public boolean delete(int id) {
+ return jdbcTemplate.update("DELETE FROM users WHERE id=?", id) != 0;
+ }
+
+ @Override
+ public User get(int id) {
+ List<User> users = jdbcTemplate.query("SELECT * FROM users WHERE id=?", ROW_MAPPER, id);
+ return DataAccessUtils.singleResult(users);
+ }
+
+ @Override
+ public User getByEmail(String email) {
+// return jdbcTemplate.queryForObject("SELECT * FROM users WHERE email=?", ROW_MAPPER, email);
+ List<User> users = jdbcTemplate.query("SELECT * FROM users WHERE email=?", ROW_MAPPER, email);
+ return DataAccessUtils.singleResult(users);
+ }
+
+ @Override
+ public List<User> getAll() {
+ return jdbcTemplate.query("SELECT * FROM users ORDER BY name, email", ROW_MAPPER);
+ }
+}
Index: src/main/resources/db/populateDB.sql
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/main/resources/db/populateDB.sql (date 1531053669358)
+++ src/main/resources/db/populateDB.sql (date 1531053669358)
@@ -0,0 +1,11 @@
+DELETE FROM user_roles;
+DELETE FROM users;
+ALTER SEQUENCE global_seq RESTART WITH 100000;
+
+INSERT INTO users (name, email, password) VALUES
+ ('User', 'user@yandex.ru', 'password'),
+ ('Admin', 'admin@gmail.com', 'admin');
+
+INSERT INTO user_roles (role, user_id) VALUES
+ ('ROLE_USER', 100000),
+ ('ROLE_ADMIN', 100001);
Index: src/main/resources/db/initDB.sql
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/main/resources/db/initDB.sql (date 1531053669352)
+++ src/main/resources/db/initDB.sql (date 1531053669352)
@@ -0,0 +1,25 @@
+DROP TABLE IF EXISTS user_roles;
+DROP TABLE IF EXISTS users;
+DROP SEQUENCE IF EXISTS global_seq;
+
+CREATE SEQUENCE global_seq START 100000;
+
+CREATE TABLE users
+(
+ id INTEGER PRIMARY KEY DEFAULT nextval('global_seq'),
+ name VARCHAR NOT NULL,
+ email VARCHAR NOT NULL,
+ password VARCHAR NOT NULL,
+ registered TIMESTAMP DEFAULT now() NOT NULL,
+ enabled BOOL DEFAULT TRUE NOT NULL,
+ calories_per_day INTEGER DEFAULT 2000 NOT NULL
+);
+CREATE UNIQUE INDEX users_unique_email_idx ON users (email);
+
+CREATE TABLE user_roles
+(
+ user_id INTEGER NOT NULL,
+ role VARCHAR,
+ CONSTRAINT user_roles_idx UNIQUE (user_id, role),
+ FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE
+);
\ No newline at end of file
Index: src/main/resources/spring/spring-db.xml
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/main/resources/spring/spring-db.xml (date 1531053669365)
+++ src/main/resources/spring/spring-db.xml (date 1531053669365)
@@ -0,0 +1,25 @@
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns:context="http://www.springframework.org/schema/context"
+ xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
+ http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
+
+ <context:property-placeholder location="classpath:db/postgres.properties" system-properties-mode="OVERRIDE"/>
+
+ <!--no pooling-->
+ <bean id="dataSource"
+ class="org.springframework.jdbc.datasource.DriverManagerDataSource">
+ <property name="driverClassName" value="org.postgresql.Driver"/>
+ <property name="url" value="${database.url}"/>
+ <property name="username" value="${database.username}"/>
+ <property name="password" value="${database.password}"/>
+ </bean>
+
+ <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
+ <constructor-arg ref="dataSource"/>
+ </bean>
+
+ <bean id="namedJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
+ <constructor-arg ref="dataSource"/>
+ </bean>
+</beans>
\ No newline at end of file
Index: pom.xml
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- pom.xml (date 1531053555000)
+++ pom.xml (date 1531053669375)
@@ -74,6 +74,11 @@
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-jdbc</artifactId>
+ <version>${spring.version}</version>
+ </dependency>
<!--DataBase-->
<dependency>