Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Spring Data JDBCを使い倒す! / Intruduction to Spring...
Search
Ryo Shindo
April 19, 2018
Programming
4
2.2k
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
Share
More Decks by Ryo Shindo
See All by Ryo Shindo
今こそ知りたいSpring Test / Spring Fest 2020
rshindo
11
5.8k
こわくないソースコードリーディング生活 / JJUG CCC 2019 Fall
rshindo
15
9.2k
開発者のためのSpring Boot Actuator入門 / jsug-2019-08
rshindo
5
5.7k
早わかりSpring Data JDBC / jsug-2019-01
rshindo
5
4.1k
ふつうのJavaアプリ開発のための自動テスト戦略 / JJUG CCC 2018 Fall
rshindo
9
4.9k
Spring WebFluxで学ぶReactive Application / Introduction to Reactive
rshindo
1
2.2k
まだまだ間に合う!JUnit 5入門 / JJUG CCC 2018 Spring
rshindo
6
3.2k
Other Decks in Programming
See All in Programming
LLM Supervised Fine-tuningの理論と実践
datanalyticslabo
5
1.2k
命名をリントする
chiroruxx
1
410
短期間での新規プロダクト開発における「コスパの良い」Goのテスト戦略」 / kamakura.go
n3xem
2
170
tidymodelsによるtidyな生存時間解析 / Japan.R2024
dropout009
1
780
Fibonacci Function Gallery - Part 1
philipschwarz
PRO
0
220
PHPとAPI Platformで作る本格的なWeb APIアプリケーション(入門編) / phpcon 2024 Intro to API Platform
ttskch
0
230
複雑な仕様に立ち向かうアーキテクチャ
myohei
0
170
useSyncExternalStoreを使いまくる
ssssota
6
1k
PHPUnitしか使ってこなかった 一般PHPerがPestに乗り換えた実録
mashirou1234
0
190
Go の GC の不得意な部分を克服したい
taiyow
3
790
Spatial Rendering for Apple Vision Pro
warrenm
0
110
責務を分離するための例外設計 - PHPカンファレンス 2024
kajitack
6
890
Featured
See All Featured
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
132
33k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.5k
Gamification - CAS2011
davidbonilla
80
5.1k
Optimising Largest Contentful Paint
csswizardry
33
3k
Fashionably flexible responsive web design (full day workshop)
malarkey
405
66k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
45
2.2k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
1.9k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
44
9.3k
A better future with KSS
kneath
238
17k
How to Ace a Technical Interview
jacobian
276
23k
Transcript
Spring Data JDBCΛ ͍͢ʂ Acroquest Technologyגࣜձࣾ ਐ౻ɹྒྷ
ࣗݾհ • ਐ౻ɹྒྷ @shindo_ryo • Acroquest Technologyגࣜձࣾ • (ݩ)จܥJavaΤϯδχΞ
Spring Data JDBC is Կ
Spring ͷ DB ΞΫηεϥΠϒϥϦ (·ͩ1.0.0.BUILD-SNAPSHOT)
͏͋Δ͡ΌΜ • Spring JDBC (JdbcTemplate) • mybatis-spring • Spring Data
JPA
Կ͕ҧ͏ͷ͔
Spring Data JDBC DBΞΫηεͷ ࣮Λఏڙ͠ͳ͍
Spring Data JDBC Ξμϓλʔ Spring Data JDBC Spring JDBC MyBatis
<<interface>> Repository ࣮ͷΓସ͑ BeanఆٛͰߦ͏
;Θͬͱݴ͏ͱɺ DDDຊͷRepository ύλʔϯΛ ͖ͳ࣮Ͱ؆୯ʹͭ͘ΕΔΞϨ
EmployeeRepository.java public interface EmployeeRepository extends CrudRepository<Employee, Long> { @Query("select *
from employee where age > :age") List<Employee> findByAgeOlderThan(@Param(“age") int age); } ͔͍͔ͭͨ
@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); ͔͍͔ͭͨ
Spring JDBC ࣮ͷ߹ SpringDataJdbcConfig.java @Configuration @EnableJdbcRepositories public class SpringDataJdbcConfig {
@Bean DataAccessStrategy dataAccessStrategy(JdbcMappingContext context) { return new DefaultDataAccessStrategy( new SqlGeneratorSource(context), context); } }
Ͷɺ؆୯Ͱ͠ΐ͏ʁ
MyBatis ࣮ͷ߹ SpringDataJdbcConfig.java @Configuration @EnableJdbcRepositories public class SpringDataJdbcConfig { @Bean
DataAccessStrategy dataAccessStrategy(SqlSession sqlSession) { return new MyBatisDataAccessStrategy(sqlSession); } }
application.properties mybatis.mapper-locations= classpath:/com/example/demo/mapper/*Mapper.xml ɹɹɹˡMapperϑΝΠϧͷϩέʔγϣϯΛࢦఆ͢Δ mybatis.type-aliases-package=com.example.demo ɹɹɹˡEntityΫϥεͷύοέʔδΛࢦఆ͢Δ MyBatis ࣮ͷ߹
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Λఆٛ͢Δ
Ͷɺ؆୯Ͱʢུʣ
جຊతʹɺDataAccessStrategy ͷ࣮ΛΓସ͑Δ͚ͩ
DataAccessStrategy ΛࣗͰ࣮͢Ε ͲΜͳORMʹ͑Δ
ͪΖΜHibernateΠέΔ
ͦΕԿͯ Spring Data JPA
Hibernate ࣮ͷ߹ SpringDataJdbcConfig.java @Configuration @EnableJdbcRepositories @EnableAutoConfiguration(exclude = HibernateJpaAutoConfiguration.class) public class
SpringDataJdbcConfig { @Bean DataAccessStrategy dataAccessStrategy() { return new HibernateDataAcccessStrategy( localSessionFactoryBean().getObject()); } …
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 Θͳ͍ͷͰࣗલఆٛ
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(); } …
Ͷɺ̺ʢུʣ
͔͍ͨͨͷܗ 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;
Spring Data JDBC ·ͩ·ͩൃల్্
͜ΜͳIssue͕͋Γ·͢ • Support for ValueObjects/Embbeded • Pagenation abstraction • @Column
• etc…
ࠓޙʹظʂʂʂ