760 likes | 2.39k Views
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
E N D
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 Hades • http://redmine.synyx.org/projects/show/hades • Feature • DAO, page, sort • Work with Service Transaction, Spring MVC
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>
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 • ……
Enable Repository • <jpa:repositories base-package="com.example.repositories" />
Domain • @Entity • public class Department implements Serializable{ • @Id private Long id; • private String name; • …… • }
DTO • public class DepartmentDto { • private Long id; • private String name; • public DepartmentDto(Long id, String name) { • super(); • this.id = id; • this.name = name; • }
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(); • }
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);
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
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);
Get Page of entities • page = departmentRepository.findAll(pageable); • page = departmentRepository.findByName(name, pageable); • Page<DepartmentDto> pageDto = departmentRepository.findDtoByName(name, pageable);
Get List of Entities • departmentRepository.findByNameLikeOrderByIdAscNameDesc("AA%")
Supported keywords inside method names • findByLastnameAndFirstname • findByLastnameOrFirstname • findByStartDateBetween • findByAgeLessThan • findByAgeIsNull • findByAgeIn(Collection<Age> • ages) • ……
Usage • Simple CRUD • Page, Sort and Order • In Service
Work with Spring MVC • TODO • Reference • http://hades.synyx.org/static/2.x/site/org.synyx.hades/reference/html/web-pagination.html
References • http://www.springsource.org/spring-data/jpa • http://static.springsource.org/spring-data/data-jpa/docs/current/reference/html/