Spring Data JDBCを使い倒す! / Intruduction to Spring Data JDBC

Spring Data JDBCを使い倒す! / Intruduction to Spring Data JDBC

JSUG勉強会 2018年その3 LT大会 登壇者募集!
2018-04-18(水)19:00 - 21:00

9b8f304ce234a4a8e38708247ffd68e0?s=128

Ryo Shindo

April 19, 2018
Tweet

Transcript

  1. Spring Data JDBCΛ
 ࢖͍౗͢ʂ
 Acroquest Technologyגࣜձࣾ ਐ౻ɹྒྷ

  2. ࣗݾ঺հ • ਐ౻ɹྒྷ @shindo_ryo • Acroquest Technologyגࣜձࣾ • (ݩ)จܥJavaΤϯδχΞ

  3. Spring Data JDBC is Կ

  4. Spring ͷ DB ΞΫηεϥΠϒϥϦ
 (·ͩ1.0.0.BUILD-SNAPSHOT)

  5. ΋͏͋Δ͡ΌΜ • Spring JDBC (JdbcTemplate) • mybatis-spring • Spring Data

    JPA
  6. Կ͕ҧ͏ͷ͔

  7. Spring Data JDBC ͸ DBΞΫηεͷ
 ࣮૷Λఏڙ͠ͳ͍

  8. Spring Data JDBC ͸Ξμϓλʔ Spring Data JDBC Spring JDBC MyBatis

    <<interface>> Repository ࣮૷ͷ੾Γସ͑͸ BeanఆٛͰߦ͏
  9. ;Θͬͱݴ͏ͱɺ
 DDDຊͷRepository ύλʔϯΛ ޷͖ͳ࣮૷Ͱ؆୯ʹͭ͘ΕΔΞϨ

  10. EmployeeRepository.java public interface EmployeeRepository extends CrudRepository<Employee, Long> { @Query("select *

    from employee where age > :age") List<Employee> findByAgeOlderThan(@Param(“age") int age); } ͔͍͔ͭͨ
  11. @Autowired EmployeeRepository repository; // insert or update repository.save(employee); // select

    Iterable<Employee> employees = repository.findAll(); Optional<Employee> employee = repository.findById(id); List<Employee> employees = repository. findByAgeOlderThan(30); // delete repository.delete(employee); ͔͍͔ͭͨ
  12. Spring JDBC ࣮૷ͷ৔߹ SpringDataJdbcConfig.java @Configuration @EnableJdbcRepositories public class SpringDataJdbcConfig {

    @Bean DataAccessStrategy dataAccessStrategy(JdbcMappingContext context) { return new DefaultDataAccessStrategy( new SqlGeneratorSource(context), context); } }
  13. Ͷɺ؆୯Ͱ͠ΐ͏ʁ

  14. MyBatis ࣮૷ͷ৔߹ SpringDataJdbcConfig.java @Configuration @EnableJdbcRepositories public class SpringDataJdbcConfig { @Bean

    DataAccessStrategy dataAccessStrategy(SqlSession sqlSession) { return new MyBatisDataAccessStrategy(sqlSession); } }
  15. application.properties mybatis.mapper-locations=
 classpath:/com/example/demo/mapper/*Mapper.xml ɹɹɹˡMapperϑΝΠϧͷϩέʔγϣϯΛࢦఆ͢Δ mybatis.type-aliases-package=com.example.demo ɹɹɹˡEntityΫϥεͷύοέʔδΛࢦఆ͢Δ MyBatis ࣮૷ͷ৔߹

  16. EmployeeMapper.xml <mapper namespace="com.example.demo.EmployeeMapper"> <insert id="insert" useGeneratedKeys="true“ keyProperty="instance.employeeNumber"> INSERT INTO employee

    (firstname, lastname, age, hired_at) VALUES (#{instance.firstname}, #{instance.lastname}, #{instance.age}, #{instance.hiredAt}) </insert> <update id="update"> ……… MyBatis ࣮૷ͷ৔߹ CrudRepository ͷϝιου ʹରԠͨ͠SQLΛఆٛ͢Δ
  17. Ͷɺ؆୯Ͱʢུʣ

  18. جຊతʹɺDataAccessStrategy 
 ͷ࣮૷Λ੾Γସ͑Δ͚ͩ

  19. DataAccessStrategy 
 Λࣗ෼Ͱ࣮૷͢Ε͹
 ͲΜͳORMʹ΋࢖͑Δ

  20. ΋ͪΖΜHibernate΋ΠέΔ

  21. ͦΕԿͯ Spring Data JPA

  22. Hibernate ࣮૷ͷ৔߹ SpringDataJdbcConfig.java @Configuration @EnableJdbcRepositories @EnableAutoConfiguration(exclude = HibernateJpaAutoConfiguration.class) public class

    SpringDataJdbcConfig { @Bean DataAccessStrategy dataAccessStrategy() { return new HibernateDataAcccessStrategy( localSessionFactoryBean().getObject()); } …
  23. Hibernate ࣮૷ͷ৔߹ SpringDataJdbcConfig.javaʢଓ͖ʣ @Bean LocalSessionFactoryBean localSessionFactoryBean() { LocalSessionFactoryBean localSessionFactoryBean =

    new LocalSessionFactoryBean(); localSessionFactoryBean.setDataSource(datasource); localSessionFactoryBean.setAnnotatedClasses(Employee.class); (தུ) return localSessionFactoryBean; } @Bean HibernateTransactionManager transactionManager( LocalSessionFactoryBean sessionFactory) { HibernateTransactionManager txManager = new HibernateTransactionManager(); txManager.setSessionFactory(sessionFactory.getObject()); return txManager; } spring-boot-starter-data-jpa ͸࢖Θͳ͍ͷͰࣗલఆٛ
  24. Hibernate ࣮૷ͷ৔߹ HibernateDataAcccessStrategy.java public class HibernateDataAcccessStrategy implements DataAccessStrategy { private

    SessionFactory sessionFactory; public HibernateDataAcccessStrategy( SessionFactory sessionFactory) { … } private Session currentSession() { … } @Override public <T> void insert(T instance, Class<T> domainType, Map<String, Object> additionalParameters) { Session session = currentSession(); session.save(instance); session.flush(); } …
  25. Ͷɺ̺ʢུʣ

  26. ͔͍ͨͨͷܗ੻ Employee.java @Entity public class Employee { @org.springframework.data.annotation.Id @javax.persistence.Id @Column(name="employee_number")

    @GeneratedValue(strategy= GenerationType.IDENTITY) private Long employeeNumber;
  27. Spring Data JDBC͸
 ·ͩ·ͩൃల్্

  28. ͜ΜͳIssue͕͋Γ·͢ • Support for ValueObjects/Embbeded • Pagenation abstraction • @Column

    • etc…
  29. ࠓޙʹظ଴ʂʂʂ