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
Less waste, more joy, and a lot more green: How Quarkus makes Java better
hollycummins
0
100
弊社の「意識チョット低いアーキテクチャ」10選
texmeijin
5
24k
Quine, Polyglot, 良いコード
qnighy
4
640
AWS Lambdaから始まった Serverlessの「熱」とキャリアパス / It started with AWS Lambda Serverless “fever” and career path
seike460
PRO
1
260
ふかぼれ!CSSセレクターモジュール / Fukabore! CSS Selectors Module
petamoriken
0
150
watsonx.ai Dojo #4 生成AIを使ったアプリ開発、応用編
oniak3ibm
PRO
1
100
CSC509 Lecture 09
javiergs
PRO
0
140
LLM生成文章の精度評価自動化とプロンプトチューニングの効率化について
layerx
PRO
2
190
Click-free releases & the making of a CLI app
oheyadam
2
110
Amazon Qを使ってIaCを触ろう!
maruto
0
400
AI時代におけるSRE、 あるいはエンジニアの生存戦略
pyama86
6
1.1k
ヤプリ新卒SREの オンボーディング
masaki12
0
130
Featured
See All Featured
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
Documentation Writing (for coders)
carmenintech
65
4.4k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
47
2.1k
Building Your Own Lightsaber
phodgson
103
6.1k
Six Lessons from altMBA
skipperchong
27
3.5k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
It's Worth the Effort
3n
183
27k
Designing the Hi-DPI Web
ddemaree
280
34k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
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…
ࠓޙʹظʂʂʂ