Fix #216 Repository vs DAO

This commit is contained in:
hoswey
2015-11-27 11:22:33 +08:00
parent 092d48d150
commit 9d4fff6029
12 changed files with 343 additions and 72 deletions

View File

@@ -5,7 +5,6 @@ import java.util.List;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
*
* Repository pattern mediates between the domain and data mapping layers using a collection-like
* interface for accessing domain objects. A system with complex domain model often benefits from a
* layer that isolates domain objects from the details of the database access code and in such
@@ -16,9 +15,9 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
* <p>
* In this example we utilize Spring Data to automatically generate a repository for us from the
* {@link Person} domain object. Using the {@link PersonRepository} we perform CRUD operations on
* the entity. Underneath we have configured in-memory H2 database for which schema is created and
* dropped on each run.
*
* the entity, moreover, the query by {@link org.springframework.data.jpa.domain.Specification} are
* also performed. Underneath we have configured in-memory H2 database for which schema is created
* and dropped on each run.
*/
public class App {
@@ -28,16 +27,21 @@ public class App {
* @param args command line args
*/
public static void main(String[] args) {
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
PersonRepository repository = context.getBean(PersonRepository.class);
Person peter = new Person("Peter", "Sagan");
Person nasta = new Person("Nasta", "Kuzminova");
Person peter = new Person("Peter", "Sagan", 17);
Person nasta = new Person("Nasta", "Kuzminova", 25);
Person john = new Person("John", "lawrence", 35);
Person terry = new Person("Terry", "Law", 36);
// Add new Person records
repository.save(peter);
repository.save(nasta);
repository.save(john);
repository.save(terry);
// Count Person records
System.out.println("Count Person records: " + repository.count());
@@ -48,9 +52,6 @@ public class App {
System.out.println(person);
}
// Find Person by surname
System.out.println("Find by surname 'Sagan': " + repository.findBySurname("Sagan"));
// Update Person
nasta.setName("Barbora");
nasta.setSurname("Spotakova");
@@ -61,9 +62,22 @@ public class App {
// Remove record from Person
repository.delete(2L);
// And finally count records
// count records
System.out.println("Count Person records: " + repository.count());
// find by name
Person p = repository.findOne(new PersonSpecifications.NameEqualSpec("John"));
System.out.println("Find by John is " + p);
// find by age
persons = repository.findAll(new PersonSpecifications.AgeBetweenSpec(20, 40));
System.out.println("Find Person with age between 20,40: ");
for (Person person : persons) {
System.out.println(person);
}
context.close();
}
}

View File

@@ -18,11 +18,14 @@ public class Person {
private String name;
private String surname;
private int age;
public Person() {}
public Person(String name, String surname) {
public Person(String name, String surname, int age) {
this.name = name;
this.surname = surname;
this.age = age;
}
public Long getId() {
@@ -49,8 +52,59 @@ public class Person {
this.surname = surname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", surname=" + surname + "]";
return "Person [id=" + id + ", name=" + name + ", surname=" + surname + ", age=" + age + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((surname == null) ? 0 : surname.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (age != other.age)
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (surname == null) {
if (other.surname != null)
return false;
} else if (!surname.equals(other.surname))
return false;
return true;
}
}

View File

@@ -1,7 +1,6 @@
package com.iluwatar.repository;
import java.util.List;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@@ -11,7 +10,8 @@ import org.springframework.stereotype.Repository;
*
*/
@Repository
public interface PersonRepository extends CrudRepository<Person, Long> {
public interface PersonRepository
extends CrudRepository<Person, Long>, JpaSpecificationExecutor<Person> {
public List<Person> findBySurname(String surname);
public Person findByName(String name);
}

View File

@@ -0,0 +1,50 @@
package com.iluwatar.repository;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.springframework.data.jpa.domain.Specification;
/**
* Helper class, includes vary Specification as the abstraction of sql query criteria
*/
public class PersonSpecifications {
public static class AgeBetweenSpec implements Specification<Person> {
private int from;
private int to;
public AgeBetweenSpec(int from, int to) {
this.from = from;
this.to = to;
}
@Override
public Predicate toPredicate(Root<Person> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
return cb.between(root.get("age"), from, to);
}
}
public static class NameEqualSpec implements Specification<Person> {
public String name;
public NameEqualSpec(String name) {
this.name = name;
}
public Predicate toPredicate(Root<Person> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
return cb.equal(root.get("name"), this.name);
}
}
}

View File

@@ -6,10 +6,10 @@
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd">
<jpa:repositories base-package="com.iluwatar" />
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<jpa:repositories base-package="com.iluwatar" base-class="org.springframework.data.jpa.repository.support.SimpleJpaRepository" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>