Crud Operations using Spring Data JPA
import org.springframework.data.repository.CrudRepository;
public interface PersonDAO extends CrudRepository<Person, Long> {
}
Person - bean which you want to persist(Bean name)
Long - Type of primary key of the bean Person.(primary key is mandatory to persist)
Implementation
We dont really implement PersonDAO interface at all,Hibernate implements for use,we just use those method as shown below.
@Service
public class ServiceLayer {
@Autowired
private PersonDAO dao;
 public Person addPerson(Person person) {
  
  return dao.save(person);
 
 }
 public List getAllPerson() {
  
  return (List) dao.findAll();
 }
 
 public void deletePerson(int id) {
  
  dao.delete(id);
 }
}
  
Source code is available in next chapter
0 comments:
Post a Comment