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.6k
こわくないソースコードリーディング生活 / JJUG CCC 2019 Fall
rshindo
15
9.1k
開発者のためのSpring Boot Actuator入門 / jsug-2019-08
rshindo
5
5.6k
早わかりSpring Data JDBC / jsug-2019-01
rshindo
5
4k
ふつうの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.1k
Other Decks in Programming
See All in Programming
リリース8年目のサービスの1800個のERBファイルをViewComponentに移行した方法とその結果
katty0324
5
3.6k
CSC305 Lecture 13
javiergs
PRO
0
130
CSC509 Lecture 09
javiergs
PRO
0
110
Vitest Browser Mode への期待 / Vitest Browser Mode
odanado
PRO
2
1.7k
推し活としてのrails new/oshikatsu_ha_iizo
sakahukamaki
3
1.7k
Vue.js学習の振り返り
hiro_xre
2
130
Android 15 でアクションバー表示時にステータスバーが白くなってしまう問題
tonionagauzzi
0
140
WEBエンジニア向けAI活用入門
sutetotanuki
0
300
ECSのサービス間通信 4つの方法を比較する 〜Canary,Blue/Greenも添えて〜
tkikuc
11
2.3k
qmuntal/stateless のススメ
sgash708
0
120
Kotlin2でdataクラスの copyメソッドを禁止する/Data class copy function to have the same visibility as constructor
eichisanden
1
140
Boost Performance and Developer Productivity with Jakarta EE 11
ivargrimstad
0
850
Featured
See All Featured
KATA
mclloyd
29
13k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
790
Build The Right Thing And Hit Your Dates
maggiecrowley
32
2.4k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
228
52k
Adopting Sorbet at Scale
ufuk
73
9k
Docker and Python
trallard
40
3.1k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.4k
The Art of Programming - Codeland 2020
erikaheidi
51
13k
Gamification - CAS2011
davidbonilla
80
5k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5k
Faster Mobile Websites
deanohume
304
30k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
231
17k
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…
ࠓޙʹظʂʂʂ