|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+package io.zipcoder.persistenceapp;
|
|
|
2
|
+
|
|
|
3
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
4
|
+import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
5
|
+import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
|
|
|
6
|
+import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
|
|
|
7
|
+import org.springframework.jdbc.core.namedparam.SqlParameterSource;
|
|
|
8
|
+import org.springframework.jdbc.support.GeneratedKeyHolder;
|
|
|
9
|
+import org.springframework.jdbc.support.KeyHolder;
|
|
|
10
|
+import org.springframework.stereotype.Service;
|
|
|
11
|
+
|
|
|
12
|
+import java.util.ArrayList;
|
|
|
13
|
+import java.util.HashMap;
|
|
|
14
|
+import java.util.List;
|
|
|
15
|
+import java.util.Map;
|
|
|
16
|
+
|
|
|
17
|
+@Service
|
|
|
18
|
+public class PersonService {
|
|
|
19
|
+
|
|
|
20
|
+ @Autowired
|
|
|
21
|
+ private JdbcTemplate jdbcTemplate;
|
|
|
22
|
+
|
|
|
23
|
+ @Autowired
|
|
|
24
|
+ private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
|
|
|
25
|
+
|
|
|
26
|
+
|
|
|
27
|
+ public List<Person> getAllPeople(){
|
|
|
28
|
+ List<Map<String, Object>> list = jdbcTemplate.queryForList("SELECT * FROM PERSON");
|
|
|
29
|
+ List<Person> people = new ArrayList<>();
|
|
|
30
|
+ for (Map<String, Object> row : list) {
|
|
|
31
|
+ Person person = new Person(String.valueOf(row.get("FIRST_NAME")), String.valueOf(row.get("LAST_NAME")), String.valueOf(row.get("MOBILE")), row.get("BIRTHDAY").toString(), (Short)row.get("HOME_ID"));
|
|
|
32
|
+ person.setId((Integer)row.get("ID"));
|
|
|
33
|
+ people.add(person);
|
|
|
34
|
+ }
|
|
|
35
|
+ return people;
|
|
|
36
|
+ }
|
|
|
37
|
+
|
|
|
38
|
+ public int savePerson(Person e){
|
|
|
39
|
+ System.out.println(e.toString());
|
|
|
40
|
+ KeyHolder holder = new GeneratedKeyHolder();
|
|
|
41
|
+ SqlParameterSource parameters = new MapSqlParameterSource()
|
|
|
42
|
+ .addValue("first_name", e.getFirstName())
|
|
|
43
|
+ .addValue("last_name", e.getLastName())
|
|
|
44
|
+ .addValue("mobile", e.getMobile())
|
|
|
45
|
+ .addValue("birthday", e.getBirthday())
|
|
|
46
|
+ .addValue("home_id", e.getHomeId());
|
|
|
47
|
+
|
|
|
48
|
+ namedParameterJdbcTemplate.update("INSERT INTO PERSON (FIRST_NAME, LAST_NAME, MOBILE, BIRTHDAY, HOME_ID) VALUES (:first_name, :last_name, :mobile, :birthday, :home_id);", parameters, holder);
|
|
|
49
|
+ return holder.getKey().intValue();
|
|
|
50
|
+ }
|
|
|
51
|
+
|
|
|
52
|
+ public int updatePerson(Person e){
|
|
|
53
|
+ return jdbcTemplate.update("UPDATE PERSON SET FIRST_NAME = '" +
|
|
|
54
|
+ e.getFirstName() + "', LAST_NAME = '" +
|
|
|
55
|
+ e.getLastName() + "', MOBILE = '" +
|
|
|
56
|
+ e.getMobile() + "', BIRTHDAY = '" +
|
|
|
57
|
+ e.getBirthday() + "', HOME_ID = '" +
|
|
|
58
|
+ e.getHomeId() + "' where id = '" + e.getId() + "');");
|
|
|
59
|
+ }
|
|
|
60
|
+
|
|
|
61
|
+ public int deletePerson(Person e){
|
|
|
62
|
+ return jdbcTemplate.update("DELETE FROM PERSON where ID = " + e.getId() + ";");
|
|
|
63
|
+ }
|
|
|
64
|
+
|
|
|
65
|
+ public void removeList(Person... e){
|
|
|
66
|
+ for(Person person : e){
|
|
|
67
|
+ deletePerson(person);
|
|
|
68
|
+ }
|
|
|
69
|
+ }
|
|
|
70
|
+
|
|
|
71
|
+ public List<Person> findAllByFirstName(String firstName, Class objectType){
|
|
|
72
|
+ return jdbcTemplate.queryForList("SELECT * FROM PERSON WHERE FIRST_NAME = '" + firstName + "';", Person.class);
|
|
|
73
|
+ }
|
|
|
74
|
+
|
|
|
75
|
+ public Person findById(int ID){
|
|
|
76
|
+ return jdbcTemplate.queryForObject("SELECT * FROM PERSON WHERE ID = " + ID + ";", Person.class);
|
|
|
77
|
+ }
|
|
|
78
|
+
|
|
|
79
|
+ public Map<String, List<Person>> surnameQuery(){
|
|
|
80
|
+ List<Person> people = getAllPeople();
|
|
|
81
|
+ Map<String, List<Person>> map = new HashMap<>();
|
|
|
82
|
+ for(Person person : people){
|
|
|
83
|
+ if(map.containsKey(person.getLastName())){
|
|
|
84
|
+ map.get(person.getLastName()).add(person);
|
|
|
85
|
+ } else {
|
|
|
86
|
+ map.put(person.getLastName(), new ArrayList<Person>());
|
|
|
87
|
+ map.get(person.getLastName()).add(person);
|
|
|
88
|
+ }
|
|
|
89
|
+ }
|
|
|
90
|
+ return map;
|
|
|
91
|
+ }
|
|
|
92
|
+
|
|
|
93
|
+ public Map<String, Integer> firstNameCount(){
|
|
|
94
|
+ List<Person> people = getAllPeople();
|
|
|
95
|
+ Map<String, Integer> map = new HashMap<>();
|
|
|
96
|
+ for(Person person : people){
|
|
|
97
|
+ if(map.containsKey(person.getFirstName())){
|
|
|
98
|
+ int amount = map.get(person.getFirstName());
|
|
|
99
|
+ amount = amount + 1;
|
|
|
100
|
+ map.put(person.getFirstName(), amount);
|
|
|
101
|
+ } else {
|
|
|
102
|
+ map.put(person.getFirstName(), 1);
|
|
|
103
|
+ }
|
|
|
104
|
+ }
|
|
|
105
|
+ return map;
|
|
|
106
|
+ }
|
|
|
107
|
+}
|