Bladeren bron

JPA portion?

Trinh Tong 6 jaren geleden
bovenliggende
commit
ea7da4d372

+ 16
- 0
src/main/java/io/zipcoder/persistenceapp/repositories/PersonRepository.java Bestand weergeven

@@ -0,0 +1,16 @@
1
+package io.zipcoder.persistenceapp.repositories;
2
+
3
+import io.zipcoder.persistenceapp.model.Person;
4
+import org.springframework.data.jpa.repository.JpaRepository;
5
+import org.springframework.stereotype.Repository;
6
+
7
+import java.util.List;
8
+
9
+@Repository
10
+public interface PersonRepository extends JpaRepository<Person, Integer> {
11
+
12
+    List<Person> findByMobile(String mobile);
13
+
14
+
15
+    List<Person> findByLastName(String lastName);
16
+}

+ 78
- 0
src/main/java/io/zipcoder/persistenceapp/service/JdbcPersonServiceImpl.java Bestand weergeven

@@ -0,0 +1,78 @@
1
+package io.zipcoder.persistenceapp.service;
2
+
3
+import io.zipcoder.persistenceapp.controller.mappers.PersonRowMapper;
4
+import io.zipcoder.persistenceapp.model.Person;
5
+import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.jdbc.core.BeanPropertyRowMapper;
7
+import org.springframework.jdbc.core.JdbcTemplate;
8
+import org.springframework.stereotype.Service;
9
+
10
+import java.sql.JDBCType;
11
+import java.util.List;
12
+
13
+@Service
14
+public class JdbcPersonServiceImpl implements PersonService {
15
+
16
+    private final JDBCType[] types = {JDBCType.VARCHAR, JDBCType.VARCHAR, JDBCType.VARCHAR, JDBCType.DATE, JDBCType.SMALLINT };
17
+
18
+    private final
19
+    JdbcTemplate jdbcTemplate;
20
+
21
+    @Autowired
22
+    public JdbcPersonServiceImpl(JdbcTemplate jdbcTemplate) {
23
+        this.jdbcTemplate = jdbcTemplate;
24
+    }
25
+
26
+    public void add(Person p) {
27
+        Object[] info = {p.getFirstName(), p.getLastName(), p.getMobile(), p.getBirthday(), p.getHomeId()};
28
+        jdbcTemplate.update(
29
+                "INSERT INTO person(first_name, last_name, mobile, birthday, home_ID)" +
30
+                        "VALUES (?, ?, ?, ?, ?)", info);
31
+    }
32
+
33
+    // Test this one
34
+    public void update(Person p) {
35
+        jdbcTemplate.update(
36
+                "UPDATE person set first_name = ?, last_name ?, mobile = ?, birthday = ?, home_ID = ?" +
37
+                        "VALUES (?, ?, ?, ?, ?) WHERE id = ?",
38
+                p.getFirstName(), p.getLastName(), p.getMobile(), p.getBirthday(), p.getHomeId(), p.getId());
39
+    }
40
+
41
+    public void remove(Integer id) {
42
+        jdbcTemplate.update("DELETE FROM person WHERE id = ?", id);
43
+    }
44
+
45
+    public void removePeople(Iterable<Person> p) {
46
+        p.forEach(person -> remove(person.getId()));
47
+    }
48
+
49
+    public Person findPersonById(Integer id) {
50
+        return jdbcTemplate.queryForObject("SELECT * FROM PERSON WHERE ID=?", new Object[] {id},
51
+                new BeanPropertyRowMapper<>(Person.class));
52
+    }
53
+
54
+    public List<Person> getAllPeople() {
55
+        return jdbcTemplate.query("SELECT * FROM PERSON", new PersonRowMapper());
56
+    }
57
+
58
+    public List<Person> findPeopleByMobile(String mobile) {
59
+        return jdbcTemplate.query("SELECT * FROM PERSON WHERE MOBILE=?", new Object[] {mobile},
60
+                new PersonRowMapper());
61
+    }
62
+
63
+    public List<Person> findPeopleByLastName(String lastName) {
64
+        return jdbcTemplate.query("SELECT * FROM PERSON WHERE LAST_NAME=?", new Object[] {lastName},
65
+                new PersonRowMapper());
66
+    }
67
+
68
+// public List<Person> generateSurnameMap(String lastName) {
69
+//        return jdbcTemplate.query("SELECT * FROM PERSON WHERE LAST_NAME=?", new Object[] {lastName},
70
+//                new PersonRowMapper());
71
+//    }
72
+
73
+/*
74
+implement the count for people with selected first name
75
+ */
76
+}
77
+
78
+

+ 56
- 0
src/main/java/io/zipcoder/persistenceapp/service/JpaPersonServiceImpl.java Bestand weergeven

@@ -0,0 +1,56 @@
1
+package io.zipcoder.persistenceapp.service;
2
+
3
+import io.zipcoder.persistenceapp.repositories.PersonRepository;
4
+import io.zipcoder.persistenceapp.model.Person;
5
+import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.stereotype.Service;
7
+
8
+import java.util.List;
9
+
10
+@Service
11
+public class JpaPersonServiceImpl implements PersonService {
12
+
13
+    @Autowired
14
+    PersonRepository personRepository;
15
+
16
+    @Override
17
+    public void add(Person p) {
18
+        personRepository.save(p);
19
+    }
20
+
21
+    @Override
22
+    public void update(Person p) {
23
+        personRepository.save(p);
24
+
25
+    }
26
+
27
+    @Override
28
+    public void remove(Integer id) {
29
+        personRepository.delete(id);
30
+    }
31
+
32
+    @Override
33
+    public Person findPersonById(Integer id) {
34
+        return personRepository.findOne(id);
35
+    }
36
+
37
+    @Override
38
+    public List<Person> getAllPeople() {
39
+        return personRepository.findAll();
40
+    }
41
+
42
+    @Override
43
+    public List<Person> findPeopleByMobile(String mobile) {
44
+        return personRepository.findByMobile(mobile);
45
+    }
46
+
47
+    @Override
48
+    public List<Person> findPeopleByLastName(String lastName) {
49
+        return personRepository.findByLastName(lastName);
50
+    }
51
+
52
+//    @Override
53
+//    public List<Person> generateSurnameMap(String lastName) {
54
+//        return null;
55
+//    }
56
+}

+ 9
- 71
src/main/java/io/zipcoder/persistenceapp/service/PersonService.java Bestand weergeven

@@ -1,80 +1,18 @@
1 1
 package io.zipcoder.persistenceapp.service;
2 2
 
3
-import io.zipcoder.persistenceapp.controller.mappers.PersonRowMapper;
4 3
 import io.zipcoder.persistenceapp.model.Person;
5
-import org.springframework.beans.factory.annotation.Autowired;
6
-import org.springframework.jdbc.core.BeanPropertyRowMapper;
7
-import org.springframework.jdbc.core.JdbcTemplate;
8
-import org.springframework.stereotype.Service;
9 4
 
10
-import java.sql.JDBCType;
11 5
 import java.util.List;
12 6
 
13
-/*
14
-Service speaks to the repo/jdbc template, controller speaks to service to get stuff
7
+public interface PersonService {
15 8
 
16
- */
17
-@Service
18
-public class PersonService {
9
+    void add(Person p);
10
+    void update(Person p);
11
+    void remove (Integer id);
12
+    Person findPersonById(Integer id);
13
+    List<Person> getAllPeople();
14
+    List<Person> findPeopleByMobile(String mobile);
15
+    List<Person> findPeopleByLastName(String lastName);
16
+//    List<Person> generateSurnameMap(String lastName);
19 17
 
20
-    private final JDBCType[] types = {JDBCType.VARCHAR, JDBCType.VARCHAR, JDBCType.VARCHAR, JDBCType.DATE, JDBCType.SMALLINT };
21
-
22
-    private final
23
-    JdbcTemplate jdbcTemplate;
24
-
25
-    @Autowired
26
-    public PersonService(JdbcTemplate jdbcTemplate) {
27
-        this.jdbcTemplate = jdbcTemplate;
28
-    }
29
-
30
-    public void add(Person p) {
31
-        Object[] info = {p.getFirstName(), p.getLastName(), p.getMobile(), p.getBirthday(), p.getHomeId()};
32
-        jdbcTemplate.update(
33
-                "INSERT INTO person(first_name, last_name, mobile, birthday, home_ID)" +
34
-                        "VALUES (?, ?, ?, ?, ?)", info);
35
-    }
36
-
37
-    // Test this one
38
-    public void update(Person p) {
39
-        jdbcTemplate.update(
40
-                "UPDATE person set first_name = ?, last_name ?, mobile = ?, birthday = ?, home_ID = ?" +
41
-                        "VALUES (?, ?, ?, ?, ?) WHERE id = ?",
42
-                p.getFirstName(), p.getLastName(), p.getMobile(), p.getBirthday(), p.getHomeId(), p.getId());
43
-    }
44
-
45
-    public void remove(Integer id) {
46
-        jdbcTemplate.update("DELETE FROM person WHERE id = ?", id);
47
-    }
48
-
49
-    public void removePeople(Iterable<Person> p) {
50
-        p.forEach(person -> remove(person.getId()));
51
-    }
52
-
53
-    public Person findPersonById(Integer id) {
54
-        return jdbcTemplate.queryForObject("SELECT * FROM PERSON WHERE ID=?", new Object[] {id},
55
-                new BeanPropertyRowMapper<>(Person.class));
56
-    }
57
-
58
-    public List<Person> getAllPeople() {
59
-        return jdbcTemplate.query("SELECT * FROM PERSON", new PersonRowMapper());
60
-    }
61
-
62
-    public List<Person> findPeopleByMobile(String mobile) {
63
-        return jdbcTemplate.query("SELECT * FROM PERSON WHERE MOBILE=?", new Object[] {mobile},
64
-                new PersonRowMapper());
65
-    }
66
-
67
-    public List<Person> findPeopleByLastName(String lastName) {
68
-        return jdbcTemplate.query("SELECT * FROM PERSON WHERE LAST_NAME=?", new Object[] {lastName},
69
-                new PersonRowMapper());
70
-    }
71
-
72
-    public List<Person> generateSurnameMap(String lastName) {
73
-        return jdbcTemplate.query("SELECT * FROM PERSON WHERE LAST_NAME=?", new Object[] {lastName},
74
-                new PersonRowMapper());
75
-    }
76
-
77
-    /*
78
-    implement the count for people with selected first name
79
-     */
80 18
 }