Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥
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
.NET Conf 2024の振り返り
tomokusaba
0
200
気をつけたい!Desktop対応で陥りやすい罠とその対策
goto_tsl
0
200
社内活動の取り組み紹介 ~ スリーシェイクでこんな取り組みしてます ~
bells17
0
390
Cursorでアプリケーションの追加開発や保守をどこまでできるか試したら得るものが多かった話
drumnistnakano
0
280
プロダクトの品質に コミットする / Commit to Product Quality
pekepek
1
690
Java 23の概要とJava Web Frameworkの現状 / Java 23 and Java web framework
kishida
2
380
Modular Monolith Monorepo ~シンプルさを保ちながらmonorepoのメリットを最大化する~
yuisakamoto
11
4k
layerx_20241129.pdf
kyoheig3
2
260
カンファレンスの「アレ」Webでなんとかしませんか? / Conference “thing” Why don't you do something about it on the Web?
dero1to
2
160
PipeCDの歩き方
kuro_kurorrr
4
140
Jakarta EE meets AI
ivargrimstad
0
910
Symfony Mapper Component
soyuka
2
550
Featured
See All Featured
How to Think Like a Performance Engineer
csswizardry
21
1.2k
Speed Design
sergeychernyshev
25
650
GitHub's CSS Performance
jonrohan
1030
460k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
26
1.8k
Embracing the Ebb and Flow
colly
84
4.5k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.2k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
110
49k
YesSQL, Process and Tooling at Scale
rocio
169
14k
Testing 201, or: Great Expectations
jmmastey
40
7.1k
Optimising Largest Contentful Paint
csswizardry
33
3k
Docker and Python
trallard
41
3.1k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
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…
ࠓޙʹظʂʂʂ