#84 Added Repositories
This commit is contained in:
parent
a44f32a1e0
commit
6ed145c6ab
@ -1,7 +1,5 @@
|
|||||||
package com.iluwatar.layers;
|
package com.iluwatar.layers;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||||
|
|
||||||
public class App {
|
public class App {
|
||||||
@ -9,39 +7,44 @@ public class App {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
|
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
|
||||||
"applicationContext.xml");
|
"applicationContext.xml");
|
||||||
PersonDao dao = context.getBean(PersonDao.class);
|
|
||||||
|
CakeLayerDao cakeLayerDao = context.getBean(CakeLayerDao.class);
|
||||||
Person peter = new Person("Peter", "Sagan");
|
cakeLayerDao.save(new CakeLayer("strawberry", 1200));
|
||||||
Person nasta = new Person("Nasta", "Kuzminova");
|
System.out.println("Count CakeLayer records: " + cakeLayerDao.count());
|
||||||
|
|
||||||
// Add new Person records
|
// PersonDao dao = context.getBean(PersonDao.class);
|
||||||
dao.save(peter);
|
//
|
||||||
dao.save(nasta);
|
// Person peter = new Person("Peter", "Sagan");
|
||||||
|
// Person nasta = new Person("Nasta", "Kuzminova");
|
||||||
// Count Person records
|
//
|
||||||
System.out.println("Count Person records: " + dao.count());
|
// // Add new Person records
|
||||||
|
// dao.save(peter);
|
||||||
// Print all records
|
// dao.save(nasta);
|
||||||
List<Person> persons = (List<Person>) dao.findAll();
|
//
|
||||||
for (Person person : persons) {
|
// // Count Person records
|
||||||
System.out.println(person);
|
// System.out.println("Count Person records: " + dao.count());
|
||||||
}
|
//
|
||||||
|
// // Print all records
|
||||||
// Find Person by surname
|
// List<Person> persons = (List<Person>) dao.findAll();
|
||||||
System.out.println("Find by surname 'Sagan': " + dao.findBySurname("Sagan"));
|
// for (Person person : persons) {
|
||||||
|
// System.out.println(person);
|
||||||
// Update Person
|
// }
|
||||||
nasta.setName("Barbora");
|
//
|
||||||
nasta.setSurname("Spotakova");
|
// // Find Person by surname
|
||||||
dao.save(nasta);
|
// System.out.println("Find by surname 'Sagan': " + dao.findBySurname("Sagan"));
|
||||||
|
//
|
||||||
System.out.println("Find by id 2: " + dao.findOne(2L));
|
// // Update Person
|
||||||
|
// nasta.setName("Barbora");
|
||||||
// Remove record from Person
|
// nasta.setSurname("Spotakova");
|
||||||
dao.delete(2L);
|
// dao.save(nasta);
|
||||||
|
//
|
||||||
// And finally count records
|
// System.out.println("Find by id 2: " + dao.findOne(2L));
|
||||||
System.out.println("Count Person records: " + dao.count());
|
//
|
||||||
|
// // Remove record from Person
|
||||||
|
// dao.delete(2L);
|
||||||
|
//
|
||||||
|
// // And finally count records
|
||||||
|
// System.out.println("Count Person records: " + dao.count());
|
||||||
|
|
||||||
context.close();
|
context.close();
|
||||||
}
|
}
|
||||||
|
9
layers/src/main/java/com/iluwatar/layers/CakeDao.java
Normal file
9
layers/src/main/java/com/iluwatar/layers/CakeDao.java
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package com.iluwatar.layers;
|
||||||
|
|
||||||
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface CakeDao extends CrudRepository<Cake, Long> {
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package com.iluwatar.layers;
|
||||||
|
|
||||||
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface CakeLayerDao extends CrudRepository<CakeLayer, Long> {
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package com.iluwatar.layers;
|
||||||
|
|
||||||
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface CakeToppingDao extends CrudRepository<CakeTopping, Long> {
|
||||||
|
|
||||||
|
}
|
@ -1,58 +0,0 @@
|
|||||||
package com.iluwatar.layers;
|
|
||||||
|
|
||||||
import javax.persistence.Entity;
|
|
||||||
import javax.persistence.GeneratedValue;
|
|
||||||
import javax.persistence.Id;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* Person entity
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
@Entity
|
|
||||||
public class Person {
|
|
||||||
|
|
||||||
@Id
|
|
||||||
@GeneratedValue
|
|
||||||
private Long id;
|
|
||||||
private String name;
|
|
||||||
private String surname;
|
|
||||||
|
|
||||||
public Person() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public Person(String name, String surname) {
|
|
||||||
this.name = name;
|
|
||||||
this.surname = surname;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Long getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setId(Long id) {
|
|
||||||
this.id = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setName(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getSurname() {
|
|
||||||
return surname;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setSurname(String surname) {
|
|
||||||
this.surname = surname;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "Person [id=" + id + ", name=" + name + ", surname=" + surname
|
|
||||||
+ "]";
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,17 +0,0 @@
|
|||||||
package com.iluwatar.layers;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.springframework.data.repository.CrudRepository;
|
|
||||||
import org.springframework.stereotype.Repository;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* Person repository
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
@Repository
|
|
||||||
public interface PersonDao extends CrudRepository<Person, Long> {
|
|
||||||
|
|
||||||
public List<Person> findBySurname(String surname);
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user