Trinh Tong 6 years ago
parent
commit
03f9f195da

+ 18
- 0
src/main/java/io/zipcoder/persistenceapp/controller/mappers/HomeRowMapper.java View File

@@ -0,0 +1,18 @@
1
+package io.zipcoder.persistenceapp.controller.mappers;
2
+
3
+import io.zipcoder.persistenceapp.model.Home;
4
+import org.springframework.jdbc.core.RowMapper;
5
+
6
+import java.sql.ResultSet;
7
+import java.sql.SQLException;
8
+
9
+public class HomeRowMapper implements RowMapper<Home> {
10
+    @Override
11
+    public Home mapRow(ResultSet rs, int rowNum) throws SQLException {
12
+        Home home = new Home();
13
+        home.setAddress(rs.getString("ADDRESS"));
14
+        home.setHomeNumber(rs.getString("HOMENUMBER"));
15
+        home.setId(rs.getInt("id"));
16
+        return home;
17
+    }
18
+}

src/main/java/io/zipcoder/persistenceapp/controller/PersonRowMapper.java → src/main/java/io/zipcoder/persistenceapp/controller/mappers/PersonRowMapper.java View File

@@ -1,4 +1,4 @@
1
-package io.zipcoder.persistenceapp.controller;
1
+package io.zipcoder.persistenceapp.controller.mappers;
2 2
 
3 3
 import io.zipcoder.persistenceapp.model.Person;
4 4
 import org.springframework.jdbc.core.RowMapper;
@@ -6,7 +6,7 @@ import org.springframework.jdbc.core.RowMapper;
6 6
 import java.sql.ResultSet;
7 7
 import java.sql.SQLException;
8 8
 
9
-class PersonRowMapper implements RowMapper<Person> {
9
+public class PersonRowMapper implements RowMapper<Person> {
10 10
     @Override
11 11
     public Person mapRow(ResultSet rs, int rowNum) throws SQLException {
12 12
         Person person = new Person();

+ 5
- 18
src/main/java/io/zipcoder/persistenceapp/service/HomeService.java View File

@@ -1,15 +1,12 @@
1 1
 package io.zipcoder.persistenceapp.service;
2 2
 
3
+import io.zipcoder.persistenceapp.controller.mappers.HomeRowMapper;
4
+import io.zipcoder.persistenceapp.controller.mappers.PersonRowMapper;
3 5
 import io.zipcoder.persistenceapp.model.Home;
4 6
 import io.zipcoder.persistenceapp.model.Person;
5 7
 import org.springframework.beans.factory.annotation.Autowired;
6
-import org.springframework.context.annotation.Bean;
7 8
 import org.springframework.jdbc.core.BeanPropertyRowMapper;
8 9
 import org.springframework.jdbc.core.JdbcTemplate;
9
-import org.springframework.jdbc.core.RowMapper;
10
-
11
-import java.sql.ResultSet;
12
-import java.sql.SQLException;
13 10
 import java.util.List;
14 11
 
15 12
 public class HomeService {
@@ -23,17 +20,6 @@ public class HomeService {
23 20
     public HomeService() {
24 21
     }
25 22
 
26
-    class HomeRowMapper implements RowMapper<Home> {
27
-        @Override
28
-        public Home mapRow(ResultSet rs, int rowNum) throws SQLException {
29
-            Home home = new Home();
30
-            home.setAddress(rs.getString("ADDRESS"));
31
-            home.setHomeNumber(rs.getString("HOMENUMBER"));
32
-            home.setId(rs.getInt("id"));
33
-            return home;
34
-        }
35
-    }
36
-
37 23
     public void addHome(Home home) {
38 24
         jdbcTemplate.update(
39 25
                 "INSERT INTO home(address, homenumber)" +
@@ -70,7 +56,7 @@ public class HomeService {
70 56
     public Home findHomeById(Integer id) {
71 57
         return jdbcTemplate.queryForObject(
72 58
                 "SELECT * FROM home WHERE ID = ?",
73
-                new Object[] {id}, new BeanPropertyRowMapper<>(Home.class));
59
+                new Object[]{id}, new BeanPropertyRowMapper<>(Home.class));
74 60
     }
75 61
 
76 62
     public Home findHomeByHomeNumber(String homeNumber) {
@@ -94,5 +80,6 @@ public class HomeService {
94 80
     public List<Person> generatePeopleInHome(Integer id) {
95 81
         return jdbcTemplate.query(
96 82
                 "SELECT * FROM person INNER JOIN home on person.home_id = home.id WHERE home.id = ?",
97
-                new Object[]{id}, new PersonService.PersonRowMapper());
83
+                new Object[]{id}, new PersonRowMapper());
84
+    }
98 85
 }

+ 1
- 1
src/main/java/io/zipcoder/persistenceapp/service/PersonService.java View File

@@ -1,6 +1,6 @@
1 1
 package io.zipcoder.persistenceapp.service;
2 2
 
3
-import io.zipcoder.persistenceapp.controller.PersonRowMapper;
3
+import io.zipcoder.persistenceapp.controller.mappers.PersonRowMapper;
4 4
 import io.zipcoder.persistenceapp.model.Person;
5 5
 import org.springframework.beans.factory.annotation.Autowired;
6 6
 import org.springframework.jdbc.core.BeanPropertyRowMapper;