Merge pull request #281 from ankurkaushal/master
Reformat according to google style guide
This commit is contained in:
@ -7,61 +7,63 @@ 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
|
||||
* 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
|
||||
* systems it can be worthwhile to build another layer of abstraction over the mapping layer where
|
||||
* query construction code is concentrated. This becomes more important when there are a large
|
||||
* query construction code is concentrated. This becomes more important when there are a large
|
||||
* number of domain classes or heavy querying. In these cases particularly, adding this layer helps
|
||||
* minimize duplicate query logic.
|
||||
* <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.
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
* @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");
|
||||
/**
|
||||
* Program entry point
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
ClassPathXmlApplicationContext context =
|
||||
new ClassPathXmlApplicationContext("applicationContext.xml");
|
||||
PersonRepository repository = context.getBean(PersonRepository.class);
|
||||
|
||||
// Add new Person records
|
||||
repository.save(peter);
|
||||
repository.save(nasta);
|
||||
Person peter = new Person("Peter", "Sagan");
|
||||
Person nasta = new Person("Nasta", "Kuzminova");
|
||||
|
||||
// Count Person records
|
||||
System.out.println("Count Person records: " + repository.count());
|
||||
// Add new Person records
|
||||
repository.save(peter);
|
||||
repository.save(nasta);
|
||||
|
||||
// Print all records
|
||||
List<Person> persons = (List<Person>) repository.findAll();
|
||||
for (Person person : persons) {
|
||||
System.out.println(person);
|
||||
}
|
||||
// Count Person records
|
||||
System.out.println("Count Person records: " + repository.count());
|
||||
|
||||
// Find Person by surname
|
||||
System.out.println("Find by surname 'Sagan': " + repository.findBySurname("Sagan"));
|
||||
// Print all records
|
||||
List<Person> persons = (List<Person>) repository.findAll();
|
||||
for (Person person : persons) {
|
||||
System.out.println(person);
|
||||
}
|
||||
|
||||
// Update Person
|
||||
nasta.setName("Barbora");
|
||||
nasta.setSurname("Spotakova");
|
||||
repository.save(nasta);
|
||||
// Find Person by surname
|
||||
System.out.println("Find by surname 'Sagan': " + repository.findBySurname("Sagan"));
|
||||
|
||||
System.out.println("Find by id 2: " + repository.findOne(2L));
|
||||
// Update Person
|
||||
nasta.setName("Barbora");
|
||||
nasta.setSurname("Spotakova");
|
||||
repository.save(nasta);
|
||||
|
||||
// Remove record from Person
|
||||
repository.delete(2L);
|
||||
System.out.println("Find by id 2: " + repository.findOne(2L));
|
||||
|
||||
// And finally count records
|
||||
System.out.println("Count Person records: " + repository.count());
|
||||
// Remove record from Person
|
||||
repository.delete(2L);
|
||||
|
||||
context.close();
|
||||
}
|
||||
// And finally count records
|
||||
System.out.println("Count Person records: " + repository.count());
|
||||
|
||||
context.close();
|
||||
}
|
||||
}
|
||||
|
@ -12,47 +12,45 @@ import javax.persistence.Id;
|
||||
@Entity
|
||||
public class Person {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
private String name;
|
||||
private String surname;
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
private String name;
|
||||
private String surname;
|
||||
|
||||
public Person() {
|
||||
}
|
||||
public Person() {}
|
||||
|
||||
public Person(String name, String surname) {
|
||||
this.name = name;
|
||||
this.surname = surname;
|
||||
}
|
||||
public Person(String name, String surname) {
|
||||
this.name = name;
|
||||
this.surname = surname;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getSurname() {
|
||||
return surname;
|
||||
}
|
||||
public String getSurname() {
|
||||
return surname;
|
||||
}
|
||||
|
||||
public void setSurname(String surname) {
|
||||
this.surname = surname;
|
||||
}
|
||||
public void setSurname(String surname) {
|
||||
this.surname = surname;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Person [id=" + id + ", name=" + name + ", surname=" + surname
|
||||
+ "]";
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Person [id=" + id + ", name=" + name + ", surname=" + surname + "]";
|
||||
}
|
||||
}
|
||||
|
@ -12,6 +12,6 @@ import org.springframework.stereotype.Repository;
|
||||
*/
|
||||
@Repository
|
||||
public interface PersonRepository extends CrudRepository<Person, Long> {
|
||||
|
||||
public List<Person> findBySurname(String surname);
|
||||
|
||||
public List<Person> findBySurname(String surname);
|
||||
}
|
||||
|
@ -11,9 +11,9 @@ import com.iluwatar.repository.App;
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user