将 find 方法的返回值改为 Optional<T>。
This commit is contained in:
@@ -4,6 +4,7 @@ import java.io.Serializable;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
@@ -20,23 +21,23 @@ public abstract class JdbcEntityDaoSupport<T extends Entity<ID>, ID extends Seri
|
||||
extends PlusoneJdbcDaoSupport {
|
||||
|
||||
protected RowMapper<T> rowMapper;
|
||||
protected ResultSetExtractor<T> resultSetExtractor;
|
||||
protected ResultSetExtractor<Optional<T>> resultSetExtractor;
|
||||
|
||||
protected JdbcEntityDaoSupport(@Nonnull NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
|
||||
super(namedParameterJdbcTemplate);
|
||||
this.rowMapper = (ResultSet rs, int rowNum) -> mapRow(rs);
|
||||
this.resultSetExtractor = (ResultSet rs) -> rs.next() ? mapRow(rs) : null;
|
||||
this.resultSetExtractor = (ResultSet rs) -> rs.next() ? Optional.of(mapRow(rs)) : Optional.empty();
|
||||
}
|
||||
|
||||
protected final T queryForObject(String sql) {
|
||||
protected final Optional<T> queryForObject(String sql) {
|
||||
return queryForObject(sql, this.resultSetExtractor);
|
||||
}
|
||||
|
||||
protected final T queryForObject(String sql, SqlParameterSource paramSource) {
|
||||
protected final Optional<T> queryForObject(String sql, SqlParameterSource paramSource) {
|
||||
return queryForObject(sql, paramSource, this.resultSetExtractor);
|
||||
}
|
||||
|
||||
protected final T queryForObject(String sql, String paramName, Object value) {
|
||||
protected final Optional<T> queryForObject(String sql, String paramName, Object value) {
|
||||
return queryForObject(sql, new MapSqlParameterSource(paramName, value), this.resultSetExtractor);
|
||||
}
|
||||
|
||||
@@ -66,7 +67,7 @@ public abstract class JdbcEntityDaoSupport<T extends Entity<ID>, ID extends Seri
|
||||
this.rowMapper = rowMapper;
|
||||
}
|
||||
|
||||
protected void setResultSetExtractor(@Nonnull ResultSetExtractor<T> resultSetExtractor) {
|
||||
protected void setResultSetExtractor(@Nonnull ResultSetExtractor<Optional<T>> resultSetExtractor) {
|
||||
this.resultSetExtractor = resultSetExtractor;
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package xyz.zhouxy.plusone.jdbc;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
@@ -20,7 +21,7 @@ public abstract class JdbcRepositorySupport<T extends AggregateRoot<ID>, ID exte
|
||||
|
||||
protected abstract void doDelete(@Nonnull T entity);
|
||||
|
||||
protected abstract T doFindById(@Nonnull ID id);
|
||||
protected abstract Optional<T> doFindById(@Nonnull ID id);
|
||||
|
||||
protected abstract T doInsert(@Nonnull T entity);
|
||||
|
||||
@@ -35,7 +36,7 @@ public abstract class JdbcRepositorySupport<T extends AggregateRoot<ID>, ID exte
|
||||
}
|
||||
|
||||
@Override
|
||||
public final T find(ID id) {
|
||||
public final Optional<T> find(ID id) {
|
||||
if (id == null) {
|
||||
throw new IllegalArgumentException("Id cannot be null.");
|
||||
}
|
||||
|
Reference in New Issue
Block a user