Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

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

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

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

Ryo Shindo

April 19, 2018
Tweet

More Decks by Ryo Shindo

Other Decks in Programming

Transcript

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

    <<interface>> Repository ࣮૷ͷ੾Γସ͑͸ BeanఆٛͰߦ͏
  2. EmployeeRepository.java public interface EmployeeRepository extends CrudRepository<Employee, Long> { @Query("select *

    from employee where age > :age") List<Employee> findByAgeOlderThan(@Param(“age") int age); } ͔͍͔ͭͨ
  3. @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); ͔͍͔ͭͨ
  4. Spring JDBC ࣮૷ͷ৔߹ SpringDataJdbcConfig.java @Configuration @EnableJdbcRepositories public class SpringDataJdbcConfig {

    @Bean DataAccessStrategy dataAccessStrategy(JdbcMappingContext context) { return new DefaultDataAccessStrategy( new SqlGeneratorSource(context), context); } }
  5. MyBatis ࣮૷ͷ৔߹ SpringDataJdbcConfig.java @Configuration @EnableJdbcRepositories public class SpringDataJdbcConfig { @Bean

    DataAccessStrategy dataAccessStrategy(SqlSession sqlSession) { return new MyBatisDataAccessStrategy(sqlSession); } }
  6. 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Λఆٛ͢Δ
  7. Hibernate ࣮૷ͷ৔߹ SpringDataJdbcConfig.java @Configuration @EnableJdbcRepositories @EnableAutoConfiguration(exclude = HibernateJpaAutoConfiguration.class) public class

    SpringDataJdbcConfig { @Bean DataAccessStrategy dataAccessStrategy() { return new HibernateDataAcccessStrategy( localSessionFactoryBean().getObject()); } …
  8. 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 ͸࢖Θͳ͍ͷͰࣗલఆٛ
  9. 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(); } …