重构 DAO 基础代码。
This commit is contained in:
@@ -4,7 +4,7 @@ import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
/**
|
||||
* 需要时,当数据操作的行数不符合预期时抛出的异常
|
||||
* 需要时,当数据操作的结果不符合预期时抛出的异常
|
||||
*
|
||||
* <p>
|
||||
* 暂时先这样,后续完善异常体系时可能会更改。
|
||||
@@ -14,18 +14,18 @@ import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
* @see xyz.zhouxy.plusone.util.AssertResult
|
||||
*/
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
public class DataOperationNumberException extends PlusoneException {
|
||||
public class DataOperationResultException extends PlusoneException {
|
||||
|
||||
@java.io.Serial
|
||||
private static final long serialVersionUID = -9220765735990318186L;
|
||||
|
||||
public static final int ERROR_CODE = 4110200;
|
||||
|
||||
public DataOperationNumberException() {
|
||||
super(ERROR_CODE, "数据操作的行数不符合预期");
|
||||
public DataOperationResultException() {
|
||||
super(ERROR_CODE, "数据操作结果不符合预期");
|
||||
}
|
||||
|
||||
public DataOperationNumberException(String message) {
|
||||
public DataOperationResultException(String message) {
|
||||
super(ERROR_CODE, message);
|
||||
}
|
||||
}
|
@@ -1,10 +1,10 @@
|
||||
package xyz.zhouxy.plusone.util;
|
||||
|
||||
|
||||
import xyz.zhouxy.plusone.exception.DataNotExistException;
|
||||
import xyz.zhouxy.plusone.exception.DataOperationNumberException;
|
||||
import xyz.zhouxy.plusone.exception.DataOperationResultException;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* 对数据库执行结果进行判断
|
||||
@@ -17,59 +17,34 @@ public final class AssertResult {
|
||||
throw new IllegalStateException("Utility class");
|
||||
}
|
||||
|
||||
public static void update(boolean expression) {
|
||||
if (!expression) {
|
||||
throw new DataOperationNumberException();
|
||||
public static <E extends Throwable> void isTrue(boolean condition, Supplier<E> e) throws E {
|
||||
if (!condition) {
|
||||
throw e.get();
|
||||
}
|
||||
}
|
||||
|
||||
public static void update(boolean expression, String message) {
|
||||
if (!expression) {
|
||||
throw new DataOperationNumberException(message);
|
||||
}
|
||||
public static <T> void equals(T result, T expectedValue) {
|
||||
isTrue(Objects.equals(result, expectedValue), DataOperationResultException::new);
|
||||
}
|
||||
|
||||
public static void update(Object i, int expectedValue) {
|
||||
if (!Objects.equals(i, expectedValue)) {
|
||||
throw new DataOperationNumberException();
|
||||
}
|
||||
}
|
||||
|
||||
public static void update(Object i, int expectedValue, String format) {
|
||||
if (!Objects.equals(i, expectedValue)) {
|
||||
throw new DataOperationNumberException(String.format(format, i));
|
||||
}
|
||||
public static <T> void equals(T result, T expectedValue, String msgTemplate, Object... args) {
|
||||
isTrue(!Objects.equals(result, expectedValue),
|
||||
() -> new DataOperationResultException(String.format(msgTemplate, args)));
|
||||
}
|
||||
|
||||
public static void updateOneRow(int i) {
|
||||
update(i, 1);
|
||||
equals(i, 1);
|
||||
}
|
||||
|
||||
public static void updateOneRow(Object i, String format) {
|
||||
update(i, 1, format);
|
||||
}
|
||||
|
||||
public static void exist(boolean expression) {
|
||||
if (!expression) {
|
||||
throw new DataNotExistException();
|
||||
}
|
||||
}
|
||||
|
||||
public static void exist(boolean expression, String message) {
|
||||
if (!expression) {
|
||||
throw new DataNotExistException(message);
|
||||
}
|
||||
public static void updateOneRow(int i, String format, Object... args) {
|
||||
equals(i, 1, format, args);
|
||||
}
|
||||
|
||||
public static void nonNull(Object obj) {
|
||||
if (Objects.isNull(obj)) {
|
||||
throw new DataNotExistException();
|
||||
}
|
||||
isTrue(Objects.nonNull(obj), DataNotExistException::new);
|
||||
}
|
||||
|
||||
public static void nonNull(Object obj, String message) {
|
||||
if (Objects.isNull(obj)) {
|
||||
throw new DataNotExistException(message);
|
||||
}
|
||||
isTrue(Objects.nonNull(obj), () -> new DataNotExistException(message));
|
||||
}
|
||||
}
|
||||
|
@@ -16,70 +16,48 @@ import org.springframework.jdbc.core.namedparam.SqlParameterSource;
|
||||
|
||||
import xyz.zhouxy.plusone.domain.Entity;
|
||||
|
||||
public abstract class JdbcEntityDaoSupport<T extends Entity<ID>, ID extends Serializable> {
|
||||
protected final NamedParameterJdbcTemplate jdbc;
|
||||
public abstract class JdbcEntityDaoSupport<T extends Entity<ID>, ID extends Serializable>
|
||||
extends PlusoneJdbcDaoSupport {
|
||||
|
||||
protected RowMapper<T> rowMapper;
|
||||
protected ResultSetExtractor<T> resultSetExtractor;
|
||||
|
||||
protected JdbcEntityDaoSupport(@Nonnull NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
|
||||
this.jdbc = namedParameterJdbcTemplate;
|
||||
super(namedParameterJdbcTemplate);
|
||||
this.rowMapper = (ResultSet rs, int rowNum) -> mapRow(rs);
|
||||
this.resultSetExtractor = (ResultSet rs) -> rs.next() ? mapRow(rs) : null;
|
||||
}
|
||||
|
||||
protected final T queryForObject(String sql) {
|
||||
return this.jdbc.query(sql, this.resultSetExtractor);
|
||||
return queryForObject(sql, this.resultSetExtractor);
|
||||
}
|
||||
|
||||
protected final T queryForObject(String sql, SqlParameterSource paramSource) {
|
||||
return this.jdbc.query(sql, paramSource, this.resultSetExtractor);
|
||||
return queryForObject(sql, paramSource, this.resultSetExtractor);
|
||||
}
|
||||
|
||||
protected final T queryForObject(String sql, String paramName, Object value) {
|
||||
return this.jdbc.query(sql, new MapSqlParameterSource(paramName, value), this.resultSetExtractor);
|
||||
return queryForObject(sql, new MapSqlParameterSource(paramName, value), this.resultSetExtractor);
|
||||
}
|
||||
|
||||
protected final List<T> queryForList(String sql) {
|
||||
return this.jdbc.query(sql, this.rowMapper);
|
||||
return queryForList(sql, this.rowMapper);
|
||||
}
|
||||
|
||||
protected final List<T> queryForList(String sql, SqlParameterSource parameterSource) {
|
||||
return this.jdbc.query(sql, parameterSource, this.rowMapper);
|
||||
return queryForList(sql, parameterSource, this.rowMapper);
|
||||
}
|
||||
|
||||
protected final List<T> queryForList(String sql, String paramName, Object value) {
|
||||
return this.jdbc.query(sql, new MapSqlParameterSource(paramName, value), this.rowMapper);
|
||||
return queryForList(sql, new MapSqlParameterSource(paramName, value), this.rowMapper);
|
||||
}
|
||||
|
||||
protected final Stream<T> queryForStream(String sql, SqlParameterSource parameterSource) {
|
||||
return this.jdbc.queryForStream(sql, parameterSource, this.rowMapper);
|
||||
return queryForStream(sql, parameterSource, this.rowMapper);
|
||||
}
|
||||
|
||||
protected final Stream<T> queryForStream(String sql, String paramName, Object value) {
|
||||
return this.jdbc.queryForStream(sql, new MapSqlParameterSource(paramName, value), this.rowMapper);
|
||||
}
|
||||
|
||||
protected final <E> Stream<E> queryForStream(String sql, SqlParameterSource parameterSource, Class<E> elementType) {
|
||||
return this.jdbc.queryForList(sql, parameterSource, elementType).stream();
|
||||
}
|
||||
|
||||
protected final <E> Stream<E> queryForStream(String sql, String paramName, Object value, Class<E> elementType) {
|
||||
return this.jdbc.queryForList(sql, new MapSqlParameterSource(paramName, value), elementType).stream();
|
||||
}
|
||||
|
||||
protected final boolean queryExists(String sql, SqlParameterSource parameterSource) {
|
||||
Boolean isExists = this.jdbc.query(sql, parameterSource, ResultSet::next);
|
||||
return Boolean.TRUE.equals(isExists);
|
||||
}
|
||||
|
||||
protected final boolean queryExists(String sql, String paramName, Object value) {
|
||||
Boolean isExists = this.jdbc.query(sql, new MapSqlParameterSource(paramName, value), ResultSet::next);
|
||||
return Boolean.TRUE.equals(isExists);
|
||||
}
|
||||
|
||||
protected final int update(String sql, SqlParameterSource parameterSource) {
|
||||
return this.jdbc.update(sql, parameterSource);
|
||||
return queryForStream(sql, new MapSqlParameterSource(paramName, value), this.rowMapper);
|
||||
}
|
||||
|
||||
protected abstract T mapRow(ResultSet rs) throws SQLException;
|
||||
|
@@ -1,5 +1,6 @@
|
||||
package xyz.zhouxy.plusone.jdbc;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
|
||||
|
||||
@@ -16,15 +17,17 @@ import xyz.zhouxy.plusone.spring.SpringContextHolder;
|
||||
*/
|
||||
public final class JdbcFactory {
|
||||
|
||||
private static final ApplicationContext CONTEXT = SpringContextHolder.getContext();
|
||||
|
||||
private JdbcFactory() {
|
||||
throw new IllegalStateException("Utility class");
|
||||
}
|
||||
|
||||
public static JdbcTemplate getJdbcTemplate() {
|
||||
return SpringContextHolder.getContext().getBean(JdbcTemplate.class);
|
||||
return CONTEXT.getBean(JdbcTemplate.class);
|
||||
}
|
||||
|
||||
public static NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() {
|
||||
return SpringContextHolder.getContext().getBean(NamedParameterJdbcTemplate.class);
|
||||
return CONTEXT.getBean(NamedParameterJdbcTemplate.class);
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,174 @@
|
||||
package xyz.zhouxy.plusone.jdbc;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import org.springframework.jdbc.core.ResultSetExtractor;
|
||||
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.namedparam.SqlParameterSource;
|
||||
|
||||
import xyz.zhouxy.plusone.exception.DataOperationResultException;
|
||||
import xyz.zhouxy.plusone.util.NumberUtil;
|
||||
|
||||
public abstract class PlusoneJdbcDaoSupport {
|
||||
protected final NamedParameterJdbcTemplate jdbc;
|
||||
|
||||
protected PlusoneJdbcDaoSupport(@Nonnull NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
|
||||
this.jdbc = namedParameterJdbcTemplate;
|
||||
}
|
||||
|
||||
protected final <T> T queryForObject(String sql, ResultSetExtractor<T> resultSetExtractor) {
|
||||
return this.jdbc.query(sql, resultSetExtractor);
|
||||
}
|
||||
|
||||
protected final <T> T queryForObject(String sql, SqlParameterSource paramSource,
|
||||
ResultSetExtractor<T> resultSetExtractor) {
|
||||
return this.jdbc.query(sql, paramSource, resultSetExtractor);
|
||||
}
|
||||
|
||||
protected final <T> T queryForObject(String sql, String paramName, Object value,
|
||||
ResultSetExtractor<T> resultSetExtractor) {
|
||||
return this.jdbc.query(sql, new MapSqlParameterSource(paramName, value), resultSetExtractor);
|
||||
}
|
||||
|
||||
protected final <T> List<T> queryForList(String sql, RowMapper<T> rowMapper) {
|
||||
return this.jdbc.query(sql, rowMapper);
|
||||
}
|
||||
|
||||
protected final <T> List<T> queryForList(String sql, SqlParameterSource parameterSource, RowMapper<T> rowMapper) {
|
||||
return this.jdbc.query(sql, parameterSource, rowMapper);
|
||||
}
|
||||
|
||||
protected final <T> List<T> queryForList(String sql, String paramName, Object value, RowMapper<T> rowMapper) {
|
||||
return this.jdbc.query(sql, new MapSqlParameterSource(paramName, value), rowMapper);
|
||||
}
|
||||
|
||||
protected final <T> Stream<T> queryForStream(String sql, SqlParameterSource parameterSource,
|
||||
RowMapper<T> rowMapper) {
|
||||
return this.jdbc.queryForStream(sql, parameterSource, rowMapper);
|
||||
}
|
||||
|
||||
protected final <T> Stream<T> queryForStream(String sql, String paramName, Object value, RowMapper<T> rowMapper) {
|
||||
return this.jdbc.queryForStream(sql, new MapSqlParameterSource(paramName, value), rowMapper);
|
||||
}
|
||||
|
||||
protected final <T> Stream<T> queryForStream(String sql, SqlParameterSource parameterSource, Class<T> elementType) {
|
||||
return this.jdbc.queryForList(sql, parameterSource, elementType).stream();
|
||||
}
|
||||
|
||||
protected final <T> Stream<T> queryForStream(String sql, String paramName, Object value, Class<T> elementType) {
|
||||
return this.jdbc.queryForList(sql, new MapSqlParameterSource(paramName, value), elementType).stream();
|
||||
}
|
||||
|
||||
protected final boolean queryExists(String sql, SqlParameterSource parameterSource) {
|
||||
Boolean isExists = this.jdbc.query(sql, parameterSource, ResultSet::next);
|
||||
return Boolean.TRUE.equals(isExists);
|
||||
}
|
||||
|
||||
protected final boolean queryExists(String sql, String paramName, Object value) {
|
||||
Boolean isExists = this.jdbc.query(sql, new MapSqlParameterSource(paramName, value), ResultSet::next);
|
||||
return Boolean.TRUE.equals(isExists);
|
||||
}
|
||||
|
||||
protected final int update(String sql, SqlParameterSource parameterSource) {
|
||||
return this.jdbc.update(sql, parameterSource);
|
||||
}
|
||||
|
||||
protected final int update(String sql, String paramName, Object value) {
|
||||
return this.jdbc.update(sql, new MapSqlParameterSource(paramName, value));
|
||||
}
|
||||
|
||||
protected final int batchUpdate(String sql, SqlParameterSource[] batchArgs) {
|
||||
int[] i = this.jdbc.batchUpdate(sql, batchArgs);
|
||||
return NumberUtil.sum(i);
|
||||
}
|
||||
|
||||
protected final <T> int batchUpdate(String sql, Stream<T> c,
|
||||
@Nonnull Function<T, SqlParameterSource> paramSourceBuilder) {
|
||||
int[] i = this.jdbc.batchUpdate(sql, buildSqlParameterSourceArray(c, paramSourceBuilder));
|
||||
return NumberUtil.sum(i);
|
||||
}
|
||||
|
||||
protected final <T> int batchUpdate(String sql, Collection<T> c,
|
||||
@Nonnull Function<T, SqlParameterSource> paramSourceBuilder) {
|
||||
int[] i = this.jdbc.batchUpdate(sql, buildSqlParameterSourceArray(c, paramSourceBuilder));
|
||||
return NumberUtil.sum(i);
|
||||
}
|
||||
|
||||
protected static final <T> void assertResultEquals(T result, T expectedValue) {
|
||||
if (!Objects.equals(result, expectedValue)) {
|
||||
throw new DataOperationResultException();
|
||||
}
|
||||
}
|
||||
|
||||
protected static final <T> void assertResultEquals(T result, T expectedValue, Function<T, String> errMsg) {
|
||||
if (!Objects.equals(result, expectedValue)) {
|
||||
throw new DataOperationResultException(errMsg.apply(result));
|
||||
}
|
||||
}
|
||||
|
||||
protected static final <T> void assertResultEquals(T result, T expectedValue, String msgTemplate, Object... args) {
|
||||
if (!Objects.equals(result, expectedValue)) {
|
||||
throw new DataOperationResultException(String.format(msgTemplate, args));
|
||||
}
|
||||
}
|
||||
|
||||
protected static final <T, E extends Throwable> void assertResultEqualsOrThrow(T result, T expectedValue,
|
||||
Function<T, E> e) throws E {
|
||||
if (!Objects.equals(result, expectedValue)) {
|
||||
throw e.apply(result);
|
||||
}
|
||||
}
|
||||
|
||||
protected static final void assertUpdateOneRow(int result) {
|
||||
assertResultEquals(result, 1);
|
||||
}
|
||||
|
||||
protected static final void assertUpdateOneRow(int result, Function<Integer, String> errMsg) {
|
||||
assertResultEquals(result, 1, errMsg);
|
||||
}
|
||||
|
||||
protected static final void assertUpdateOneRow(int result, String msgTemplate, Object... args) {
|
||||
assertResultEquals(result, 1, msgTemplate, args);
|
||||
}
|
||||
|
||||
protected static final <E extends Throwable> void assertUpdateOneRowOrThrow(int result, Function<Integer, E> e)
|
||||
throws E {
|
||||
assertResultEqualsOrThrow(result, 1, e);
|
||||
}
|
||||
|
||||
protected static final <T> SqlParameterSource[] buildSqlParameterSourceArray(
|
||||
T[] c,
|
||||
@Nonnull Function<T, SqlParameterSource> paramSourceBuilder) {
|
||||
if (c == null || c.length == 0) {
|
||||
return new SqlParameterSource[] {};
|
||||
}
|
||||
return buildSqlParameterSourceArray(Arrays.stream(c), paramSourceBuilder);
|
||||
}
|
||||
|
||||
protected static final <T> SqlParameterSource[] buildSqlParameterSourceArray(
|
||||
Collection<T> c,
|
||||
@Nonnull Function<T, SqlParameterSource> paramSourceBuilder) {
|
||||
if (c == null || c.isEmpty()) {
|
||||
return new SqlParameterSource[] {};
|
||||
}
|
||||
return buildSqlParameterSourceArray(c.stream(), paramSourceBuilder);
|
||||
}
|
||||
|
||||
protected static final <T> SqlParameterSource[] buildSqlParameterSourceArray(
|
||||
Stream<T> stream,
|
||||
@Nonnull Function<T, SqlParameterSource> paramSourceBuilder) {
|
||||
Objects.requireNonNull(stream);
|
||||
Objects.requireNonNull(paramSourceBuilder);
|
||||
return stream.map(paramSourceBuilder).toArray(SqlParameterSource[]::new);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user