#5 Completed Lab

開啟中
mpierse 請求將 7 次程式碼提交從 mpierse/CR-MacroLab-Spring-MovieDatabase.SQL.JDBC.JPA:master 合併至 master

+ 51
- 0
src/main/java/io/zipcoder/persistenceapp/home/Home.java 查看文件

1
+package io.zipcoder.persistenceapp.home;
2
+
3
+import javax.persistence.Column;
4
+import javax.persistence.Entity;
5
+import javax.persistence.GeneratedValue;
6
+import javax.persistence.Id;
7
+
8
+@Entity
9
+public class Home {
10
+
11
+    @Id
12
+    @GeneratedValue
13
+    @Column(name = "id")
14
+    private Long id;
15
+    @Column(name = "address")
16
+    private String address;
17
+    @Column(name = "homenumber")
18
+    private String homenumber;
19
+
20
+    public Home(){}
21
+
22
+    public Home(Long id, String address, String homenumber) {
23
+        this.id = id;
24
+        this.address = address;
25
+        this.homenumber = homenumber;
26
+    }
27
+
28
+    public Long getId() {
29
+        return id;
30
+    }
31
+
32
+    public void setId(Long id) {
33
+        this.id = id;
34
+    }
35
+
36
+    public String getAddress() {
37
+        return address;
38
+    }
39
+
40
+    public void setAddress(String address) {
41
+        this.address = address;
42
+    }
43
+
44
+    public String getHomenumber() {
45
+        return homenumber;
46
+    }
47
+
48
+    public void setHomenumber(String homenumber) {
49
+        this.homenumber = homenumber;
50
+    }
51
+}

+ 27
- 0
src/main/java/io/zipcoder/persistenceapp/home/HomeController.java 查看文件

1
+package io.zipcoder.persistenceapp.home;
2
+
3
+import io.zipcoder.persistenceapp.person.Person;
4
+import org.springframework.beans.factory.annotation.Autowired;
5
+import org.springframework.web.bind.annotation.PathVariable;
6
+import org.springframework.web.bind.annotation.RequestMapping;
7
+import org.springframework.web.bind.annotation.RequestMethod;
8
+import org.springframework.web.bind.annotation.RestController;
9
+
10
+import java.util.List;
11
+
12
+@RestController
13
+public class HomeController {
14
+
15
+    @Autowired
16
+    private HomeService homeService;
17
+
18
+    @RequestMapping(value = "/homes", method = RequestMethod.GET)
19
+    public List<Home> getAllHomes(){
20
+        return homeService.findAll();
21
+    }
22
+
23
+    @RequestMapping(value = "/home/{id}/{address}/{houseNumber}", method = RequestMethod.POST)
24
+    public Home createHome(@PathVariable Long id, @PathVariable String address, @PathVariable String houseNumber) {
25
+        return homeService.createHome(id, address, houseNumber);
26
+    }
27
+}

+ 40
- 0
src/main/java/io/zipcoder/persistenceapp/home/HomeService.java 查看文件

1
+package io.zipcoder.persistenceapp.home;
2
+
3
+import io.zipcoder.persistenceapp.person.Person;
4
+import org.springframework.beans.factory.annotation.Autowired;
5
+import org.springframework.jdbc.core.JdbcTemplate;
6
+import org.springframework.stereotype.Service;
7
+
8
+import java.util.ArrayList;
9
+import java.util.List;
10
+import java.util.Map;
11
+
12
+@Service
13
+public class HomeService {
14
+
15
+    @Autowired
16
+    JdbcTemplate jdbcTemplate;
17
+
18
+    public HomeService() {
19
+    }
20
+
21
+    public List<Home> findAll(){
22
+        String sql = "Select * from Home";
23
+        List<Home> homes = new ArrayList<>();
24
+        List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql);
25
+        for(Map row : rows){
26
+            Home h = new Home();
27
+            h.setId(Long.parseLong(String.valueOf(row.get("id"))));
28
+            h.setAddress((String) row.get("address"));
29
+            h.setHomenumber((String) row.get("homenumber"));
30
+            homes.add(h);
31
+        }
32
+        return homes;
33
+    }
34
+
35
+    public Home createHome(Long id, String address, String houseNumber) {
36
+        this.jdbcTemplate.update("insert into home (id, address, homenumber) values (?, ?, ?)",
37
+                new Object[] {id, address, houseNumber});
38
+        return new Home(id, address, houseNumber);
39
+    }
40
+}

+ 84
- 0
src/main/java/io/zipcoder/persistenceapp/person/JdbcPersonServiceImpl.java 查看文件

1
+package io.zipcoder.persistenceapp.person;
2
+
3
+import io.zipcoder.persistenceapp.person.Person;
4
+import org.springframework.beans.factory.annotation.Autowired;
5
+import org.springframework.jdbc.core.JdbcTemplate;
6
+import org.springframework.stereotype.Service;
7
+
8
+import java.util.ArrayList;
9
+import java.util.List;
10
+import java.util.Map;
11
+
12
+@Service
13
+public class JdbcPersonServiceImpl implements PersonService{
14
+
15
+    @Autowired
16
+    private JdbcTemplate jdbcTemplate;
17
+
18
+    public List<Person> findAll() {
19
+        String sql = "SELECT * FROM Person";
20
+        List<Person> people = new ArrayList<>();
21
+        List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql);
22
+        for (Map row : rows) {
23
+            Person p = new Person();
24
+            p.setId(Long.parseLong(String.valueOf(row.get("id"))));
25
+            p.setFirstName((String) row.get("first_name"));
26
+            p.setLastName((String) row.get("last_name"));
27
+            p.setMobile((String) row.get("mobile"));
28
+            p.setBirthDate(String.valueOf(row.get("birthday")));
29
+            p.setHomeId(Short.parseShort(String.valueOf(row.get("home_id"))));
30
+            people.add(p);
31
+        }
32
+        return people;
33
+    }
34
+
35
+    public Person createPerson(Long id, String firstName, String lastName, String mobile, String birthdate, Short homeId){
36
+    this.jdbcTemplate.update("insert into person (id, first_name, last_name, mobile, birthday, home_id) values (?, ?, ?, ?, ?, ?)",
37
+            new Object[] {id, firstName, lastName, mobile, birthdate, homeId});
38
+    return new Person(id, firstName,lastName, mobile, birthdate, homeId);
39
+    }
40
+
41
+    public Person updatePerson(Long id, String firstName, String lastName, String mobile, String birthdate, Short homeId){
42
+         this.jdbcTemplate.update("update person set first_name=?, last_name=?, mobile=?, birthday=?, home_id=? where id= ?",
43
+                new Object[] {firstName, lastName, mobile, birthdate, homeId, id});
44
+        return new Person(id, firstName,lastName, mobile, birthdate, homeId);
45
+    }
46
+
47
+    public Person getPerson(Long id){
48
+        List<Person> personList = findAll();
49
+        for (Person p : personList) {
50
+            if(p.getId()==id)
51
+                return p;
52
+        }
53
+        return null;
54
+    }
55
+
56
+    public Person getPerson(String lastname){
57
+        List<Person> personList = findAll();
58
+        for (Person p : personList) {
59
+            if(p.getLastName()==lastname)
60
+                return p;
61
+        }
62
+        return null;
63
+    }
64
+
65
+    public Person getReverseLookup(String mobile){
66
+        List<Person> personList = findAll();
67
+        for (Person p : personList) {
68
+            if(p.getMobile()==mobile)
69
+                return p;
70
+        }
71
+        return null;
72
+    }
73
+
74
+    public int getFirstnameStats(String firstname){
75
+        List<Person> personList = findAll();
76
+        int stats = 0;
77
+        for (Person p : personList) {
78
+            if(p.getFirstName()==firstname)
79
+                stats++;
80
+        }
81
+        return stats;
82
+    }
83
+
84
+}

+ 84
- 0
src/main/java/io/zipcoder/persistenceapp/person/Person.java 查看文件

1
+package io.zipcoder.persistenceapp.person;
2
+
3
+import javax.persistence.*;
4
+import javax.persistence.criteria.CriteriaBuilder;
5
+import java.util.Date;
6
+
7
+@Entity
8
+public class Person {
9
+    @Id
10
+    @GeneratedValue
11
+    @Column(name = "id")
12
+    private Long id;
13
+    @Column(name = "first_name")
14
+    private String firstName;
15
+    @Column(name = "last_name")
16
+    private String lastName;
17
+    @Column(name = "mobile")
18
+    private String mobile;
19
+    @Column(name = "birthday")
20
+    private String birthDate;
21
+    @Column(name = "home_id")
22
+    private Short homeId;
23
+
24
+    public Person(){
25
+
26
+    }
27
+
28
+    public Person(Long id, String firstName, String lastName, String mobile, String date, Short homeId) {
29
+        this.id=id;
30
+        this.firstName = firstName;
31
+        this.lastName = lastName;
32
+        this.mobile=mobile;
33
+        this.birthDate=date;
34
+        this.homeId = homeId;
35
+    }
36
+
37
+    public String getMobile() {
38
+        return mobile;
39
+    }
40
+
41
+    public void setMobile(String mobile) {
42
+        this.mobile = mobile;
43
+    }
44
+
45
+    public Short getHomeId() {
46
+        return homeId;
47
+    }
48
+
49
+    public void setHomeId(Short homeId) {
50
+        this.homeId = homeId;
51
+    }
52
+
53
+    public Long getId() {
54
+        return id;
55
+    }
56
+
57
+    public void setId(Long id) {
58
+        this.id = id;
59
+    }
60
+
61
+    public String getFirstName() {
62
+        return firstName;
63
+    }
64
+
65
+    public void setFirstName(String firstName) {
66
+        this.firstName = firstName;
67
+    }
68
+
69
+    public String getLastName() {
70
+        return lastName;
71
+    }
72
+
73
+    public void setLastName(String lastName) {
74
+        this.lastName = lastName;
75
+    }
76
+
77
+    public String getBirthDate() {
78
+        return birthDate;
79
+    }
80
+
81
+    public void setBirthDate(String birthDate) {
82
+        this.birthDate = birthDate;
83
+    }
84
+}

+ 54
- 0
src/main/java/io/zipcoder/persistenceapp/person/PersonController.java 查看文件

1
+package io.zipcoder.persistenceapp.person;
2
+
3
+import org.springframework.beans.factory.annotation.Autowired;
4
+import org.springframework.web.bind.annotation.PathVariable;
5
+import org.springframework.web.bind.annotation.RequestMapping;
6
+import org.springframework.web.bind.annotation.RequestMethod;
7
+import org.springframework.web.bind.annotation.RestController;
8
+
9
+import java.util.List;
10
+
11
+@RestController
12
+public class PersonController {
13
+
14
+    @Autowired
15
+    private JdbcPersonServiceImpl personService;
16
+
17
+    public PersonController() {
18
+    }
19
+
20
+    @RequestMapping(value = "/people", method = RequestMethod.GET)
21
+    public List<Person> getAllPeople(){
22
+        return personService.findAll();
23
+    }
24
+
25
+    @RequestMapping(value = "/people/{id}/{firstName}/{lastname}/{mobile}/{birthday}/{homeId}", method = RequestMethod.POST)
26
+    public Person createPerson(@PathVariable Long id, @PathVariable String firstName, @PathVariable String lastname, @PathVariable String mobile, @PathVariable String birthday, @PathVariable Short homeId) {
27
+        return personService.createPerson(id, firstName, lastname, mobile, birthday, homeId);
28
+    }
29
+
30
+    @RequestMapping(value = "/people/{id}/{firstName}/{lastname}/{mobile}/{birthday}/{homeId}", method = RequestMethod.PUT)
31
+    public Person updatePerson(@PathVariable Long id, @PathVariable String firstName, @PathVariable String lastname, @PathVariable String mobile, @PathVariable String birthday, @PathVariable Short homeId) {
32
+        return personService.updatePerson(id, firstName, lastname, mobile, birthday, homeId);
33
+    }
34
+
35
+    @RequestMapping(value = "/people/{id}", method = RequestMethod.GET)
36
+    public Person getPerson(@PathVariable Long id){
37
+        return personService.getPerson(id);
38
+    }
39
+
40
+    @RequestMapping(value = "/people/{lastName}", method = RequestMethod.GET)
41
+    public Person getPersonByLastname(@PathVariable String lastName){
42
+        return personService.getPerson(lastName);
43
+    }
44
+
45
+    @RequestMapping(value = "/people/{mobile}", method = RequestMethod.GET)
46
+    public Person getReverseLookup(@PathVariable String mobile){
47
+        return personService.getReverseLookup(mobile);
48
+    }
49
+
50
+    @RequestMapping(value = "/people/firstname/stats/{firstName}", method = RequestMethod.GET)
51
+    public int getFirstNameStats(@PathVariable String firstName){
52
+        return personService.getFirstnameStats(firstName);
53
+    }
54
+}

+ 6
- 0
src/main/java/io/zipcoder/persistenceapp/person/PersonRepository.java 查看文件

1
+package io.zipcoder.persistenceapp.person;
2
+
3
+import org.springframework.data.jpa.repository.JpaRepository;
4
+
5
+public interface PersonRepository extends JpaRepository<Person, Long> {
6
+}

+ 15
- 0
src/main/java/io/zipcoder/persistenceapp/person/PersonService.java 查看文件

1
+package io.zipcoder.persistenceapp.person;
2
+
3
+import java.util.List;
4
+
5
+
6
+public interface PersonService {
7
+
8
+    List<Person> findAll();
9
+    Person createPerson(Long id, String firstName, String lastName, String mobile, String birthdate, Short homeId);
10
+    Person updatePerson(Long id, String firstName, String lastName, String mobile, String birthdate, Short homeId);
11
+    Person getPerson(Long id);
12
+    Person getPerson(String lastname);
13
+    Person getReverseLookup(String mobile);
14
+    int getFirstnameStats(String firstname);
15
+}

+ 66
- 0
src/main/java/io/zipcoder/persistenceapp/person/PersonServiceJpa.java 查看文件

1
+package io.zipcoder.persistenceapp.person;
2
+
3
+import org.springframework.beans.factory.annotation.Autowired;
4
+import org.springframework.context.annotation.Primary;
5
+import org.springframework.stereotype.Service;
6
+
7
+import java.util.List;
8
+
9
+@Service
10
+@Primary
11
+public class PersonServiceJpa implements PersonService{
12
+
13
+    @Autowired
14
+    PersonRepository personRepository;
15
+
16
+    @Override
17
+    public List<Person> findAll() {
18
+        return personRepository.findAll();
19
+    }
20
+
21
+    @Override
22
+    public Person createPerson(Long id, String firstName, String lastName, String mobile, String birthdate, Short homeId) {
23
+        return personRepository.save(new Person(id,firstName,lastName,mobile,birthdate,homeId));
24
+    }
25
+
26
+    @Override
27
+    public Person updatePerson(Long id, String firstName, String lastName, String mobile, String birthdate, Short homeId) {
28
+        return personRepository.save(new Person(id,firstName,lastName,mobile,birthdate,homeId));
29
+    }
30
+
31
+    @Override
32
+    public Person getPerson(Long id) {
33
+        return personRepository.getOne(id);
34
+    }
35
+
36
+    @Override
37
+    public Person getPerson(String lastname) {
38
+        List<Person> ppl = findAll();
39
+        for (Person p : ppl) {
40
+            if (p.getLastName().equals(lastname))
41
+                return p;
42
+        }
43
+        return null;
44
+    }
45
+
46
+    @Override
47
+    public Person getReverseLookup(String mobile) {
48
+        List<Person> ppl = findAll();
49
+        for (Person p : ppl) {
50
+            if (p.getMobile().equals(mobile))
51
+                return p;
52
+        }
53
+        return null;
54
+    }
55
+
56
+    @Override
57
+    public int getFirstnameStats(String firstname) {
58
+        List<Person> ppl = findAll();
59
+        int count = 0;
60
+        for (Person p : ppl) {
61
+            if (p.getMobile().equals(firstname))
62
+               count++;
63
+        }
64
+        return count;
65
+    }
66
+}

+ 1
- 1
src/main/resources/application-h2.properties 查看文件

1
-spring.datasource.url=jdbc:h2:mem:testdb;Mode=Oracle
1
+spring.datasource.url=jdbc:h2:mem:test;Mode=Oracle
2
 spring.datasource.platform=h2
2
 spring.datasource.platform=h2
3
 spring.jpa.hibernate.ddl-auto=none
3
 spring.jpa.hibernate.ddl-auto=none
4
 spring.datasource.continue-on-error=true
4
 spring.datasource.continue-on-error=true

+ 60
- 0
src/main/resources/script.sql 查看文件

1
+CREATE TABLE MOVIE(ID INT PRIMARY KEY, TITLE VARCHAR(255),
2
+RUNTIME INT, GENRE VARCHAR(255), IMDB_SCORE DOUBLE, RATING VARCHAR(255));
3
+
4
+INSERT INTO MOVIE
5
+VALUES (1, 'Howard the Duck', 110, 'Sci-Fi', 4.6, 'PG');
6
+
7
+INSERT INTO MOVIE
8
+VALUES (2, 'Lavalantula', 83, 'Horror', 4.7, 'TV-14');
9
+
10
+INSERT INTO MOVIE
11
+VALUES (3, 'Starship Troopers', 129, 'Sci-Fi', 7.2, 'PG-13');
12
+
13
+INSERT INTO MOVIE
14
+VALUES (4, 'Waltz With Bashir', 90	, 'Documentary', 8.0, 'R');
15
+
16
+INSERT INTO MOVIE
17
+VALUES (5, 'Spaceballs', 96, 'Comedy', 7.1, 'PG');
18
+
19
+INSERT INTO MOVIE
20
+VALUES (6, 'Monsters Inc.', 92, 'Animation', 8.1, 'G');
21
+
22
+INSERT INTO MOVIE
23
+VALUES (7, 'The Princess Bride.', 100, 'Awesome', 10.0, 'PG');
24
+
25
+INSERT INTO MOVIE
26
+VALUES (8, 'Elf', 95, 'Christmas', 9.5, 'PG');
27
+
28
+Select * FROM MOVIE
29
+Where GENRE = 'Sci-Fi';
30
+
31
+Select * FROM MOVIE
32
+Where IMDB_SCORE > 6.5;
33
+
34
+Select * FROM MOVIE
35
+Where RUNTIME < 100
36
+AND RATING = 'PG'
37
+OR RATING = 'G';
38
+
39
+Select GENRE, AVG(RUNTIME) FROM MOVIE
40
+GROUP BY GENRE;
41
+
42
+UPDATE MOVIE
43
+SET Rating = 'R'
44
+WHERE TITLE = 'Starship Troopers';
45
+
46
+SELECT ID FROM MOVIE
47
+WHERE GENRE = 'Horror'
48
+OR GENRE = 'Documentary';
49
+
50
+Select RATING, AVG(IMDB_SCORE), MAX(IMDB_SCORE), MIN(IMDB_SCORE)
51
+FROM MOVIE
52
+GROUP BY RATING;
53
+
54
+Select RATING, AVG(IMDB_SCORE), MAX(IMDB_SCORE), MIN(IMDB_SCORE)
55
+FROM MOVIE
56
+GROUP BY RATING
57
+HAVING COUNT(*) >1;
58
+
59
+DELETE FROM MOVIE
60
+WHERE RATING = 'R';