Skip to content

Commit

Permalink
Migrate to JdbcClient
Browse files Browse the repository at this point in the history
  • Loading branch information
making committed Jul 26, 2023
1 parent 7fb1c5d commit b2303de
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 52 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<start-class>am.ik.blog.BlogApiApplication</start-class>
<spring-doc.version>2.1.0</spring-doc.version>
<mybatis-thymeleaf.version>1.0.4</mybatis-thymeleaf.version>
<spring-framework.version>6.1.0-SNAPSHOT</spring-framework.version>
<jar-classifier></jar-classifier>
</properties>

Expand Down
38 changes: 19 additions & 19 deletions src/main/java/am/ik/blog/category/CategoryMapper.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
package am.ik.blog.category;

import java.sql.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import am.ik.blog.util.FileLoader;
import org.mybatis.scripting.thymeleaf.SqlGenerator;

import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.simple.JdbcClient;
import org.springframework.stereotype.Repository;

import java.sql.Array;
import java.util.ArrayList;
import java.util.List;

import static am.ik.blog.util.FileLoader.loadSqlAsString;

@Repository
public class CategoryMapper {
private final NamedParameterJdbcTemplate jdbcTemplate;
private final JdbcClient jdbcClient;

private final SqlGenerator sqlGenerator;

public CategoryMapper(NamedParameterJdbcTemplate jdbcTemplate,
SqlGenerator sqlGenerator) {
this.jdbcTemplate = jdbcTemplate;
this.jdbcClient = JdbcClient.create(jdbcTemplate);
this.sqlGenerator = sqlGenerator;
}

Expand All @@ -32,15 +30,17 @@ public List<List<Category>> findAll(String tenantId) {
final String sql = this.sqlGenerator.generate(
loadSqlAsString("am/ik/blog/category/CategoryMapper/findAll.sql"),
params.getValues(), params::addValue);
return this.jdbcTemplate.query(sql, params, (rs, rowNum) -> {
final List<Category> categories = new ArrayList<>();
final Array categoriesArray = rs.getArray("categories");
if (categoriesArray != null) {
for (Object category : ((Object[]) categoriesArray.getArray())) {
categories.add(new Category((String) category));
}
}
return categories;
});
return this.jdbcClient.sql(sql) //
.paramSource(params) //
.query((rs, rowNum) -> {
final List<Category> categories = new ArrayList<>();
final Array categoriesArray = rs.getArray("categories");
if (categoriesArray != null) {
for (Object category : ((Object[]) categoriesArray.getArray())) {
categories.add(new Category((String) category));
}
}
return categories;
}).list();
}
}
47 changes: 23 additions & 24 deletions src/main/java/am/ik/blog/entry/EntryMapper.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
package am.ik.blog.entry;

import java.sql.Timestamp;
import java.time.ZoneOffset;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import am.ik.blog.category.Category;
import am.ik.blog.entry.keyword.KeywordExtractor;
import am.ik.blog.entry.search.SearchCriteria;
Expand All @@ -17,20 +8,23 @@
import am.ik.pagination.OffsetPageRequest;
import am.ik.yavi.core.ConstraintViolationsException;
import org.mybatis.scripting.thymeleaf.SqlGenerator;

import org.springframework.dao.support.DataAccessUtils;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.simple.JdbcClient;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.time.ZoneOffset;
import java.util.*;

import static am.ik.blog.util.FileLoader.loadSqlAsString;
import static java.util.stream.Collectors.joining;

@Repository
public class EntryMapper {
private final NamedParameterJdbcTemplate jdbcTemplate;
private final JdbcClient jdbcClient;

private final SqlGenerator sqlGenerator;

Expand Down Expand Up @@ -58,7 +52,7 @@ public class EntryMapper {

public EntryMapper(NamedParameterJdbcTemplate jdbcTemplate, SqlGenerator sqlGenerator,
KeywordExtractor keywordExtractor) {
this.jdbcTemplate = jdbcTemplate;
this.jdbcClient = JdbcClient.create(jdbcTemplate);
this.sqlGenerator = sqlGenerator;
this.keywordExtractor = keywordExtractor;
}
Expand All @@ -72,8 +66,7 @@ public Optional<Entry> findOne(Long entryId, String tenantId,
final String sql = this.sqlGenerator.generate(
loadSqlAsString("am/ik/blog/entry/EntryMapper/findOne.sql"),
params.getValues(), params::addValue);
return DataAccessUtils
.optionalResult(this.jdbcTemplate.query(sql, params, rowMapper));
return this.jdbcClient.sql(sql).paramSource(params).query(rowMapper).optional();
}

@Transactional(readOnly = true)
Expand All @@ -89,7 +82,7 @@ public List<Entry> findAll(SearchCriteria searchCriteria, String tenantId,
final String sql = this.sqlGenerator.generate(
loadSqlAsString("am/ik/blog/entry/EntryMapper/findAll.sql"),
params.getValues(), params::addValue);
return this.jdbcTemplate.query(sql, params, rowMapper);
return this.jdbcClient.sql(sql).paramSource(params).query(rowMapper).list();
}

@Transactional(readOnly = true)
Expand All @@ -107,8 +100,10 @@ public long count(SearchCriteria searchCriteria, String tenantId) {
final String sql = this.sqlGenerator.generate(
loadSqlAsString("am/ik/blog/entry/EntryMapper/count.sql"),
params.getValues(), params::addValue);
final Long count = this.jdbcTemplate.queryForObject(sql, params,
(rs, rowNum) -> rs.getLong("count"));
final Long count = this.jdbcClient.sql(sql) //
.paramSource(params) //
.query((rs, rowNum) -> rs.getLong("count")) //
.single();
return Objects.<Long> requireNonNullElse(count, 0L);
}

Expand All @@ -118,8 +113,8 @@ public long nextId(String tenantId) {
final String sql = this.sqlGenerator.generate(
loadSqlAsString("am/ik/blog/entry/EntryMapper/nextId.sql"),
params.getValues(), params::addValue);
final Long nextId = this.jdbcTemplate.queryForObject(sql, Map.of(),
(rs, i) -> rs.getLong("next"));
final Long nextId = this.jdbcClient.sql(sql) //
.query((rs, i) -> rs.getLong("next")).single();
return Objects.<Long> requireNonNullElse(nextId, 1L);
}

Expand All @@ -130,7 +125,7 @@ public int delete(Long entryId, String tenantId) {
final String sql = this.sqlGenerator.generate(
loadSqlAsString("am/ik/blog/entry/EntryMapper/deleteEntry.sql"),
params.getValues(), params::addValue);
return this.jdbcTemplate.update(sql, params);
return this.jdbcClient.sql(sql).paramSource(params).update();
}

@Transactional
Expand Down Expand Up @@ -160,7 +155,9 @@ public Map<String, Integer> save(Entry entry, String tenantId) {
final String upsertEntrySql = this.sqlGenerator.generate(
loadSqlAsString("am/ik/blog/entry/EntryMapper/upsertEntry.sql"),
params.getValues(), params::addValue);
final int upsertEntryCount = this.jdbcTemplate.update(upsertEntrySql, params);
final int upsertEntryCount = this.jdbcClient.sql(upsertEntrySql) //
.paramSource(params) //
.update();
result.put("upsertEntry", upsertEntryCount);
return result;
}
Expand All @@ -174,8 +171,10 @@ private List<Long> entryIds(SearchCriteria searchCriteria, String tenantId,
params.getValues(), params::addValue)
+ " LIMIT %d OFFSET %d".formatted(pageRequest.pageSize(),
pageRequest.offset());
return this.jdbcTemplate.query(sql, params,
(rs, rowNum) -> rs.getLong("entry_id"));
return this.jdbcClient.sql(sql) //
.paramSource(params) //
.query((rs, rowNum) -> rs.getLong("entry_id")) //
.list();
}

private static MapSqlParameterSource entryIdsParameterSource(List<Long> ids) {
Expand Down
19 changes: 10 additions & 9 deletions src/main/java/am/ik/blog/tag/TagMapper.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
package am.ik.blog.tag;

import java.util.List;

import am.ik.blog.util.FileLoader;
import org.mybatis.scripting.thymeleaf.SqlGenerator;

import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.simple.JdbcClient;
import org.springframework.stereotype.Repository;

import java.util.List;

import static am.ik.blog.util.FileLoader.loadSqlAsString;

@Repository
public class TagMapper {
private final NamedParameterJdbcTemplate jdbcTemplate;
private final JdbcClient jdbcClient;

private final SqlGenerator sqlGenerator;

public TagMapper(NamedParameterJdbcTemplate jdbcTemplate, SqlGenerator sqlGenerator) {
this.jdbcTemplate = jdbcTemplate;
this.jdbcClient = JdbcClient.create(jdbcTemplate);
this.sqlGenerator = sqlGenerator;
}

Expand All @@ -28,8 +27,10 @@ public List<TagNameAndCount> findOrderByTagNameAsc(String tenantId) {
final String sql = this.sqlGenerator.generate(
loadSqlAsString("am/ik/blog/tag/TagMapper/findOrderByTagNameAsc.sql"),
params.getValues(), params::addValue);
return this.jdbcTemplate.query(sql, params,
(rs, rowNum) -> new TagNameAndCount(rs.getString("tag_name"),
rs.getInt("count")));
return this.jdbcClient.sql(sql) //
.paramSource(params) //
.query((rs, rowNum) -> new TagNameAndCount(rs.getString("tag_name"),
rs.getInt("count")))
.list();
}
}

0 comments on commit b2303de

Please sign in to comment.