优化代码。

dev
ZhouXY108 2023-06-29 01:43:43 +08:00
parent dcb86f9100
commit 79466d180f
2 changed files with 17 additions and 16 deletions

View File

@ -8,6 +8,9 @@ import javax.annotation.Nonnull;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource; import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import com.google.common.base.Preconditions;
import xyz.zhouxy.plusone.commons.annotation.Overridable;
import xyz.zhouxy.plusone.domain.AggregateRoot; import xyz.zhouxy.plusone.domain.AggregateRoot;
import xyz.zhouxy.plusone.domain.IRepository; import xyz.zhouxy.plusone.domain.IRepository;
@ -19,35 +22,33 @@ public abstract class JdbcRepositorySupport<T extends AggregateRoot<ID>, ID exte
super(namedParameterJdbcTemplate); super(namedParameterJdbcTemplate);
} }
@Overridable
protected abstract void doDelete(@Nonnull T entity); protected abstract void doDelete(@Nonnull T entity);
@Overridable
protected abstract Optional<T> doFindById(@Nonnull ID id); protected abstract Optional<T> doFindById(@Nonnull ID id);
@Overridable
protected abstract T doInsert(@Nonnull T entity); protected abstract T doInsert(@Nonnull T entity);
@Overridable
protected abstract T doUpdate(@Nonnull T entity); protected abstract T doUpdate(@Nonnull T entity);
@Override @Override
public final void delete(T entity) { public final void delete(T entity) {
if (entity == null) { Preconditions.checkArgument(entity != null, "Cannot delete null.");
throw new IllegalArgumentException("Cannot delete null.");
}
doDelete(entity); doDelete(entity);
} }
@Override @Override
public final Optional<T> find(ID id) { public final Optional<T> find(ID id) {
if (id == null) { Preconditions.checkArgument(id != null, "Id cannot be null.");
throw new IllegalArgumentException("Id cannot be null.");
}
return doFindById(id); return doFindById(id);
} }
@Override @Override
public final T save(T entity) { public final T save(T entity) {
if (entity == null) { Preconditions.checkArgument(entity != null, "Cannot save null.");
throw new IllegalArgumentException("Cannot save null.");
}
return entity.getId().isPresent() ? doUpdate(entity) : doInsert(entity); return entity.getId().isPresent() ? doUpdate(entity) : doInsert(entity);
} }

View File

@ -7,6 +7,7 @@ import java.sql.SQLException;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.Set;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -94,13 +95,12 @@ public class DictRepositoryImpl extends JdbcRepositorySupport<Dict, Long> implem
@Override @Override
protected final Dict mapRow(ResultSet rs) throws SQLException { protected final Dict mapRow(ResultSet rs) throws SQLException {
long id = rs.getLong("id"); final long id = rs.getLong("id");
return new Dict( final String dictType = rs.getString("dict_type");
id, final String dictLabel = rs.getString("dict_label");
rs.getString("dict_type"), final Set<DictValue> values = this.dictValueDAO.selectDictValuesByDictId(id);
rs.getString("dict_label"), final long version = rs.getLong("version");
this.dictValueDAO.selectDictValuesByDictId(id), return new Dict(id, dictType, dictLabel, values, version);
rs.getLong("version"));
} }
@Override @Override