新增查询 boolean 的方法,并简单重构代码。
This commit is contained in:
parent
01fda84276
commit
ce86d97e38
@ -26,10 +26,6 @@ import java.util.ArrayList;
|
|||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.OptionalDouble;
|
|
||||||
import java.util.OptionalInt;
|
|
||||||
import java.util.OptionalLong;
|
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
@ -38,7 +34,6 @@ import com.google.common.collect.Lists;
|
|||||||
|
|
||||||
import xyz.zhouxy.plusone.commons.collection.CollectionTools;
|
import xyz.zhouxy.plusone.commons.collection.CollectionTools;
|
||||||
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
||||||
import xyz.zhouxy.plusone.commons.util.OptionalTools;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* JdbcOperationSupport
|
* JdbcOperationSupport
|
||||||
@ -111,14 +106,14 @@ class JdbcOperationSupport {
|
|||||||
// #region - queryFirst
|
// #region - queryFirst
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 执行查询,将查询结果的第一行数据按照指定逻辑进行处理,返回 {@link Optional}
|
* 执行查询,将查询结果的第一行数据按照指定逻辑进行映射
|
||||||
*
|
*
|
||||||
* @param conn 数据库连接
|
* @param conn 数据库连接
|
||||||
* @param sql SQL
|
* @param sql SQL
|
||||||
* @param params 参数
|
* @param params 参数
|
||||||
* @param rowMapper {@link ResultSet} 中每一行的数据的处理逻辑
|
* @param rowMapper {@link ResultSet} 中每一行的数据的处理逻辑
|
||||||
*/
|
*/
|
||||||
static <T> Optional<T> queryFirst(Connection conn, String sql, Object[] params, RowMapper<T> rowMapper)
|
static <T> T queryFirst(Connection conn, String sql, Object[] params, RowMapper<T> rowMapper)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
assertConnectionNotNull(conn);
|
assertConnectionNotNull(conn);
|
||||||
assertSqlNotNull(sql);
|
assertSqlNotNull(sql);
|
||||||
@ -134,7 +129,7 @@ class JdbcOperationSupport {
|
|||||||
* @param params 参数
|
* @param params 参数
|
||||||
* @param clazz 目标类型
|
* @param clazz 目标类型
|
||||||
*/
|
*/
|
||||||
static <T> Optional<T> queryFirst(Connection conn, String sql, Object[] params, Class<T> clazz)
|
static <T> T queryFirst(Connection conn, String sql, Object[] params, Class<T> clazz)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
assertConnectionNotNull(conn);
|
assertConnectionNotNull(conn);
|
||||||
assertSqlNotNull(sql);
|
assertSqlNotNull(sql);
|
||||||
@ -149,7 +144,7 @@ class JdbcOperationSupport {
|
|||||||
* @param sql SQL
|
* @param sql SQL
|
||||||
* @param params 参数
|
* @param params 参数
|
||||||
*/
|
*/
|
||||||
static Optional<String> queryFirstString(Connection conn, String sql, Object[] params)
|
static String queryFirstString(Connection conn, String sql, Object[] params)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
return queryFirst(conn, sql, params, (rs, rowNumber) -> rs.getString(1));
|
return queryFirst(conn, sql, params, (rs, rowNumber) -> rs.getString(1));
|
||||||
}
|
}
|
||||||
@ -161,10 +156,9 @@ class JdbcOperationSupport {
|
|||||||
* @param sql SQL
|
* @param sql SQL
|
||||||
* @param params 参数
|
* @param params 参数
|
||||||
*/
|
*/
|
||||||
static OptionalInt queryFirstInt(Connection conn, String sql, Object[] params)
|
static Integer queryFirstInt(Connection conn, String sql, Object[] params)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
Optional<Integer> result = queryFirst(conn, sql, params, (rs, rowNumber) -> rs.getInt(1));
|
return queryFirst(conn, sql, params, (rs, rowNumber) -> rs.getInt(1));
|
||||||
return OptionalTools.toOptionalInt(result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -174,10 +168,9 @@ class JdbcOperationSupport {
|
|||||||
* @param sql SQL
|
* @param sql SQL
|
||||||
* @param params 参数
|
* @param params 参数
|
||||||
*/
|
*/
|
||||||
static OptionalLong queryFirstLong(Connection conn, String sql, Object[] params)
|
static Long queryFirstLong(Connection conn, String sql, Object[] params)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
Optional<Long> result = queryFirst(conn, sql, params, (rs, rowNumber) -> rs.getLong(1));
|
return queryFirst(conn, sql, params, (rs, rowNumber) -> rs.getLong(1));
|
||||||
return OptionalTools.toOptionalLong(result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -187,10 +180,9 @@ class JdbcOperationSupport {
|
|||||||
* @param sql SQL
|
* @param sql SQL
|
||||||
* @param params 参数
|
* @param params 参数
|
||||||
*/
|
*/
|
||||||
static OptionalDouble queryFirstDouble(Connection conn, String sql, Object[] params)
|
static Double queryFirstDouble(Connection conn, String sql, Object[] params)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
Optional<Double> result = queryFirst(conn, sql, params, (rs, rowNumber) -> rs.getDouble(1));
|
return queryFirst(conn, sql, params, (rs, rowNumber) -> rs.getDouble(1));
|
||||||
return OptionalTools.toOptionalDouble(result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -200,11 +192,23 @@ class JdbcOperationSupport {
|
|||||||
* @param sql SQL
|
* @param sql SQL
|
||||||
* @param params 参数
|
* @param params 参数
|
||||||
*/
|
*/
|
||||||
static Optional<BigDecimal> queryFirstBigDecimal(Connection conn, String sql, Object[] params)
|
static BigDecimal queryFirstBigDecimal(Connection conn, String sql, Object[] params)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
return queryFirst(conn, sql, params, (rs, rowNumber) -> rs.getBigDecimal(1));
|
return queryFirst(conn, sql, params, (rs, rowNumber) -> rs.getBigDecimal(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询结果,并转换为 bool 值
|
||||||
|
*
|
||||||
|
* @param conn 数据库连接
|
||||||
|
* @param sql SQL
|
||||||
|
* @param params 参数
|
||||||
|
*/
|
||||||
|
static Boolean queryFirstBoolean(Connection conn, String sql, Object[] params)
|
||||||
|
throws SQLException {
|
||||||
|
return queryFirst(conn, sql, params, (rs, rowNumber) -> rs.getBoolean(1));
|
||||||
|
}
|
||||||
|
|
||||||
// #endregion
|
// #endregion
|
||||||
|
|
||||||
// #region - update & batchUpdate
|
// #region - update & batchUpdate
|
||||||
@ -357,9 +361,9 @@ class JdbcOperationSupport {
|
|||||||
* @param resultHandler 结果处理器,用于处理 {@link ResultSet}
|
* @param resultHandler 结果处理器,用于处理 {@link ResultSet}
|
||||||
*/
|
*/
|
||||||
private static <T> T queryInternal(@Nonnull Connection conn,
|
private static <T> T queryInternal(@Nonnull Connection conn,
|
||||||
@Nonnull String sql,
|
@Nonnull String sql,
|
||||||
@Nullable Object[] params,
|
@Nullable Object[] params,
|
||||||
@Nonnull ResultHandler<T> resultHandler)
|
@Nonnull ResultHandler<T> resultHandler)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||||
fillStatement(stmt, params);
|
fillStatement(stmt, params);
|
||||||
@ -378,9 +382,9 @@ class JdbcOperationSupport {
|
|||||||
* @param rowMapper {@link ResultSet} 中每一行的数据的处理逻辑
|
* @param rowMapper {@link ResultSet} 中每一行的数据的处理逻辑
|
||||||
*/
|
*/
|
||||||
private static <T> List<T> queryListInternal(@Nonnull Connection conn,
|
private static <T> List<T> queryListInternal(@Nonnull Connection conn,
|
||||||
@Nonnull String sql,
|
@Nonnull String sql,
|
||||||
@Nullable Object[] params,
|
@Nullable Object[] params,
|
||||||
@Nonnull RowMapper<T> rowMapper)
|
@Nonnull RowMapper<T> rowMapper)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
return queryInternal(conn, sql, params, rs -> {
|
return queryInternal(conn, sql, params, rs -> {
|
||||||
List<T> result = new ArrayList<>();
|
List<T> result = new ArrayList<>();
|
||||||
@ -401,17 +405,13 @@ class JdbcOperationSupport {
|
|||||||
* @param params 参数
|
* @param params 参数
|
||||||
* @param rowMapper 行数据映射逻辑
|
* @param rowMapper 行数据映射逻辑
|
||||||
*/
|
*/
|
||||||
private static <T> Optional<T> queryFirstInternal(@Nonnull Connection conn,
|
private static <T> T queryFirstInternal(@Nonnull Connection conn,
|
||||||
@Nonnull String sql,
|
@Nonnull String sql,
|
||||||
@Nullable Object[] params,
|
@Nullable Object[] params,
|
||||||
@Nonnull RowMapper<T> rowMapper)
|
@Nonnull RowMapper<T> rowMapper)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
return queryInternal(conn, sql, params, rs -> {
|
return queryInternal(conn, sql, params, rs ->
|
||||||
if (rs.next()) {
|
rs.next() ? rowMapper.mapRow(rs, 0) : null);
|
||||||
return Optional.ofNullable(rowMapper.mapRow(rs, 0));
|
|
||||||
}
|
|
||||||
return Optional.empty();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// #endregion
|
// #endregion
|
||||||
|
@ -285,6 +285,22 @@ interface JdbcOperations {
|
|||||||
Optional<BigDecimal> queryFirstBigDecimal(String sql)
|
Optional<BigDecimal> queryFirstBigDecimal(String sql)
|
||||||
throws SQLException;
|
throws SQLException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询结果,并转换为 boolean
|
||||||
|
*
|
||||||
|
* @param sql SQL
|
||||||
|
*/
|
||||||
|
boolean queryAsBoolean(String sql)
|
||||||
|
throws SQLException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询结果,并转换为 boolean
|
||||||
|
*
|
||||||
|
* @param sql SQL
|
||||||
|
*/
|
||||||
|
boolean queryAsBoolean(String sql, Object[] params)
|
||||||
|
throws SQLException;
|
||||||
|
|
||||||
// #endregion
|
// #endregion
|
||||||
|
|
||||||
// #region - update & batchUpdate
|
// #region - update & batchUpdate
|
||||||
|
@ -34,6 +34,7 @@ import javax.sql.DataSource;
|
|||||||
import xyz.zhouxy.plusone.commons.function.ThrowingConsumer;
|
import xyz.zhouxy.plusone.commons.function.ThrowingConsumer;
|
||||||
import xyz.zhouxy.plusone.commons.function.ThrowingPredicate;
|
import xyz.zhouxy.plusone.commons.function.ThrowingPredicate;
|
||||||
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
||||||
|
import xyz.zhouxy.plusone.commons.util.OptionalTools;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SimpleJdbcTemplate
|
* SimpleJdbcTemplate
|
||||||
@ -138,7 +139,8 @@ public class SimpleJdbcTemplate implements JdbcOperations {
|
|||||||
public List<Map<String, Object>> queryList(String sql)
|
public List<Map<String, Object>> queryList(String sql)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
try (Connection conn = this.dataSource.getConnection()) {
|
try (Connection conn = this.dataSource.getConnection()) {
|
||||||
return JdbcOperationSupport.queryList(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, RowMapper.HASH_MAP_MAPPER);
|
return JdbcOperationSupport
|
||||||
|
.queryList(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, RowMapper.HASH_MAP_MAPPER);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -147,7 +149,8 @@ public class SimpleJdbcTemplate implements JdbcOperations {
|
|||||||
public List<DbRecord> queryRecordList(String sql)
|
public List<DbRecord> queryRecordList(String sql)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
try (Connection conn = this.dataSource.getConnection()) {
|
try (Connection conn = this.dataSource.getConnection()) {
|
||||||
return JdbcOperationSupport.queryList(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, RowMapper.RECORD_MAPPER);
|
return JdbcOperationSupport
|
||||||
|
.queryList(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, RowMapper.RECORD_MAPPER);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -160,7 +163,8 @@ public class SimpleJdbcTemplate implements JdbcOperations {
|
|||||||
public <T> Optional<T> queryFirst(String sql, Object[] params, RowMapper<T> rowMapper)
|
public <T> Optional<T> queryFirst(String sql, Object[] params, RowMapper<T> rowMapper)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
try (Connection conn = this.dataSource.getConnection()) {
|
try (Connection conn = this.dataSource.getConnection()) {
|
||||||
return JdbcOperationSupport.queryFirst(conn, sql, params, rowMapper);
|
final T result = JdbcOperationSupport.queryFirst(conn, sql, params, rowMapper);
|
||||||
|
return Optional.ofNullable(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -169,7 +173,8 @@ public class SimpleJdbcTemplate implements JdbcOperations {
|
|||||||
public <T> Optional<T> queryFirst(String sql, Object[] params, Class<T> clazz)
|
public <T> Optional<T> queryFirst(String sql, Object[] params, Class<T> clazz)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
try (Connection conn = this.dataSource.getConnection()) {
|
try (Connection conn = this.dataSource.getConnection()) {
|
||||||
return JdbcOperationSupport.queryFirst(conn, sql, params, clazz);
|
final T result = JdbcOperationSupport.queryFirst(conn, sql, params, clazz);
|
||||||
|
return Optional.ofNullable(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -178,7 +183,9 @@ public class SimpleJdbcTemplate implements JdbcOperations {
|
|||||||
public Optional<Map<String, Object>> queryFirst(String sql, Object[] params)
|
public Optional<Map<String, Object>> queryFirst(String sql, Object[] params)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
try (Connection conn = this.dataSource.getConnection()) {
|
try (Connection conn = this.dataSource.getConnection()) {
|
||||||
return JdbcOperationSupport.queryFirst(conn, sql, params, RowMapper.HASH_MAP_MAPPER);
|
final Map<String, Object> result = JdbcOperationSupport
|
||||||
|
.queryFirst(conn, sql, params, RowMapper.HASH_MAP_MAPPER);
|
||||||
|
return Optional.ofNullable(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -187,7 +194,9 @@ public class SimpleJdbcTemplate implements JdbcOperations {
|
|||||||
public Optional<DbRecord> queryFirstRecord(String sql, Object[] params)
|
public Optional<DbRecord> queryFirstRecord(String sql, Object[] params)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
try (Connection conn = this.dataSource.getConnection()) {
|
try (Connection conn = this.dataSource.getConnection()) {
|
||||||
return JdbcOperationSupport.queryFirst(conn, sql, params, RowMapper.RECORD_MAPPER);
|
final DbRecord result = JdbcOperationSupport
|
||||||
|
.queryFirst(conn, sql, params, RowMapper.RECORD_MAPPER);
|
||||||
|
return Optional.ofNullable(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -196,7 +205,8 @@ public class SimpleJdbcTemplate implements JdbcOperations {
|
|||||||
public Optional<String> queryFirstString(String sql, Object[] params)
|
public Optional<String> queryFirstString(String sql, Object[] params)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
try (Connection conn = this.dataSource.getConnection()) {
|
try (Connection conn = this.dataSource.getConnection()) {
|
||||||
return JdbcOperationSupport.queryFirstString(conn, sql, params);
|
final String result = JdbcOperationSupport.queryFirstString(conn, sql, params);
|
||||||
|
return Optional.ofNullable(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -205,7 +215,8 @@ public class SimpleJdbcTemplate implements JdbcOperations {
|
|||||||
public OptionalInt queryFirstInt(String sql, Object[] params)
|
public OptionalInt queryFirstInt(String sql, Object[] params)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
try (Connection conn = this.dataSource.getConnection()) {
|
try (Connection conn = this.dataSource.getConnection()) {
|
||||||
return JdbcOperationSupport.queryFirstInt(conn, sql, params);
|
final Integer result = JdbcOperationSupport.queryFirstInt(conn, sql, params);
|
||||||
|
return OptionalTools.optionalOf(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -214,7 +225,8 @@ public class SimpleJdbcTemplate implements JdbcOperations {
|
|||||||
public OptionalLong queryFirstLong(String sql, Object[] params)
|
public OptionalLong queryFirstLong(String sql, Object[] params)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
try (Connection conn = this.dataSource.getConnection()) {
|
try (Connection conn = this.dataSource.getConnection()) {
|
||||||
return JdbcOperationSupport.queryFirstLong(conn, sql, params);
|
final Long result = JdbcOperationSupport.queryFirstLong(conn, sql, params);
|
||||||
|
return OptionalTools.optionalOf(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -223,7 +235,8 @@ public class SimpleJdbcTemplate implements JdbcOperations {
|
|||||||
public OptionalDouble queryFirstDouble(String sql, Object[] params)
|
public OptionalDouble queryFirstDouble(String sql, Object[] params)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
try (Connection conn = this.dataSource.getConnection()) {
|
try (Connection conn = this.dataSource.getConnection()) {
|
||||||
return JdbcOperationSupport.queryFirstDouble(conn, sql, params);
|
final Double result = JdbcOperationSupport.queryFirstDouble(conn, sql, params);
|
||||||
|
return OptionalTools.optionalOf(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -232,7 +245,8 @@ public class SimpleJdbcTemplate implements JdbcOperations {
|
|||||||
public Optional<BigDecimal> queryFirstBigDecimal(String sql, Object[] params)
|
public Optional<BigDecimal> queryFirstBigDecimal(String sql, Object[] params)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
try (Connection conn = this.dataSource.getConnection()) {
|
try (Connection conn = this.dataSource.getConnection()) {
|
||||||
return JdbcOperationSupport.queryFirstBigDecimal(conn, sql, params);
|
final BigDecimal result = JdbcOperationSupport.queryFirstBigDecimal(conn, sql, params);
|
||||||
|
return Optional.ofNullable(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -241,7 +255,9 @@ public class SimpleJdbcTemplate implements JdbcOperations {
|
|||||||
public <T> Optional<T> queryFirst(String sql, RowMapper<T> rowMapper)
|
public <T> Optional<T> queryFirst(String sql, RowMapper<T> rowMapper)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
try (Connection conn = this.dataSource.getConnection()) {
|
try (Connection conn = this.dataSource.getConnection()) {
|
||||||
return JdbcOperationSupport.queryFirst(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, rowMapper);
|
final T result = JdbcOperationSupport
|
||||||
|
.queryFirst(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, rowMapper);
|
||||||
|
return Optional.ofNullable(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -250,7 +266,9 @@ public class SimpleJdbcTemplate implements JdbcOperations {
|
|||||||
public <T> Optional<T> queryFirst(String sql, Class<T> clazz)
|
public <T> Optional<T> queryFirst(String sql, Class<T> clazz)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
try (Connection conn = this.dataSource.getConnection()) {
|
try (Connection conn = this.dataSource.getConnection()) {
|
||||||
return JdbcOperationSupport.queryFirst(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, clazz);
|
final T result = JdbcOperationSupport
|
||||||
|
.queryFirst(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, clazz);
|
||||||
|
return Optional.ofNullable(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -259,7 +277,9 @@ public class SimpleJdbcTemplate implements JdbcOperations {
|
|||||||
public Optional<Map<String, Object>> queryFirst(String sql)
|
public Optional<Map<String, Object>> queryFirst(String sql)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
try (Connection conn = this.dataSource.getConnection()) {
|
try (Connection conn = this.dataSource.getConnection()) {
|
||||||
return JdbcOperationSupport.queryFirst(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, RowMapper.HASH_MAP_MAPPER);
|
final Map<String, Object> result = JdbcOperationSupport
|
||||||
|
.queryFirst(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, RowMapper.HASH_MAP_MAPPER);
|
||||||
|
return Optional.ofNullable(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -268,7 +288,9 @@ public class SimpleJdbcTemplate implements JdbcOperations {
|
|||||||
public Optional<DbRecord> queryFirstRecord(String sql)
|
public Optional<DbRecord> queryFirstRecord(String sql)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
try (Connection conn = this.dataSource.getConnection()) {
|
try (Connection conn = this.dataSource.getConnection()) {
|
||||||
return JdbcOperationSupport.queryFirst(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, RowMapper.RECORD_MAPPER);
|
final DbRecord result = JdbcOperationSupport
|
||||||
|
.queryFirst(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, RowMapper.RECORD_MAPPER);
|
||||||
|
return Optional.ofNullable(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -277,7 +299,9 @@ public class SimpleJdbcTemplate implements JdbcOperations {
|
|||||||
public Optional<String> queryFirstString(String sql)
|
public Optional<String> queryFirstString(String sql)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
try (Connection conn = this.dataSource.getConnection()) {
|
try (Connection conn = this.dataSource.getConnection()) {
|
||||||
return JdbcOperationSupport.queryFirstString(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
final String result = JdbcOperationSupport.
|
||||||
|
queryFirstString(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
||||||
|
return Optional.ofNullable(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -286,7 +310,9 @@ public class SimpleJdbcTemplate implements JdbcOperations {
|
|||||||
public OptionalInt queryFirstInt(String sql)
|
public OptionalInt queryFirstInt(String sql)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
try (Connection conn = this.dataSource.getConnection()) {
|
try (Connection conn = this.dataSource.getConnection()) {
|
||||||
return JdbcOperationSupport.queryFirstInt(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
final Integer result = JdbcOperationSupport
|
||||||
|
.queryFirstInt(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
||||||
|
return OptionalTools.optionalOf(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -295,7 +321,9 @@ public class SimpleJdbcTemplate implements JdbcOperations {
|
|||||||
public OptionalLong queryFirstLong(String sql)
|
public OptionalLong queryFirstLong(String sql)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
try (Connection conn = this.dataSource.getConnection()) {
|
try (Connection conn = this.dataSource.getConnection()) {
|
||||||
return JdbcOperationSupport.queryFirstLong(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
final Long result = JdbcOperationSupport
|
||||||
|
.queryFirstLong(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
||||||
|
return OptionalTools.optionalOf(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -304,7 +332,9 @@ public class SimpleJdbcTemplate implements JdbcOperations {
|
|||||||
public OptionalDouble queryFirstDouble(String sql)
|
public OptionalDouble queryFirstDouble(String sql)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
try (Connection conn = this.dataSource.getConnection()) {
|
try (Connection conn = this.dataSource.getConnection()) {
|
||||||
return JdbcOperationSupport.queryFirstDouble(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
final Double result = JdbcOperationSupport
|
||||||
|
.queryFirstDouble(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
||||||
|
return OptionalTools.optionalOf(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -313,7 +343,31 @@ public class SimpleJdbcTemplate implements JdbcOperations {
|
|||||||
public Optional<BigDecimal> queryFirstBigDecimal(String sql)
|
public Optional<BigDecimal> queryFirstBigDecimal(String sql)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
try (Connection conn = this.dataSource.getConnection()) {
|
try (Connection conn = this.dataSource.getConnection()) {
|
||||||
return JdbcOperationSupport.queryFirstBigDecimal(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
final BigDecimal result = JdbcOperationSupport
|
||||||
|
.queryFirstBigDecimal(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
||||||
|
return Optional.ofNullable(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc} */
|
||||||
|
@Override
|
||||||
|
public boolean queryAsBoolean(String sql) // TODO 单元测试
|
||||||
|
throws SQLException {
|
||||||
|
try (Connection conn = this.dataSource.getConnection()) {
|
||||||
|
final Boolean result = JdbcOperationSupport
|
||||||
|
.queryFirstBoolean(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
||||||
|
return Boolean.TRUE.equals(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc} */
|
||||||
|
@Override
|
||||||
|
public boolean queryAsBoolean(String sql, Object[] params) // TODO 单元测试
|
||||||
|
throws SQLException {
|
||||||
|
try (Connection conn = this.dataSource.getConnection()) {
|
||||||
|
final Boolean result = JdbcOperationSupport
|
||||||
|
.queryFirstBoolean(conn, sql, params);
|
||||||
|
return Boolean.TRUE.equals(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -372,7 +426,8 @@ public class SimpleJdbcTemplate implements JdbcOperations {
|
|||||||
int batchSize, List<Exception> exceptions)
|
int batchSize, List<Exception> exceptions)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
try (Connection conn = this.dataSource.getConnection()) {
|
try (Connection conn = this.dataSource.getConnection()) {
|
||||||
return JdbcOperationSupport.batchUpdateAndIgnoreException(conn, sql, params, batchSize, exceptions);
|
return JdbcOperationSupport
|
||||||
|
.batchUpdateAndIgnoreException(conn, sql, params, batchSize, exceptions);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -392,7 +447,8 @@ public class SimpleJdbcTemplate implements JdbcOperations {
|
|||||||
* @throws SQLException SQL 异常
|
* @throws SQLException SQL 异常
|
||||||
* @throws E 事务中的异常
|
* @throws E 事务中的异常
|
||||||
*/
|
*/
|
||||||
public <E extends Exception> void executeTransaction(@Nonnull final ThrowingConsumer<JdbcExecutor, E> operations)
|
public <E extends Exception> void executeTransaction(
|
||||||
|
@Nonnull final ThrowingConsumer<JdbcExecutor, E> operations)
|
||||||
throws SQLException, E {
|
throws SQLException, E {
|
||||||
AssertTools.checkNotNull(operations, "Operations can not be null.");
|
AssertTools.checkNotNull(operations, "Operations can not be null.");
|
||||||
try (Connection conn = this.dataSource.getConnection()) {
|
try (Connection conn = this.dataSource.getConnection()) {
|
||||||
@ -422,7 +478,8 @@ public class SimpleJdbcTemplate implements JdbcOperations {
|
|||||||
* @throws SQLException 数据库异常
|
* @throws SQLException 数据库异常
|
||||||
* @throws E 事务中的异常类型
|
* @throws E 事务中的异常类型
|
||||||
*/
|
*/
|
||||||
public <E extends Exception> void commitIfTrue(@Nonnull final ThrowingPredicate<JdbcExecutor, E> operations)
|
public <E extends Exception> void commitIfTrue(
|
||||||
|
@Nonnull final ThrowingPredicate<JdbcExecutor, E> operations)
|
||||||
throws SQLException, E {
|
throws SQLException, E {
|
||||||
AssertTools.checkNotNull(operations, "Operations can not be null.");
|
AssertTools.checkNotNull(operations, "Operations can not be null.");
|
||||||
try (Connection conn = this.dataSource.getConnection()) {
|
try (Connection conn = this.dataSource.getConnection()) {
|
||||||
@ -469,7 +526,8 @@ public class SimpleJdbcTemplate implements JdbcOperations {
|
|||||||
@Override
|
@Override
|
||||||
public <T> T query(String sql, ResultHandler<T> resultHandler)
|
public <T> T query(String sql, ResultHandler<T> resultHandler)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
return JdbcOperationSupport.query(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, resultHandler);
|
return JdbcOperationSupport
|
||||||
|
.query(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, resultHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
// #endregion
|
// #endregion
|
||||||
@ -508,7 +566,8 @@ public class SimpleJdbcTemplate implements JdbcOperations {
|
|||||||
@Override
|
@Override
|
||||||
public <T> List<T> queryList(String sql, RowMapper<T> rowMapper)
|
public <T> List<T> queryList(String sql, RowMapper<T> rowMapper)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
return JdbcOperationSupport.queryList(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, rowMapper);
|
return JdbcOperationSupport
|
||||||
|
.queryList(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, rowMapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
@ -522,14 +581,16 @@ public class SimpleJdbcTemplate implements JdbcOperations {
|
|||||||
@Override
|
@Override
|
||||||
public List<Map<String, Object>> queryList(String sql)
|
public List<Map<String, Object>> queryList(String sql)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
return JdbcOperationSupport.queryList(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, RowMapper.HASH_MAP_MAPPER);
|
return JdbcOperationSupport
|
||||||
|
.queryList(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, RowMapper.HASH_MAP_MAPPER);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
@Override
|
@Override
|
||||||
public List<DbRecord> queryRecordList(String sql)
|
public List<DbRecord> queryRecordList(String sql)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
return JdbcOperationSupport.queryList(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, RowMapper.RECORD_MAPPER);
|
return JdbcOperationSupport
|
||||||
|
.queryList(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, RowMapper.RECORD_MAPPER);
|
||||||
}
|
}
|
||||||
|
|
||||||
// #endregion
|
// #endregion
|
||||||
@ -540,126 +601,172 @@ public class SimpleJdbcTemplate implements JdbcOperations {
|
|||||||
@Override
|
@Override
|
||||||
public <T> Optional<T> queryFirst(String sql, Object[] params, RowMapper<T> rowMapper)
|
public <T> Optional<T> queryFirst(String sql, Object[] params, RowMapper<T> rowMapper)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
return JdbcOperationSupport.queryFirst(this.conn, sql, params, rowMapper);
|
final T result = JdbcOperationSupport.queryFirst(this.conn, sql, params, rowMapper);
|
||||||
|
return Optional.ofNullable(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
@Override
|
@Override
|
||||||
public <T> Optional<T> queryFirst(String sql, Object[] params, Class<T> clazz)
|
public <T> Optional<T> queryFirst(String sql, Object[] params, Class<T> clazz)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
return JdbcOperationSupport.queryFirst(this.conn, sql, params, clazz);
|
final T result = JdbcOperationSupport.queryFirst(this.conn, sql, params, clazz);
|
||||||
|
return Optional.ofNullable(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
@Override
|
@Override
|
||||||
public Optional<Map<String, Object>> queryFirst(String sql, Object[] params)
|
public Optional<Map<String, Object>> queryFirst(String sql, Object[] params)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
return JdbcOperationSupport.queryFirst(this.conn, sql, params, RowMapper.HASH_MAP_MAPPER);
|
final Map<String, Object> result = JdbcOperationSupport
|
||||||
|
.queryFirst(this.conn, sql, params, RowMapper.HASH_MAP_MAPPER);
|
||||||
|
return Optional.ofNullable(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
@Override
|
@Override
|
||||||
public Optional<DbRecord> queryFirstRecord(String sql, Object[] params)
|
public Optional<DbRecord> queryFirstRecord(String sql, Object[] params)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
return JdbcOperationSupport.queryFirst(this.conn, sql, params, RowMapper.RECORD_MAPPER);
|
final DbRecord result = JdbcOperationSupport
|
||||||
|
.queryFirst(this.conn, sql, params, RowMapper.RECORD_MAPPER);
|
||||||
|
return Optional.ofNullable(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
@Override
|
@Override
|
||||||
public Optional<String> queryFirstString(String sql, Object[] params)
|
public Optional<String> queryFirstString(String sql, Object[] params)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
return JdbcOperationSupport.queryFirstString(this.conn, sql, params);
|
final String result = JdbcOperationSupport.queryFirstString(this.conn, sql, params);
|
||||||
|
return Optional.ofNullable(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
@Override
|
@Override
|
||||||
public OptionalInt queryFirstInt(String sql, Object[] params)
|
public OptionalInt queryFirstInt(String sql, Object[] params)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
return JdbcOperationSupport.queryFirstInt(this.conn, sql, params);
|
final Integer result = JdbcOperationSupport.queryFirstInt(this.conn, sql, params);
|
||||||
|
return OptionalTools.optionalOf(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
@Override
|
@Override
|
||||||
public OptionalLong queryFirstLong(String sql, Object[] params)
|
public OptionalLong queryFirstLong(String sql, Object[] params)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
return JdbcOperationSupport.queryFirstLong(this.conn, sql, params);
|
final Long result = JdbcOperationSupport.queryFirstLong(this.conn, sql, params);
|
||||||
|
return OptionalTools.optionalOf(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
@Override
|
@Override
|
||||||
public OptionalDouble queryFirstDouble(String sql, Object[] params)
|
public OptionalDouble queryFirstDouble(String sql, Object[] params)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
return JdbcOperationSupport.queryFirstDouble(this.conn, sql, params);
|
final Double result = JdbcOperationSupport.queryFirstDouble(this.conn, sql, params);
|
||||||
|
return OptionalTools.optionalOf(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
@Override
|
@Override
|
||||||
public Optional<BigDecimal> queryFirstBigDecimal(String sql, Object[] params)
|
public Optional<BigDecimal> queryFirstBigDecimal(String sql, Object[] params)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
return JdbcOperationSupport.queryFirstBigDecimal(this.conn, sql, params);
|
final BigDecimal result = JdbcOperationSupport.queryFirstBigDecimal(this.conn, sql, params);
|
||||||
|
return Optional.ofNullable(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
@Override
|
@Override
|
||||||
public <T> Optional<T> queryFirst(String sql, RowMapper<T> rowMapper)
|
public <T> Optional<T> queryFirst(String sql, RowMapper<T> rowMapper)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
return JdbcOperationSupport.queryFirst(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, rowMapper);
|
final T result = JdbcOperationSupport
|
||||||
|
.queryFirst(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, rowMapper);
|
||||||
|
return Optional.ofNullable(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
@Override
|
@Override
|
||||||
public <T> Optional<T> queryFirst(String sql, Class<T> clazz)
|
public <T> Optional<T> queryFirst(String sql, Class<T> clazz)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
return JdbcOperationSupport.queryFirst(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, clazz);
|
final T result = JdbcOperationSupport
|
||||||
|
.queryFirst(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, clazz);
|
||||||
|
return Optional.ofNullable(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
@Override
|
@Override
|
||||||
public Optional<Map<String, Object>> queryFirst(String sql)
|
public Optional<Map<String, Object>> queryFirst(String sql)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
return JdbcOperationSupport.queryFirst(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, RowMapper.HASH_MAP_MAPPER);
|
final Map<String, Object> result = JdbcOperationSupport
|
||||||
|
.queryFirst(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, RowMapper.HASH_MAP_MAPPER);
|
||||||
|
return Optional.ofNullable(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
@Override
|
@Override
|
||||||
public Optional<DbRecord> queryFirstRecord(String sql)
|
public Optional<DbRecord> queryFirstRecord(String sql)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
return JdbcOperationSupport.queryFirst(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, RowMapper.RECORD_MAPPER);
|
final DbRecord result = JdbcOperationSupport
|
||||||
|
.queryFirst(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, RowMapper.RECORD_MAPPER);
|
||||||
|
return Optional.ofNullable(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
@Override
|
@Override
|
||||||
public Optional<String> queryFirstString(String sql)
|
public Optional<String> queryFirstString(String sql)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
return JdbcOperationSupport.queryFirstString(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
final String result = JdbcOperationSupport
|
||||||
|
.queryFirstString(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
||||||
|
return Optional.ofNullable(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
@Override
|
@Override
|
||||||
public OptionalInt queryFirstInt(String sql)
|
public OptionalInt queryFirstInt(String sql)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
return JdbcOperationSupport.queryFirstInt(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
final Integer result = JdbcOperationSupport
|
||||||
|
.queryFirstInt(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
||||||
|
return OptionalTools.optionalOf(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
@Override
|
@Override
|
||||||
public OptionalLong queryFirstLong(String sql)
|
public OptionalLong queryFirstLong(String sql)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
return JdbcOperationSupport.queryFirstLong(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
final Long result = JdbcOperationSupport
|
||||||
|
.queryFirstLong(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
||||||
|
return OptionalTools.optionalOf(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
@Override
|
@Override
|
||||||
public OptionalDouble queryFirstDouble(String sql)
|
public OptionalDouble queryFirstDouble(String sql)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
return JdbcOperationSupport.queryFirstDouble(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
final Double result = JdbcOperationSupport
|
||||||
|
.queryFirstDouble(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
||||||
|
return OptionalTools.optionalOf(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
@Override
|
@Override
|
||||||
public Optional<BigDecimal> queryFirstBigDecimal(String sql)
|
public Optional<BigDecimal> queryFirstBigDecimal(String sql)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
return JdbcOperationSupport.queryFirstBigDecimal(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
final BigDecimal result = JdbcOperationSupport
|
||||||
|
.queryFirstBigDecimal(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
||||||
|
return Optional.ofNullable(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc} */
|
||||||
|
@Override
|
||||||
|
public boolean queryAsBoolean(String sql)
|
||||||
|
throws SQLException {
|
||||||
|
final Boolean result = JdbcOperationSupport
|
||||||
|
.queryFirstBoolean(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
||||||
|
return Boolean.TRUE.equals(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc} */
|
||||||
|
@Override
|
||||||
|
public boolean queryAsBoolean(String sql, Object[] params)
|
||||||
|
throws SQLException {
|
||||||
|
final Boolean result = JdbcOperationSupport.queryFirstBoolean(this.conn, sql, params);
|
||||||
|
return Boolean.TRUE.equals(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
// #endregion
|
// #endregion
|
||||||
@ -703,10 +810,12 @@ public class SimpleJdbcTemplate implements JdbcOperations {
|
|||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
@Override
|
@Override
|
||||||
public List<int[]> batchUpdateAndIgnoreException(String sql, @Nullable Collection<Object[]> params,
|
public List<int[]> batchUpdateAndIgnoreException(String sql,
|
||||||
int batchSize, List<Exception> exceptions)
|
@Nullable Collection<Object[]> params,
|
||||||
throws SQLException {
|
int batchSize,
|
||||||
return JdbcOperationSupport.batchUpdateAndIgnoreException(this.conn, sql, params, batchSize, exceptions);
|
List<Exception> exceptions) throws SQLException {
|
||||||
|
return JdbcOperationSupport
|
||||||
|
.batchUpdateAndIgnoreException(this.conn, sql, params, batchSize, exceptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
// #endregion
|
// #endregion
|
||||||
|
@ -108,6 +108,14 @@ class SimpleJdbcTemplateTests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testQueryExists() throws SQLException {
|
||||||
|
boolean isExists = jdbcTemplate.queryAsBoolean(
|
||||||
|
"SELECT EXISTS(SELECT 1 FROM sys_account WHERE id = ? LIMIT 1)",
|
||||||
|
buildParams(998));
|
||||||
|
assertFalse(isExists);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testInsert() throws SQLException {
|
void testInsert() throws SQLException {
|
||||||
List<Map<String, Object>> keys = jdbcTemplate.update(
|
List<Map<String, Object>> keys = jdbcTemplate.update(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user