Sfoglia il codice sorgente

changed to use crudrepo. removed unused files

Nhu Nguyen 6 anni fa
parent
commit
1ec720ba87

+ 0
- 14
src/main/java/com/example/demo/DemoDataSource.java Vedi File

@@ -1,14 +0,0 @@
1
-package com.example.demo;
2
-
3
-import org.springframework.jdbc.datasource.DriverManagerDataSource;
4
-import org.springframework.stereotype.Component;
5
-import org.springframework.stereotype.Service;
6
-
7
-@Service
8
-public class DemoDataSource extends DriverManagerDataSource {
9
-
10
-    public DemoDataSource(){
11
-        super("jdbc:mysql://localhost:3306/demo1", "root", "root");
12
-        this.setDriverClassName("com.mysql.jdbc.Driver");
13
-    }
14
-}

+ 0
- 53
src/main/java/com/example/demo/user/UserController.java Vedi File

@@ -1,53 +0,0 @@
1
-package com.example.demo.user;
2
-
3
-import org.springframework.beans.factory.annotation.Autowired;
4
-import org.springframework.http.HttpStatus;
5
-import org.springframework.http.ResponseEntity;
6
-import org.springframework.stereotype.Controller;
7
-import org.springframework.ui.Model;
8
-import org.springframework.web.bind.annotation.*;
9
-
10
-import java.util.ArrayList;
11
-import java.util.List;
12
-import java.util.Map;
13
-import java.util.stream.Collector;
14
-import java.util.stream.Collectors;
15
-
16
-//change this to RestController
17
-@RestController
18
-@RequestMapping(path="/users")
19
-public class UserController {
20
-
21
-    @Autowired
22
-    private UserRepository repository;
23
-
24
-    //Don't need this anymore
25
-//    @GetMapping(path="/new")
26
-//    public String newUser(Model model){
27
-//        model.addAttribute("user", new User());
28
-//        return "users/new";
29
-//    }
30
-
31
-    // change @ModelAttribute to @RequestBody
32
-    @PostMapping
33
-    public String create(@RequestBody User user){
34
-        repository.save(user);
35
-        return "Created";
36
-    }
37
-
38
-    //change this to return user instead of template name
39
-    @RequestMapping(value="/{id}")
40
-    public User get(@PathVariable long id){
41
-        return repository.findById(id);
42
-    }
43
-
44
-//    change this to return list instead of template name
45
-    @GetMapping
46
-    public List<User> index(){
47
-        return repository.findAll();
48
-    }
49
-
50
-
51
-
52
-
53
-}

+ 4
- 101
src/main/java/com/example/demo/user/UserRepository.java Vedi File

@@ -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
 }

+ 0
- 0
src/main/resources/static/style.css Vedi File


+ 0
- 13
src/main/resources/templates/users/index.html Vedi File

@@ -1,13 +0,0 @@
1
-<h2>Users</h2>
2
-
3
-<ul>
4
-<li th:each="user: ${allUsers}">
5
-    <a th:href="@{'/users/' + ${user.id}}">
6
-
7
-        <span th:text="${user.id}"></span>
8
-        <span th:text="${user.name}"></span>
9
-    </a>
10
-</li>
11
-</ul>
12
-
13
-<a href="/users/new">New User</a>

+ 0
- 6
src/main/resources/templates/users/new.html Vedi File

@@ -1,6 +0,0 @@
1
-<h2>New User</h2>
2
-
3
-<form action="#" th:action="@{/users}" th:object="${user}" method="post">
4
-    <p>Name: <input type="text" th:field="*{name}" /></p>
5
-    <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
6
-</form>

+ 0
- 9
src/main/resources/templates/users/show.html Vedi File

@@ -1,9 +0,0 @@
1
-<h2>User details</h2>
2
-<div>
3
-    <label>ID:</label><span th:text="${user.id}"></span>
4
-</div>
5
-<div>
6
-    <label>ID:</label><span th:text="${user.name}"></span>
7
-</div>
8
-
9
-<a href="/users">All Users</a>