|
@@ -1,106 +1,9 @@
|
1
|
1
|
package com.example.demo.user;
|
2
|
2
|
|
3
|
3
|
|
4
|
|
-import com.example.demo.DemoDataSource;
|
5
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
6
|
|
-import org.springframework.stereotype.Service;
|
|
4
|
+import org.springframework.data.repository.CrudRepository;
|
|
5
|
+import org.springframework.stereotype.Repository;
|
7
|
6
|
|
8
|
|
-import java.sql.Connection;
|
9
|
|
-import java.sql.PreparedStatement;
|
10
|
|
-import java.sql.ResultSet;
|
11
|
|
-import java.sql.SQLException;
|
12
|
|
-import java.util.ArrayList;
|
13
|
|
-import java.util.List;
|
14
|
|
-
|
15
|
|
-@Service
|
16
|
|
-public class UserRepository {
|
17
|
|
- @Autowired
|
18
|
|
- private DemoDataSource dataSource;
|
19
|
|
-
|
20
|
|
- public void save(User user){
|
21
|
|
- String sql = "INSERT INTO user (name, active) VALUES (?, ?)";
|
22
|
|
- Connection conn = null;
|
23
|
|
-
|
24
|
|
- try {
|
25
|
|
- conn = dataSource.getConnection();
|
26
|
|
- PreparedStatement ps = conn.prepareStatement(sql);
|
27
|
|
- ps.setString(1, user.getName());
|
28
|
|
- ps.setBoolean(2, user.isActive());
|
29
|
|
- ps.execute();
|
30
|
|
- ps.close();
|
31
|
|
- } catch (SQLException e) {
|
32
|
|
- throw new RuntimeException(e);
|
33
|
|
- } finally {
|
34
|
|
- if (conn != null) {
|
35
|
|
- try {
|
36
|
|
- conn.close();
|
37
|
|
- } catch (SQLException e) {}
|
38
|
|
- }
|
39
|
|
- }
|
40
|
|
- }
|
41
|
|
-
|
42
|
|
- public User findById(Long id) {
|
43
|
|
-
|
44
|
|
- String sql = "SELECT * FROM user WHERE id = ?";
|
45
|
|
-
|
46
|
|
- Connection conn = null;
|
47
|
|
-
|
48
|
|
- try {
|
49
|
|
- conn = dataSource.getConnection();
|
50
|
|
- PreparedStatement ps = conn.prepareStatement(sql);
|
51
|
|
- ps.setLong(1, id);
|
52
|
|
- User user = null;
|
53
|
|
- ResultSet resultSet = ps.executeQuery();
|
54
|
|
- if (resultSet.next()) {
|
55
|
|
- user = new User();
|
56
|
|
- user.setId(resultSet.getLong("id"));
|
57
|
|
- user.setActive(resultSet.getBoolean("active"));
|
58
|
|
- user.setName(resultSet.getString("name"));
|
59
|
|
-
|
60
|
|
- }
|
61
|
|
- resultSet.close();
|
62
|
|
- ps.close();
|
63
|
|
- return user;
|
64
|
|
- } catch (SQLException e) {
|
65
|
|
- throw new RuntimeException(e);
|
66
|
|
- } finally {
|
67
|
|
- if (conn != null) {
|
68
|
|
- try {
|
69
|
|
- conn.close();
|
70
|
|
- } catch (SQLException e) {}
|
71
|
|
- }
|
72
|
|
- }
|
73
|
|
- }
|
74
|
|
-
|
75
|
|
- public List<User> findAll() {
|
76
|
|
- String sql = "SELECT * FROM user";
|
77
|
|
- List<User> users = new ArrayList<>();
|
78
|
|
-
|
79
|
|
- Connection conn = null;
|
80
|
|
-
|
81
|
|
- try {
|
82
|
|
- conn = dataSource.getConnection();
|
83
|
|
- PreparedStatement ps = conn.prepareStatement(sql);
|
84
|
|
- ResultSet resultSet = ps.executeQuery();
|
85
|
|
- while (resultSet.next()) {
|
86
|
|
- User user = new User();
|
87
|
|
- user.setId(resultSet.getLong(1));
|
88
|
|
- user.setActive(resultSet.getBoolean(2));
|
89
|
|
- user.setName(resultSet.getString(3));
|
90
|
|
-
|
91
|
|
- users.add(user);
|
92
|
|
- }
|
93
|
|
- resultSet.close();
|
94
|
|
- ps.close();
|
95
|
|
- } catch (SQLException e) {
|
96
|
|
- throw new RuntimeException(e);
|
97
|
|
- } finally {
|
98
|
|
- if (conn != null) {
|
99
|
|
- try {
|
100
|
|
- conn.close();
|
101
|
|
- } catch (SQLException e) {}
|
102
|
|
- }
|
103
|
|
- }
|
104
|
|
- return users;
|
105
|
|
- }
|
|
7
|
+@Repository
|
|
8
|
+public interface UserRepository extends CrudRepository<User, Long> {
|
106
|
9
|
}
|