Skip to content

Commit 5d5ecc3

Browse files
committed
Migrate to JdbcClient
1 parent 95a4b57 commit 5d5ecc3

File tree

5 files changed

+94
-53
lines changed

5 files changed

+94
-53
lines changed

pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<properties>
1818
<java.version>17</java.version>
1919
<ecs-logging-java.version>1.5.0</ecs-logging-java.version>
20+
<spring-framework.version>6.1.0-SNAPSHOT</spring-framework.version>
2021
</properties>
2122
<dependencies>
2223
<dependency>
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package am.ik.note.content;
22

33
import am.ik.note.reader.ReaderId;
4-
import org.springframework.dao.support.DataAccessUtils;
54
import org.springframework.jdbc.core.JdbcTemplate;
65
import org.springframework.jdbc.core.RowMapper;
6+
import org.springframework.jdbc.core.simple.JdbcClient;
77
import org.springframework.stereotype.Repository;
88
import org.springframework.transaction.annotation.Transactional;
99

@@ -12,7 +12,7 @@
1212

1313
@Repository
1414
public class NoteMapper {
15-
private final JdbcTemplate jdbcTemplate;
15+
private final JdbcClient jdbcClient;
1616

1717
private final RowMapper<Note> noteRowMapper = (rs, i) -> {
1818
final NoteId noteId = NoteId.valueOf(rs.getString("note_id"));
@@ -22,54 +22,69 @@ public class NoteMapper {
2222
};
2323

2424
public NoteMapper(JdbcTemplate jdbcTemplate) {
25-
this.jdbcTemplate = jdbcTemplate;
25+
this.jdbcClient = JdbcClient.create(jdbcTemplate);
2626
}
2727

2828
public Optional<Note> findByNoteId(NoteId noteId) {
29-
return DataAccessUtils.optionalResult(this.jdbcTemplate.query("""
29+
return this.jdbcClient.sql("""
3030
SELECT n.note_id, n.entry_id, n.note_url
3131
FROM note AS n
3232
WHERE n.note_id = ?
33-
""", this.noteRowMapper, noteId.toString()));
33+
""") //
34+
.param(noteId.toString()) //
35+
.query(this.noteRowMapper) //
36+
.optional();
3437
}
3538

3639
public Optional<Note> findByEntryId(Long entryId) {
37-
return DataAccessUtils.optionalResult(this.jdbcTemplate.query("""
40+
return this.jdbcClient.sql("""
3841
SELECT n.note_id, n.entry_id, n.note_url
3942
FROM note AS n
4043
WHERE n.entry_id = ?
41-
""", this.noteRowMapper, entryId));
44+
""") //
45+
.param(entryId) //
46+
.query(this.noteRowMapper) //
47+
.optional();
4248
}
4349

4450
public List<NoteSummaryBuilder> findAll(ReaderId readerId) {
45-
return this.jdbcTemplate.query("""
51+
return this.jdbcClient.sql("""
4652
SELECT n.note_id, n.entry_id, n.note_url, nr.reader_id
4753
FROM note AS n
4854
LEFT JOIN note_reader AS nr
4955
ON n.note_id = nr.note_id
5056
AND nr.reader_id = ?
5157
ORDER BY n.entry_id ASC
52-
""", (rs, i) -> {
53-
final NoteId noteId = NoteId.valueOf(rs.getString("note_id"));
54-
final long entryId = rs.getLong("entry_id");
55-
final String noteUrl = rs.getString("note_url");
56-
final boolean isSubscribed = rs.getString("reader_id") != null;
57-
return new NoteSummaryBuilder().withNoteId(noteId).withEntryId(entryId)
58-
.withNoteUrl(noteUrl).withSubscribed(isSubscribed);
59-
}, readerId.toString());
58+
""").param(readerId.toString()) //
59+
.query((rs, i) -> {
60+
final NoteId noteId = NoteId.valueOf(rs.getString("note_id"));
61+
final long entryId = rs.getLong("entry_id");
62+
final String noteUrl = rs.getString("note_url");
63+
final boolean isSubscribed = rs.getString("reader_id") != null;
64+
return new NoteSummaryBuilder().withNoteId(noteId)
65+
.withEntryId(entryId).withNoteUrl(noteUrl)
66+
.withSubscribed(isSubscribed);
67+
}) //
68+
.list();
6069
}
6170

6271
@Transactional
6372
public int insertNote(NoteId noteId, Long entryId, String noteUrl) {
64-
return this.jdbcTemplate.update("""
73+
return this.jdbcClient.sql("""
6574
INSERT INTO note(note_id, entry_id, note_url) VALUES (?, ?, ?)
66-
""", noteId.toString(), entryId, noteUrl);
75+
""") //
76+
.param(noteId.toString()) //
77+
.param(entryId) //
78+
.param(noteUrl) //
79+
.update();
6780
}
6881

6982
@Transactional
7083
public int deleteByEntryId(Long entryId) {
71-
return this.jdbcTemplate.update("""
84+
return this.jdbcClient.sql("""
7285
DELETE FROM note WHERE entry_id = ?
73-
""", entryId);
86+
""") //
87+
.param(entryId) //
88+
.update();
7489
}
7590
}
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,40 @@
11
package am.ik.note.content;
22

33
import am.ik.note.reader.ReaderId;
4-
54
import org.springframework.jdbc.core.JdbcTemplate;
5+
import org.springframework.jdbc.core.simple.JdbcClient;
66
import org.springframework.stereotype.Repository;
77
import org.springframework.transaction.annotation.Transactional;
88

99
@Repository
1010
public class NoteReaderMapper {
11-
private final JdbcTemplate jdbcTemplate;
11+
private final JdbcClient jdbcClient;
1212

1313
public NoteReaderMapper(JdbcTemplate jdbcTemplate) {
14-
this.jdbcTemplate = jdbcTemplate;
14+
this.jdbcClient = JdbcClient.create(jdbcTemplate);
1515
}
1616

1717
public int countByNoteIdAndReaderId(NoteId noteId, ReaderId readerId) {
18-
final Integer count = this.jdbcTemplate.queryForObject("""
18+
final Integer count = this.jdbcClient.sql("""
1919
SELECT COUNT(*)
2020
FROM note_reader AS nr
2121
WHERE nr.note_id = ?
2222
AND nr.reader_id = ?
23-
""", Integer.class, noteId.toString(), readerId.toString());
23+
""").param(noteId.toString()) //
24+
.param(readerId.toString()) //
25+
.query() //
26+
.singleValue(Integer.class);
2427
return count == null ? -1 : count;
2528
}
2629

2730
@Transactional
2831
public int insertNoteReader(NoteId noteId, ReaderId readerId) {
29-
return this.jdbcTemplate.update("""
32+
return this.jdbcClient.sql("""
3033
INSERT INTO note_reader(note_id, reader_id) VALUES (?, ?)
31-
""", noteId.toString(), readerId.toString());
34+
""") //
35+
.param(noteId.toString()) //
36+
.param(readerId.toString()) //
37+
.update();
3238
}
3339

3440
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package am.ik.note.reader;
22

3-
import org.springframework.dao.support.DataAccessUtils;
43
import org.springframework.jdbc.core.JdbcTemplate;
54
import org.springframework.jdbc.core.RowMapper;
5+
import org.springframework.jdbc.core.simple.JdbcClient;
66
import org.springframework.stereotype.Repository;
77
import org.springframework.transaction.annotation.Transactional;
88

@@ -11,7 +11,7 @@
1111

1212
@Repository
1313
public class ReaderMapper {
14-
private final JdbcTemplate jdbcTemplate;
14+
private final JdbcClient jdbcClient;
1515

1616
private final RowMapper<Reader> readerRowMapper = (rs, i) -> {
1717
final String email = rs.getString("email");
@@ -23,57 +23,71 @@ public class ReaderMapper {
2323
};
2424

2525
public ReaderMapper(JdbcTemplate jdbcTemplate) {
26-
this.jdbcTemplate = jdbcTemplate;
26+
this.jdbcClient = JdbcClient.create(jdbcTemplate);
2727
}
2828

2929
public Optional<Reader> findById(ReaderId readerId) {
30-
return DataAccessUtils.optionalResult(this.jdbcTemplate.query(
31-
"""
30+
return this.jdbcClient
31+
.sql("""
3232
SELECT r.reader_id, r.email, rp.hashed_password, r.reader_state, r.created_at
3333
FROM reader AS r
3434
LEFT JOIN reader_password rp on r.reader_id = rp.reader_id
3535
WHERE r.reader_id = ?
36-
""",
37-
this.readerRowMapper, readerId.toString()));
36+
""") //
37+
.param(readerId.toString()) //
38+
.query(this.readerRowMapper) //
39+
.optional();
3840
}
3941

4042
public Optional<Reader> findByEmail(String email) {
41-
return DataAccessUtils.optionalResult(this.jdbcTemplate.query(
42-
"""
43+
return this.jdbcClient
44+
.sql("""
4345
SELECT r.reader_id, r.email, rp.hashed_password, r.reader_state, r.created_at
4446
FROM reader AS r
4547
LEFT JOIN reader_password rp on r.reader_id = rp.reader_id
4648
WHERE r.email = ?
47-
""",
48-
this.readerRowMapper, email));
49+
""") //
50+
.param(email) //
51+
.query(this.readerRowMapper) //
52+
.optional();
4953
}
5054

5155
public List<Reader> findAll() {
52-
return this.jdbcTemplate.query("""
56+
return this.jdbcClient.sql("""
5357
SELECT reader_id, email, '' AS hashed_password, reader_state, created_at
5458
FROM reader
5559
ORDER BY created_at DESC
56-
""", this.readerRowMapper);
60+
""") //
61+
.query(this.readerRowMapper) //
62+
.list();
5763
}
5864

5965
@Transactional
6066
public int insert(ReaderId readerId, String email) {
61-
return this.jdbcTemplate.update("""
67+
return this.jdbcClient.sql("""
6268
INSERT INTO reader(reader_id, email) VALUES(?, ?)
63-
""", readerId.toString(), email);
69+
""") //
70+
.param(readerId.toString()) //
71+
.param(email) //
72+
.update();
6473
}
6574

6675
@Transactional
6776
public int updateReaderState(ReaderId readerId, ReaderState readerState) {
68-
return this.jdbcTemplate.update("""
77+
return this.jdbcClient.sql("""
6978
UPDATE reader SET reader_state = ? WHERE reader_id = ?
70-
""", readerState.name(), readerId.toString());
79+
""") //
80+
.param(readerState.name()) //
81+
.param(readerId.toString()) //
82+
.update();
7183
}
7284

7385
@Transactional
7486
public int deleteByEmail(String email) {
75-
return this.jdbcTemplate.update("""
87+
return this.jdbcClient.sql("""
7688
DELETE FROM reader WHERE email = ?
77-
""", email);
89+
""") //
90+
.param(email) //
91+
.update();
7892
}
7993
}
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,34 @@
11
package am.ik.note.reader;
22

33
import org.springframework.jdbc.core.JdbcTemplate;
4+
import org.springframework.jdbc.core.simple.JdbcClient;
45
import org.springframework.stereotype.Repository;
56
import org.springframework.transaction.annotation.Transactional;
67

78
@Repository
89
public class ReaderPasswordMapper {
9-
private final JdbcTemplate jdbcTemplate;
10+
private final JdbcClient jdbcClient;
1011

1112
public ReaderPasswordMapper(JdbcTemplate jdbcTemplate) {
12-
this.jdbcTemplate = jdbcTemplate;
13+
this.jdbcClient = JdbcClient.create(jdbcTemplate);
1314
}
1415

1516
@Transactional
1617
public int insert(ReaderPassword readerPassword) {
17-
return this.jdbcTemplate.update("""
18+
return this.jdbcClient.sql("""
1819
INSERT INTO reader_password(reader_id, hashed_password) VALUES (?, ?)
19-
""", readerPassword.readerId().toString(),
20-
readerPassword.hashedPassword());
20+
""") //
21+
.param(readerPassword.readerId().toString()) //
22+
.param(readerPassword.hashedPassword()) //
23+
.update();
2124
}
2225

2326
@Transactional
2427
public int deleteByReaderId(ReaderId readerId) {
25-
return this.jdbcTemplate.update("""
28+
return this.jdbcClient.sql("""
2629
DELETE FROM reader_password WHERE reader_id = ?
27-
""", readerId.toString());
30+
""") //
31+
.param(readerId.toString()) //
32+
.update();
2833
}
2934
}

0 commit comments

Comments
 (0)