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.7k
こわくないソースコードリーディング生活 / 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.8k
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
みんなでプロポーザルを書いてみた
yuriko1211
0
260
Quine, Polyglot, 良いコード
qnighy
4
640
アジャイルを支えるテストアーキテクチャ設計/Test Architecting for Agile
goyoki
9
3.3k
CSC509 Lecture 11
javiergs
PRO
0
180
Snowflake x dbtで作るセキュアでアジャイルなデータ基盤
tsoshiro
2
520
エンジニアとして関わる要件と仕様(公開用)
murabayashi
0
300
とにかくAWS GameDay!AWSは世界の共通言語! / Anyway, AWS GameDay! AWS is the world's lingua franca!
seike460
PRO
1
880
AI時代におけるSRE、 あるいはエンジニアの生存戦略
pyama86
6
1.2k
Webの技術スタックで マルチプラットフォームアプリ開発を可能にするElixirDesktopの紹介
thehaigo
2
1k
Micro Frontends Unmasked Opportunities, Challenges, Alternatives
manfredsteyer
PRO
0
100
ECS Service Connectのこれまでのアップデートと今後のRoadmapを見てみる
tkikuc
2
250
Tauriでネイティブアプリを作りたい
tsucchinoko
0
370
Featured
See All Featured
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
47
2.1k
The Pragmatic Product Professional
lauravandoore
31
6.3k
Statistics for Hackers
jakevdp
796
220k
Making the Leap to Tech Lead
cromwellryan
133
8.9k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
Into the Great Unknown - MozCon
thekraken
32
1.5k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
33k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
27
4.3k
It's Worth the Effort
3n
183
27k
YesSQL, Process and Tooling at Scale
rocio
169
14k
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…
ࠓޙʹظʂʂʂ