1 / 18

Spring Data JPA

Spring Data JPA. 邱张华 2011/7/28. Currently. JPA Spring Service No DAO Merge DAO into Service. Why Spring Data JPA. Spring Data Project Different Data: RDB, Mongo, Redis , HBase … Mature From H ades http://redmine.synyx.org/projects/show/hades Feature DAO, page, sort

hallie
Download Presentation

Spring Data JPA

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Spring Data JPA 邱张华 2011/7/28

  2. Currently • JPA • Spring • Service • No DAO • Merge DAO into Service

  3. Why Spring Data JPA • Spring Data Project • Different Data: RDB, Mongo, Redis, HBase … • Mature • From Hades • http://redmine.synyx.org/projects/show/hades • Feature • DAO, page, sort • Work with Service Transaction, Spring MVC

  4. Repository hierarchy • org.springframework.data.jpa.repository.JpaRepository<T, ID> • org.springframework.data.repository.PagingAndSortingRepository<T, ID> • org.springframework.data.repository.CrudRepository<T, ID> • org.springframework.data.repository.CrudRepository<T, ID> • org.springframework.data.repository.Repository<T, ID>

  5. Bean Schema • <beans xmlns="http://www.springframework.org/schema/beans" • xmlns:jpa="http://www.springframework.org/schema/data/jpa" • http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd • ……

  6. Enable Repository • <jpa:repositories base-package="com.example.repositories" />

  7. Domain • @Entity • public class Department implements Serializable{ • @Id private Long id; • private String name; • …… • }

  8. DTO • public class DepartmentDto { • private Long id; • private String name; • public DepartmentDto(Long id, String name) { • super(); • this.id = id; • this.name = name; • }

  9. User Defined Repository • public interface DepartmentRepository extends JpaRepository<Department, Long> { • @Query("select d from Department d where d.name like :name") • Page<Department> findByName(@Param("name") String name, Pageablepageable); • @Query("select new com.example.dto.DepartmentDto(d.id, d.name) from Department d where d.name like :name") • Page<DepartmentDto> findDtoByName(@Param("name") String name, • Pageablepageable); • List<Department> findByNameLikeOrderByIdAscNameDesc(String name); • // not useful • @Query("select sum(d.id) from Department d") • Long dummySum(); • }

  10. Get Bean • public static DepartmentRepositorydepartmentRepository; • context = new ClassPathXmlApplicationContext( • "/META-INF/spring/applicationContext.xml"); • context.registerShutdownHook(); • entityManagerFactory = (EntityManagerFactory) context • .getBean("entityManagerFactory"); • departmentRepository = context.getBean(DepartmentRepository.class);

  11. DAO • Department department = new Department(); • department.setName("AZZ"); • departmentRepository.save(department); • Department department = departmentRepository.findOne(1L); • department.setName("ABCABC"); • department = departmentRepository.saveAndFlush(department); • System.out.println(department); • …… • // Delete etc

  12. Pageable • Pageable pageable = new PageRequest(0, 10); • Order order1 = new Order(Direction.ASC, "id"); • Order order2 = new Order(Direction.DESC, "name"); • Sort sort = new Sort(order1, order2); • pageable = new PageRequest(0, 10, sort);

  13. Get Page of entities • page = departmentRepository.findAll(pageable); • page = departmentRepository.findByName(name, pageable); • Page<DepartmentDto> pageDto = departmentRepository.findDtoByName(name, pageable);

  14. Get List of Entities • departmentRepository.findByNameLikeOrderByIdAscNameDesc("AA%")

  15. Supported keywords inside method names • findByLastnameAndFirstname • findByLastnameOrFirstname • findByStartDateBetween • findByAgeLessThan • findByAgeIsNull • findByAgeIn(Collection<Age> • ages) • ……

  16. Usage • Simple CRUD • Page, Sort and Order • In Service

  17. Work with Spring MVC • TODO • Reference • http://hades.synyx.org/static/2.x/site/org.synyx.hades/reference/html/web-pagination.html

  18. References • http://www.springsource.org/spring-data/jpa • http://static.springsource.org/spring-data/data-jpa/docs/current/reference/html/

More Related