重构部分代码;修改方法名。
This commit is contained in:
parent
2ab0492e8c
commit
9e035d1b53
2
pom.xml
2
pom.xml
@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>xyz.zhouxy.jdbc</groupId>
|
||||
<artifactId>simple-jdbc</artifactId>
|
||||
<version>0.1.1-SNAPSHOT</version>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
|
@ -35,21 +35,21 @@ import javax.annotation.Nullable;
|
||||
|
||||
import com.google.common.base.CaseFormat;
|
||||
|
||||
public class DefaultBeanResultMap<T> implements ResultMap<T> {
|
||||
public class DefaultBeanRowMapper<T> implements RowMapper<T> {
|
||||
|
||||
private final Constructor<T> constructor;
|
||||
private final Map<String, PropertyDescriptor> colPropertyMap;
|
||||
|
||||
private DefaultBeanResultMap(Constructor<T> constructor, Map<String, PropertyDescriptor> colPropertyMap) {
|
||||
private DefaultBeanRowMapper(Constructor<T> constructor, Map<String, PropertyDescriptor> colPropertyMap) {
|
||||
this.constructor = constructor;
|
||||
this.colPropertyMap = colPropertyMap;
|
||||
}
|
||||
|
||||
public static <T> DefaultBeanResultMap<T> of(Class<T> beanType) throws SQLException {
|
||||
public static <T> DefaultBeanRowMapper<T> of(Class<T> beanType) throws SQLException {
|
||||
return of(beanType, null);
|
||||
}
|
||||
|
||||
public static <T> DefaultBeanResultMap<T> of(Class<T> beanType, @Nullable Map<String, String> propertyColMap)
|
||||
public static <T> DefaultBeanRowMapper<T> of(Class<T> beanType, @Nullable Map<String, String> propertyColMap)
|
||||
throws SQLException {
|
||||
try {
|
||||
// 获取无参构造器
|
||||
@ -74,7 +74,7 @@ public class DefaultBeanResultMap<T> implements ResultMap<T> {
|
||||
}
|
||||
Map<String, PropertyDescriptor> colPropertyMap = Arrays.stream(propertyDescriptors).collect(
|
||||
Collectors.toMap(keyMapper, Function.identity(), (a, b) -> b));
|
||||
return new DefaultBeanResultMap<>(constructor, colPropertyMap);
|
||||
return new DefaultBeanRowMapper<>(constructor, colPropertyMap);
|
||||
}
|
||||
catch (IntrospectionException e) {
|
||||
throw new SQLException("There is an exception occurs during introspection.", e);
|
||||
@ -85,7 +85,7 @@ public class DefaultBeanResultMap<T> implements ResultMap<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public T map(ResultSet rs, int rowNumber) throws SQLException {
|
||||
public T mapRow(ResultSet rs, int rowNumber) throws SQLException {
|
||||
try {
|
||||
T newInstance = this.constructor.newInstance();
|
||||
ResultSetMetaData metaData = rs.getMetaData();
|
25
src/main/java/xyz/zhouxy/jdbc/ResultHandler.java
Normal file
25
src/main/java/xyz/zhouxy/jdbc/ResultHandler.java
Normal file
@ -0,0 +1,25 @@
|
||||
/*
|
||||
* Copyright 2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package xyz.zhouxy.jdbc;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface ResultHandler<T> {
|
||||
T handle(ResultSet resultSet) throws SQLException;
|
||||
}
|
@ -23,10 +23,10 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface ResultMap<T> {
|
||||
T map(ResultSet rs, int rowNumber) throws SQLException;
|
||||
public interface RowMapper<T> {
|
||||
T mapRow(ResultSet rs, int rowNumber) throws SQLException;
|
||||
|
||||
public static final ResultMap<Map<String, Object>> mapResultMap = (rs, rowNumber) -> {
|
||||
public static final RowMapper<Map<String, Object>> HASH_MAP_MAPPER = (rs, rowNumber) -> {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
ResultSetMetaData metaData = rs.getMetaData();
|
||||
int columnCount = metaData.getColumnCount();
|
||||
@ -37,23 +37,15 @@ public interface ResultMap<T> {
|
||||
return result;
|
||||
};
|
||||
|
||||
public static final ResultMap<DbRecord> recordResultMap = (rs, rowNumber) -> {
|
||||
DbRecord result = new DbRecord();
|
||||
ResultSetMetaData metaData = rs.getMetaData();
|
||||
int columnCount = metaData.getColumnCount();
|
||||
for (int i = 1; i <= columnCount; i++) {
|
||||
String colName = metaData.getColumnName(i);
|
||||
result.put(colName, rs.getObject(colName));
|
||||
}
|
||||
return result;
|
||||
};
|
||||
public static final RowMapper<DbRecord> RECORD_MAPPER =
|
||||
(rs, rowNumber) -> new DbRecord(HASH_MAP_MAPPER.mapRow(rs, rowNumber));
|
||||
|
||||
public static <T> ResultMap<T> beanResultMap(Class<T> beanType) throws SQLException {
|
||||
return DefaultBeanResultMap.of(beanType);
|
||||
public static <T> RowMapper<T> beanRowMapper(Class<T> beanType) throws SQLException {
|
||||
return DefaultBeanRowMapper.of(beanType);
|
||||
}
|
||||
|
||||
public static <T> ResultMap<T> beanResultMap(Class<T> beanType, Map<String, String> propertyColMap)
|
||||
public static <T> RowMapper<T> beanRowMapper(Class<T> beanType, Map<String, String> propertyColMap)
|
||||
throws SQLException {
|
||||
return DefaultBeanResultMap.of(beanType, propertyColMap);
|
||||
return DefaultBeanRowMapper.of(beanType, propertyColMap);
|
||||
}
|
||||
}
|
@ -36,11 +36,10 @@ import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.function.ThrowingConsumer;
|
||||
import xyz.zhouxy.plusone.commons.function.ThrowingPredicate;
|
||||
import xyz.zhouxy.plusone.commons.collection.CollectionTools;
|
||||
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
||||
import xyz.zhouxy.plusone.commons.util.OptionalTools;
|
||||
|
||||
public class SimpleJdbcTemplate {
|
||||
@ -49,185 +48,224 @@ public class SimpleJdbcTemplate {
|
||||
private final DataSource dataSource;
|
||||
|
||||
public SimpleJdbcTemplate(@Nonnull DataSource dataSource) {
|
||||
Preconditions.checkNotNull(dataSource);
|
||||
AssertTools.checkNotNull(dataSource);
|
||||
this.dataSource = dataSource;
|
||||
}
|
||||
|
||||
public <T> List<T> query(String sql, Object[] params, ResultMap<T> resultMap)
|
||||
// #region - query
|
||||
|
||||
public <T> T query(String sql, Object[] params, ResultHandler<T> resultHandler)
|
||||
throws SQLException {
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
return JdbcExecutor.query(conn, sql, params, resultMap);
|
||||
return JdbcExecutor.query(conn, sql, params, resultHandler);
|
||||
}
|
||||
}
|
||||
|
||||
public <T> Optional<T> queryFirst(String sql, Object[] params, ResultMap<T> resultMap)
|
||||
public <T> T query(String sql, ResultHandler<T> resultHandler)
|
||||
throws SQLException {
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
return JdbcExecutor.queryFirst(conn, sql, params, resultMap);
|
||||
return JdbcExecutor.query(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, resultHandler);
|
||||
}
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> query(String sql, Object[] params)
|
||||
// #endregion
|
||||
|
||||
// #region - queryList
|
||||
|
||||
public <T> List<T> queryList(String sql, Object[] params, RowMapper<T> rowMapper)
|
||||
throws SQLException {
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
return JdbcExecutor.query(conn, sql, params);
|
||||
return JdbcExecutor.queryList(conn, sql, params, rowMapper);
|
||||
}
|
||||
}
|
||||
|
||||
public <T> List<T> queryList(String sql, Object[] params, Class<T> clazz)
|
||||
throws SQLException {
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
return JdbcExecutor.queryList(conn, sql, params, clazz);
|
||||
}
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> queryList(String sql, Object[] params)
|
||||
throws SQLException {
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
return JdbcExecutor.queryList(conn, sql, params, RowMapper.HASH_MAP_MAPPER);
|
||||
}
|
||||
}
|
||||
|
||||
public List<DbRecord> queryRecordList(String sql, Object[] params)
|
||||
throws SQLException {
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
return JdbcExecutor.queryList(conn, sql, params, RowMapper.RECORD_MAPPER);
|
||||
}
|
||||
}
|
||||
|
||||
public <T> List<T> queryList(String sql, RowMapper<T> rowMapper)
|
||||
throws SQLException {
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
return JdbcExecutor.queryList(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, rowMapper);
|
||||
}
|
||||
}
|
||||
|
||||
public <T> List<T> queryList(String sql, Class<T> clazz)
|
||||
throws SQLException {
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
return JdbcExecutor.queryList(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, clazz);
|
||||
}
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> queryList(String sql)
|
||||
throws SQLException {
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
return JdbcExecutor.queryList(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, RowMapper.HASH_MAP_MAPPER);
|
||||
}
|
||||
}
|
||||
|
||||
public List<DbRecord> queryRecordList(String sql)
|
||||
throws SQLException {
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
return JdbcExecutor.queryList(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, RowMapper.RECORD_MAPPER);
|
||||
}
|
||||
}
|
||||
|
||||
// #endregion
|
||||
|
||||
// #region - queryFirst
|
||||
|
||||
public <T> Optional<T> queryFirst(String sql, Object[] params, RowMapper<T> rowMapper)
|
||||
throws SQLException {
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
return JdbcExecutor.queryFirst(conn, sql, params, rowMapper);
|
||||
}
|
||||
}
|
||||
|
||||
public <T> Optional<T> queryFirst(String sql, Object[] params, Class<T> clazz)
|
||||
throws SQLException {
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
return JdbcExecutor.queryFirst(conn, sql, params, clazz);
|
||||
}
|
||||
}
|
||||
|
||||
public Optional<Map<String, Object>> queryFirst(String sql, Object[] params)
|
||||
throws SQLException {
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
return JdbcExecutor.queryFirst(conn, sql, params);
|
||||
}
|
||||
}
|
||||
|
||||
public List<DbRecord> queryToRecordList(String sql, Object[] params)
|
||||
throws SQLException {
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
return JdbcExecutor.queryToRecordList(conn, sql, params);
|
||||
return JdbcExecutor.queryFirst(conn, sql, params, RowMapper.HASH_MAP_MAPPER);
|
||||
}
|
||||
}
|
||||
|
||||
public Optional<DbRecord> queryFirstRecord(String sql, Object[] params)
|
||||
throws SQLException {
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
return JdbcExecutor.queryFirstRecord(conn, sql, params);
|
||||
return JdbcExecutor.queryFirst(conn, sql, params, RowMapper.RECORD_MAPPER);
|
||||
}
|
||||
}
|
||||
|
||||
public Optional<String> queryToString(String sql, Object[] params)
|
||||
public Optional<String> queryFirstString(String sql, Object[] params)
|
||||
throws SQLException {
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
return JdbcExecutor.queryToString(conn, sql, params);
|
||||
return JdbcExecutor.queryFirstString(conn, sql, params);
|
||||
}
|
||||
}
|
||||
|
||||
public OptionalInt queryToInt(String sql, Object[] params)
|
||||
public OptionalInt queryFirstInt(String sql, Object[] params)
|
||||
throws SQLException {
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
return JdbcExecutor.queryToInt(conn, sql, params);
|
||||
return JdbcExecutor.queryFirstInt(conn, sql, params);
|
||||
}
|
||||
}
|
||||
|
||||
public OptionalLong queryToLong(String sql, Object[] params)
|
||||
public OptionalLong queryFirstLong(String sql, Object[] params)
|
||||
throws SQLException {
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
return JdbcExecutor.queryToLong(conn, sql, params);
|
||||
return JdbcExecutor.queryFirstLong(conn, sql, params);
|
||||
}
|
||||
}
|
||||
|
||||
public OptionalDouble queryToDouble(String sql, Object[] params)
|
||||
public OptionalDouble queryFirstDouble(String sql, Object[] params)
|
||||
throws SQLException {
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
return JdbcExecutor.queryToDouble(conn, sql, params);
|
||||
return JdbcExecutor.queryFirstDouble(conn, sql, params);
|
||||
}
|
||||
}
|
||||
|
||||
public Optional<BigDecimal> queryToBigDecimal(String sql, Object[] params)
|
||||
public Optional<BigDecimal> queryFirstBigDecimal(String sql, Object[] params)
|
||||
throws SQLException {
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
return JdbcExecutor.queryToBigDecimal(conn, sql, params);
|
||||
return JdbcExecutor.queryFirstBigDecimal(conn, sql, params);
|
||||
}
|
||||
}
|
||||
|
||||
public int update(String sql, Object[] params)
|
||||
public <T> Optional<T> queryFirst(String sql, RowMapper<T> rowMapper)
|
||||
throws SQLException {
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
return JdbcExecutor.update(conn, sql, params);
|
||||
return JdbcExecutor.queryFirst(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, rowMapper);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行 SQL 并更新后的数据
|
||||
*
|
||||
* @param sql 要执行的 SQL 语句
|
||||
* @param params 参数
|
||||
* @param resultMap 结果映射规则
|
||||
*
|
||||
* @return 更新的数据
|
||||
* @throws SQLException 执行 SQL 遇到异常情况将抛出
|
||||
*/
|
||||
public <T> List<T> update(String sql, Object[] params, ResultMap<T> resultMap)
|
||||
public <T> Optional<T> queryFirst(String sql, Class<T> clazz)
|
||||
throws SQLException {
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
return JdbcExecutor.update(conn, sql, params, resultMap);
|
||||
}
|
||||
}
|
||||
|
||||
public <T> List<T> query(String sql, ResultMap<T> resultMap)
|
||||
throws SQLException {
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
return JdbcExecutor.query(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, resultMap);
|
||||
}
|
||||
}
|
||||
|
||||
public <T> Optional<T> queryFirst(String sql, ResultMap<T> resultMap)
|
||||
throws SQLException {
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
return JdbcExecutor.queryFirst(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, resultMap);
|
||||
}
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> query(String sql)
|
||||
throws SQLException {
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
return JdbcExecutor.query(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
||||
return JdbcExecutor.queryFirst(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, clazz);
|
||||
}
|
||||
}
|
||||
|
||||
public Optional<Map<String, Object>> queryFirst(String sql)
|
||||
throws SQLException {
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
return JdbcExecutor.queryFirst(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
||||
}
|
||||
}
|
||||
|
||||
public List<DbRecord> queryToRecordList(String sql)
|
||||
throws SQLException {
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
return JdbcExecutor.queryToRecordList(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
||||
return JdbcExecutor.queryFirst(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, RowMapper.HASH_MAP_MAPPER);
|
||||
}
|
||||
}
|
||||
|
||||
public Optional<DbRecord> queryFirstRecord(String sql)
|
||||
throws SQLException {
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
return JdbcExecutor.queryFirstRecord(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
||||
return JdbcExecutor.queryFirst(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, RowMapper.RECORD_MAPPER);
|
||||
}
|
||||
}
|
||||
|
||||
public Optional<String> queryToString(String sql)
|
||||
public Optional<String> queryFirstString(String sql)
|
||||
throws SQLException {
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
return JdbcExecutor.queryToString(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
||||
return JdbcExecutor.queryFirstString(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
||||
}
|
||||
}
|
||||
|
||||
public OptionalInt queryToInt(String sql)
|
||||
public OptionalInt queryFirstInt(String sql)
|
||||
throws SQLException {
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
return JdbcExecutor.queryToInt(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
||||
return JdbcExecutor.queryFirstInt(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
||||
}
|
||||
}
|
||||
|
||||
public OptionalLong queryToLong(String sql)
|
||||
public OptionalLong queryFirstLong(String sql)
|
||||
throws SQLException {
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
return JdbcExecutor.queryToLong(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
||||
return JdbcExecutor.queryFirstLong(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
||||
}
|
||||
}
|
||||
|
||||
public OptionalDouble queryToDouble(String sql)
|
||||
public OptionalDouble queryFirstDouble(String sql)
|
||||
throws SQLException {
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
return JdbcExecutor.queryToDouble(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
||||
return JdbcExecutor.queryFirstDouble(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
||||
}
|
||||
}
|
||||
|
||||
public Optional<BigDecimal> queryToBigDecimal(String sql)
|
||||
public Optional<BigDecimal> queryFirstBigDecimal(String sql)
|
||||
throws SQLException {
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
return JdbcExecutor.queryToBigDecimal(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
||||
return JdbcExecutor.queryFirstBigDecimal(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
||||
}
|
||||
}
|
||||
|
||||
// #endregion
|
||||
|
||||
// #region - update & batchUpdate
|
||||
|
||||
public int update(String sql, Object[] params)
|
||||
throws SQLException {
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
return JdbcExecutor.update(conn, sql, params);
|
||||
}
|
||||
}
|
||||
|
||||
@ -243,15 +281,32 @@ public class SimpleJdbcTemplate {
|
||||
*
|
||||
* @param sql 要执行的 SQL 语句
|
||||
* @param params 参数
|
||||
* @param resultMap 结果映射规则
|
||||
* @param rowMapper 结果映射规则
|
||||
*
|
||||
* @return 更新的数据
|
||||
* @throws SQLException 执行 SQL 遇到异常情况将抛出
|
||||
*/
|
||||
public <T> List<T> update(String sql, ResultMap<T> resultMap)
|
||||
public <T> List<T> update(String sql, Object[] params, RowMapper<T> rowMapper)
|
||||
throws SQLException {
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
return JdbcExecutor.update(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, resultMap);
|
||||
return JdbcExecutor.update(conn, sql, params, rowMapper);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行 SQL 并更新后的数据
|
||||
*
|
||||
* @param sql 要执行的 SQL 语句
|
||||
* @param params 参数
|
||||
* @param rowMapper 结果映射规则
|
||||
*
|
||||
* @return 更新的数据
|
||||
* @throws SQLException 执行 SQL 遇到异常情况将抛出
|
||||
*/
|
||||
public <T> List<T> update(String sql, RowMapper<T> rowMapper)
|
||||
throws SQLException {
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
return JdbcExecutor.update(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, rowMapper);
|
||||
}
|
||||
}
|
||||
|
||||
@ -262,14 +317,26 @@ public class SimpleJdbcTemplate {
|
||||
}
|
||||
}
|
||||
|
||||
public <E extends Exception> void executeTransaction(@Nonnull final ThrowingConsumer<JdbcExecutor, E> operations)
|
||||
public List<int[]> batchUpdateAndIgnoreException(String sql, @Nullable Collection<Object[]> params,
|
||||
int batchSize, List<Exception> exceptions)
|
||||
throws SQLException {
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
return JdbcExecutor.batchUpdateAndIgnoreException(conn, sql, params, batchSize, exceptions);
|
||||
}
|
||||
}
|
||||
|
||||
// #endregion
|
||||
|
||||
// #region - transaction
|
||||
|
||||
public <E extends Exception> void executeTransaction(@Nonnull final DbOperations<E> operations)
|
||||
throws SQLException, E {
|
||||
Preconditions.checkNotNull(operations, "Operations can not be null.");
|
||||
AssertTools.checkNotNull(operations, "Operations can not be null.");
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
final boolean autoCommit = conn.getAutoCommit();
|
||||
try {
|
||||
conn.setAutoCommit(false);
|
||||
operations.accept(new JdbcExecutor(conn));
|
||||
operations.execute(new JdbcExecutor(conn));
|
||||
conn.commit();
|
||||
}
|
||||
catch (Exception e) {
|
||||
@ -282,9 +349,9 @@ public class SimpleJdbcTemplate {
|
||||
}
|
||||
}
|
||||
|
||||
public <E extends Exception> void commitIfTrue(@Nonnull final ThrowingPredicate<JdbcExecutor, E> operations)
|
||||
public <E extends Exception> void commitIfTrue(@Nonnull final PredicateWithThrowable<E> operations)
|
||||
throws SQLException, E {
|
||||
Preconditions.checkNotNull(operations, "Operations can not be null.");
|
||||
AssertTools.checkNotNull(operations, "Operations can not be null.");
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
final boolean autoCommit = conn.getAutoCommit();
|
||||
try {
|
||||
@ -306,6 +373,18 @@ public class SimpleJdbcTemplate {
|
||||
}
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
public interface DbOperations<E extends Exception> {
|
||||
void execute(JdbcExecutor jdbcExecutor) throws E;
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
public interface PredicateWithThrowable<E extends Throwable> {
|
||||
boolean test(JdbcExecutor jdbcExecutor) throws E;
|
||||
}
|
||||
|
||||
// #endregion
|
||||
|
||||
public static final class JdbcExecutor {
|
||||
|
||||
private final Connection conn;
|
||||
@ -314,139 +393,163 @@ public class SimpleJdbcTemplate {
|
||||
this.conn = conn;
|
||||
}
|
||||
|
||||
public <T> List<T> query(String sql, Object[] params, ResultMap<T> resultMap)
|
||||
// #region - query
|
||||
|
||||
public <T> T query(String sql, Object[] params, ResultHandler<T> resulthHandler)
|
||||
throws SQLException {
|
||||
return JdbcExecutor.query(this.conn, sql, params, resultMap);
|
||||
return JdbcExecutor.query(this.conn, sql, params, resulthHandler);
|
||||
}
|
||||
|
||||
public <T> Optional<T> queryFirst(String sql, Object[] params, ResultMap<T> resultMap)
|
||||
public <T> T query(String sql, ResultHandler<T> resulthHandler)
|
||||
throws SQLException {
|
||||
return JdbcExecutor.queryFirst(this.conn, sql, params, resultMap);
|
||||
return JdbcExecutor.query(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, resulthHandler);
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> query(String sql, Object[] params)
|
||||
// #endregion
|
||||
|
||||
// #region - queryList
|
||||
|
||||
public <T> List<T> queryList(String sql, Object[] params, RowMapper<T> rowMapper)
|
||||
throws SQLException {
|
||||
return JdbcExecutor.query(this.conn, sql, params);
|
||||
return JdbcExecutor.queryList(this.conn, sql, params, rowMapper);
|
||||
}
|
||||
|
||||
public <T> List<T> queryList(String sql, Object[] params, Class<T> clazz)
|
||||
throws SQLException {
|
||||
return JdbcExecutor.queryList(this.conn, sql, params, clazz);
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> queryList(String sql, Object[] params)
|
||||
throws SQLException {
|
||||
return JdbcExecutor.queryList(this.conn, sql, params, RowMapper.HASH_MAP_MAPPER);
|
||||
}
|
||||
|
||||
public List<DbRecord> queryRecordList(String sql, Object[] params)
|
||||
throws SQLException {
|
||||
return JdbcExecutor.queryList(this.conn, sql, params, RowMapper.RECORD_MAPPER);
|
||||
}
|
||||
|
||||
public <T> List<T> queryList(String sql, RowMapper<T> rowMapper)
|
||||
throws SQLException {
|
||||
return JdbcExecutor.queryList(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, rowMapper);
|
||||
}
|
||||
|
||||
public <T> List<T> queryList(String sql, Class<T> clazz)
|
||||
throws SQLException {
|
||||
return JdbcExecutor.queryList(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, clazz);
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> queryList(String sql)
|
||||
throws SQLException {
|
||||
return JdbcExecutor.queryList(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, RowMapper.HASH_MAP_MAPPER);
|
||||
}
|
||||
|
||||
public List<DbRecord> queryRecordList(String sql)
|
||||
throws SQLException {
|
||||
return JdbcExecutor.queryList(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, RowMapper.RECORD_MAPPER);
|
||||
}
|
||||
|
||||
// #endregion
|
||||
|
||||
// #region - queryFirst
|
||||
|
||||
public <T> Optional<T> queryFirst(String sql, Object[] params, RowMapper<T> rowMapper)
|
||||
throws SQLException {
|
||||
return JdbcExecutor.queryFirst(this.conn, sql, params, rowMapper);
|
||||
}
|
||||
|
||||
public <T> Optional<T> queryFirst(String sql, Object[] params, Class<T> clazz)
|
||||
throws SQLException {
|
||||
return JdbcExecutor.queryFirst(this.conn, sql, params, clazz);
|
||||
}
|
||||
|
||||
public Optional<Map<String, Object>> queryFirst(String sql, Object[] params)
|
||||
throws SQLException {
|
||||
return JdbcExecutor.queryFirst(this.conn, sql, params);
|
||||
}
|
||||
|
||||
public List<DbRecord> queryToRecordList(String sql, Object[] params)
|
||||
throws SQLException {
|
||||
return JdbcExecutor.queryToRecordList(this.conn, sql, params);
|
||||
return JdbcExecutor.queryFirst(this.conn, sql, params, RowMapper.HASH_MAP_MAPPER);
|
||||
}
|
||||
|
||||
public Optional<DbRecord> queryFirstRecord(String sql, Object[] params)
|
||||
throws SQLException {
|
||||
return JdbcExecutor.queryFirstRecord(this.conn, sql, params);
|
||||
return JdbcExecutor.queryFirst(this.conn, sql, params, RowMapper.RECORD_MAPPER);
|
||||
}
|
||||
|
||||
public Optional<String> queryToString(String sql, Object[] params)
|
||||
public Optional<String> queryFirstString(String sql, Object[] params)
|
||||
throws SQLException {
|
||||
return JdbcExecutor.queryToString(this.conn, sql, params);
|
||||
return JdbcExecutor.queryFirstString(this.conn, sql, params);
|
||||
}
|
||||
|
||||
public OptionalInt queryToInt(String sql, Object[] params)
|
||||
public OptionalInt queryFirstInt(String sql, Object[] params)
|
||||
throws SQLException {
|
||||
return JdbcExecutor.queryToInt(this.conn, sql, params);
|
||||
return JdbcExecutor.queryFirstInt(this.conn, sql, params);
|
||||
}
|
||||
|
||||
public OptionalLong queryToLong(String sql, Object[] params)
|
||||
public OptionalLong queryFirstLong(String sql, Object[] params)
|
||||
throws SQLException {
|
||||
return JdbcExecutor.queryToLong(this.conn, sql, params);
|
||||
return JdbcExecutor.queryFirstLong(this.conn, sql, params);
|
||||
}
|
||||
|
||||
public OptionalDouble queryToDouble(String sql, Object[] params)
|
||||
public OptionalDouble queryFirstDouble(String sql, Object[] params)
|
||||
throws SQLException {
|
||||
return JdbcExecutor.queryToDouble(this.conn, sql, params);
|
||||
return JdbcExecutor.queryFirstDouble(this.conn, sql, params);
|
||||
}
|
||||
|
||||
public Optional<BigDecimal> queryToBigDecimal(String sql, Object[] params)
|
||||
public Optional<BigDecimal> queryFirstBigDecimal(String sql, Object[] params)
|
||||
throws SQLException {
|
||||
return JdbcExecutor.queryToBigDecimal(this.conn, sql, params);
|
||||
return JdbcExecutor.queryFirstBigDecimal(this.conn, sql, params);
|
||||
}
|
||||
|
||||
public int update(String sql, Object[] params)
|
||||
public <T> Optional<T> queryFirst(String sql, RowMapper<T> rowMapper)
|
||||
throws SQLException {
|
||||
return JdbcExecutor.update(this.conn, sql, params);
|
||||
return JdbcExecutor.queryFirst(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, rowMapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行 SQL 并更新后的数据
|
||||
*
|
||||
* @param sql 要执行的 SQL 语句
|
||||
* @param params 参数
|
||||
* @param resultMap 结果映射规则
|
||||
*
|
||||
* @return 更新的数据
|
||||
* @throws SQLException 执行 SQL 遇到异常情况将抛出
|
||||
*/
|
||||
public <T> List<T> update(String sql, Object[] params, ResultMap<T> resultMap)
|
||||
public <T> Optional<T> queryFirst(String sql, Class<T> clazz)
|
||||
throws SQLException {
|
||||
return JdbcExecutor.update(this.conn, sql, params, resultMap);
|
||||
}
|
||||
|
||||
public List<int[]> batchUpdate(String sql, @Nullable Collection<Object[]> params, int batchSize)
|
||||
throws SQLException {
|
||||
return JdbcExecutor.batchUpdate(this.conn, sql, params, batchSize);
|
||||
}
|
||||
|
||||
public <T> List<T> query(String sql, ResultMap<T> resultMap)
|
||||
throws SQLException {
|
||||
return JdbcExecutor.query(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, resultMap);
|
||||
}
|
||||
|
||||
public <T> Optional<T> queryFirst(String sql, ResultMap<T> resultMap)
|
||||
throws SQLException {
|
||||
return JdbcExecutor.queryFirst(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, resultMap);
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> query(String sql)
|
||||
throws SQLException {
|
||||
return JdbcExecutor.query(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
||||
return JdbcExecutor.queryFirst(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, clazz);
|
||||
}
|
||||
|
||||
public Optional<Map<String, Object>> queryFirst(String sql)
|
||||
throws SQLException {
|
||||
return JdbcExecutor.queryFirst(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
||||
}
|
||||
|
||||
public List<DbRecord> queryToRecordList(String sql)
|
||||
throws SQLException {
|
||||
return JdbcExecutor.queryToRecordList(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
||||
return JdbcExecutor.queryFirst(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, RowMapper.HASH_MAP_MAPPER);
|
||||
}
|
||||
|
||||
public Optional<DbRecord> queryFirstRecord(String sql)
|
||||
throws SQLException {
|
||||
return JdbcExecutor.queryFirstRecord(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
||||
return JdbcExecutor.queryFirst(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, RowMapper.RECORD_MAPPER);
|
||||
}
|
||||
|
||||
public Optional<String> queryToString(String sql)
|
||||
public Optional<String> queryFirstString(String sql)
|
||||
throws SQLException {
|
||||
return JdbcExecutor.queryToString(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
||||
return JdbcExecutor.queryFirstString(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
||||
}
|
||||
|
||||
public OptionalInt queryToInt(String sql)
|
||||
public OptionalInt queryFirstInt(String sql)
|
||||
throws SQLException {
|
||||
return JdbcExecutor.queryToInt(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
||||
return JdbcExecutor.queryFirstInt(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
||||
}
|
||||
|
||||
public OptionalLong queryToLong(String sql)
|
||||
public OptionalLong queryFirstLong(String sql)
|
||||
throws SQLException {
|
||||
return JdbcExecutor.queryToLong(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
||||
return JdbcExecutor.queryFirstLong(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
||||
}
|
||||
|
||||
public OptionalDouble queryToDouble(String sql)
|
||||
public OptionalDouble queryFirstDouble(String sql)
|
||||
throws SQLException {
|
||||
return JdbcExecutor.queryToDouble(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
||||
return JdbcExecutor.queryFirstDouble(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
||||
}
|
||||
|
||||
public Optional<BigDecimal> queryToBigDecimal(String sql)
|
||||
public Optional<BigDecimal> queryFirstBigDecimal(String sql)
|
||||
throws SQLException {
|
||||
return JdbcExecutor.queryToBigDecimal(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
||||
return JdbcExecutor.queryFirstBigDecimal(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
||||
}
|
||||
|
||||
// #endregion
|
||||
|
||||
// #region - update & batchUpdate
|
||||
|
||||
public int update(String sql, Object[] params)
|
||||
throws SQLException {
|
||||
return JdbcExecutor.update(this.conn, sql, params);
|
||||
}
|
||||
|
||||
public int update(String sql)
|
||||
@ -459,88 +562,172 @@ public class SimpleJdbcTemplate {
|
||||
*
|
||||
* @param sql 要执行的 SQL 语句
|
||||
* @param params 参数
|
||||
* @param resultMap 结果映射规则
|
||||
* @param rowMapper 结果映射规则
|
||||
*
|
||||
* @return 更新的数据
|
||||
* @throws SQLException 执行 SQL 遇到异常情况将抛出
|
||||
*/
|
||||
public <T> List<T> update(String sql, ResultMap<T> resultMap)
|
||||
public <T> List<T> update(String sql, Object[] params, RowMapper<T> rowMapper)
|
||||
throws SQLException {
|
||||
return JdbcExecutor.update(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, resultMap);
|
||||
return JdbcExecutor.update(this.conn, sql, params, rowMapper);
|
||||
}
|
||||
|
||||
private static <T> List<T> query(Connection conn, String sql, Object[] params, ResultMap<T> resultMap)
|
||||
/**
|
||||
* 执行 SQL 并更新后的数据
|
||||
*
|
||||
* @param sql 要执行的 SQL 语句
|
||||
* @param params 参数
|
||||
* @param rowMapper 结果映射规则
|
||||
*
|
||||
* @return 更新的数据
|
||||
* @throws SQLException 执行 SQL 遇到异常情况将抛出
|
||||
*/
|
||||
public <T> List<T> update(String sql, RowMapper<T> rowMapper)
|
||||
throws SQLException {
|
||||
return JdbcExecutor.update(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, rowMapper);
|
||||
}
|
||||
|
||||
public List<int[]> batchUpdate(String sql, @Nullable Collection<Object[]> params, int batchSize)
|
||||
throws SQLException {
|
||||
return JdbcExecutor.batchUpdate(this.conn, sql, params, batchSize);
|
||||
}
|
||||
|
||||
public List<int[]> batchUpdateAndIgnoreException(String sql, @Nullable Collection<Object[]> params,
|
||||
int batchSize, List<Exception> exceptions)
|
||||
throws SQLException {
|
||||
return JdbcExecutor.batchUpdateAndIgnoreException(this.conn, sql, params, batchSize, exceptions);
|
||||
}
|
||||
|
||||
// #endregion
|
||||
|
||||
// #region - internal
|
||||
|
||||
private static <T> T queryInternal(@Nonnull Connection conn,
|
||||
@Nonnull String sql,
|
||||
@Nullable Object[] params,
|
||||
@Nonnull ResultHandler<T> resultHandler)
|
||||
throws SQLException {
|
||||
assertConnectionNotNull(conn);
|
||||
assertSqlNotNull(sql);
|
||||
assertResultMapNotNull(resultMap);
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
fillStatement(stmt, params);
|
||||
try (ResultSet rs = stmt.executeQuery()) {
|
||||
List<T> result = new ArrayList<>();
|
||||
int rowNumber = 0;
|
||||
while (rs.next()) {
|
||||
T e = resultMap.map(rs, rowNumber++);
|
||||
result.add(e);
|
||||
}
|
||||
return result;
|
||||
return resultHandler.handle(rs);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static <T> Optional<T> queryFirst(Connection conn, String sql, Object[] params, ResultMap<T> resultMap)
|
||||
private static <T> List<T> queryListInternal(@Nonnull Connection conn,
|
||||
@Nonnull String sql,
|
||||
@Nullable Object[] params,
|
||||
@Nonnull RowMapper<T> rowMapper)
|
||||
throws SQLException {
|
||||
return query(conn, sql, params, resultMap).stream().findFirst();
|
||||
return queryInternal(conn, sql, params, rs -> {
|
||||
List<T> result = new ArrayList<>();
|
||||
int rowNumber = 0;
|
||||
while (rs.next()) {
|
||||
T e = rowMapper.mapRow(rs, rowNumber++);
|
||||
result.add(e);
|
||||
}
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
private static List<Map<String, Object>> query(Connection conn, String sql, Object[] params)
|
||||
private static <T> Optional<T> queryFirstInternal(@Nonnull Connection conn,
|
||||
@Nonnull String sql,
|
||||
@Nullable Object[] params,
|
||||
@Nonnull RowMapper<T> rowMapper)
|
||||
throws SQLException {
|
||||
return query(conn, sql, params, ResultMap.mapResultMap);
|
||||
return queryInternal(conn, sql, params, rs -> {
|
||||
if (rs.next()) {
|
||||
return Optional.ofNullable(rowMapper.mapRow(rs, 0));
|
||||
}
|
||||
return Optional.empty();
|
||||
});
|
||||
}
|
||||
|
||||
private static Optional<Map<String, Object>> queryFirst(Connection conn, String sql, Object[] params)
|
||||
// #endregion
|
||||
|
||||
// #region - query
|
||||
|
||||
private static <T> T query(Connection conn, String sql, Object[] params, ResultHandler<T> resultHandler)
|
||||
throws SQLException {
|
||||
return queryFirst(conn, sql, params, ResultMap.mapResultMap);
|
||||
assertConnectionNotNull(conn);
|
||||
assertSqlNotNull(sql);
|
||||
assertResultHandlerNotNull(resultHandler);
|
||||
return queryInternal(conn, sql, params, resultHandler);
|
||||
}
|
||||
|
||||
private static List<DbRecord> queryToRecordList(Connection conn, String sql, Object[] params)
|
||||
// #endregion
|
||||
|
||||
// #region - queryList
|
||||
|
||||
private static <T> List<T> queryList(Connection conn, String sql, Object[] params, RowMapper<T> rowMapper)
|
||||
throws SQLException {
|
||||
return query(conn, sql, params, ResultMap.recordResultMap);
|
||||
assertConnectionNotNull(conn);
|
||||
assertSqlNotNull(sql);
|
||||
assertRowMapperNotNull(rowMapper);
|
||||
return queryListInternal(conn, sql, params, rowMapper);
|
||||
}
|
||||
|
||||
private static Optional<DbRecord> queryFirstRecord(Connection conn, String sql, Object[] params)
|
||||
private static <T> List<T> queryList(Connection conn, String sql, Object[] params, Class<T> clazz)
|
||||
throws SQLException {
|
||||
return queryFirst(conn, sql, params, ResultMap.recordResultMap);
|
||||
assertConnectionNotNull(conn);
|
||||
assertSqlNotNull(sql);
|
||||
assertClazzNotNull(clazz);
|
||||
return queryListInternal(conn, sql, params, (rs, rowNumber) -> rs.getObject(1, clazz));
|
||||
}
|
||||
|
||||
private static Optional<String> queryToString(Connection conn, String sql, Object[] params)
|
||||
// #endregion
|
||||
|
||||
// #region - queryFirst
|
||||
|
||||
private static <T> Optional<T> queryFirst(Connection conn, String sql, Object[] params, RowMapper<T> rowMapper)
|
||||
throws SQLException {
|
||||
assertConnectionNotNull(conn);
|
||||
assertSqlNotNull(sql);
|
||||
assertRowMapperNotNull(rowMapper);
|
||||
return queryFirstInternal(conn, sql, params, rowMapper);
|
||||
}
|
||||
|
||||
private static <T> Optional<T> queryFirst(Connection conn, String sql, Object[] params, Class<T> clazz)
|
||||
throws SQLException {
|
||||
assertConnectionNotNull(conn);
|
||||
assertSqlNotNull(sql);
|
||||
assertClazzNotNull(clazz);
|
||||
return queryFirstInternal(conn, sql, params, (rs, rowNumber) -> rs.getObject(1, clazz));
|
||||
}
|
||||
|
||||
private static Optional<String> queryFirstString(Connection conn, String sql, Object[] params)
|
||||
throws SQLException {
|
||||
return queryFirst(conn, sql, params, (rs, rowNumber) -> rs.getString(1));
|
||||
}
|
||||
|
||||
private static OptionalInt queryToInt(Connection conn, String sql, Object[] params)
|
||||
private static OptionalInt queryFirstInt(Connection conn, String sql, Object[] params)
|
||||
throws SQLException {
|
||||
Optional<Integer> result = queryFirst(conn, sql, params, (rs, rowNumber) -> rs.getInt(1));
|
||||
return OptionalTools.toOptionalInt(result);
|
||||
}
|
||||
|
||||
private static OptionalLong queryToLong(Connection conn, String sql, Object[] params)
|
||||
private static OptionalLong queryFirstLong(Connection conn, String sql, Object[] params)
|
||||
throws SQLException {
|
||||
Optional<Long> result = queryFirst(conn, sql, params, (rs, rowNumber) -> rs.getLong(1));
|
||||
return OptionalTools.toOptionalLong(result);
|
||||
}
|
||||
|
||||
private static OptionalDouble queryToDouble(Connection conn, String sql, Object[] params)
|
||||
private static OptionalDouble queryFirstDouble(Connection conn, String sql, Object[] params)
|
||||
throws SQLException {
|
||||
Optional<Double> result = queryFirst(conn, sql, params, (rs, rowNumber) -> rs.getDouble(1));
|
||||
return OptionalTools.toOptionalDouble(result);
|
||||
}
|
||||
|
||||
private static Optional<BigDecimal> queryToBigDecimal(Connection conn, String sql, Object[] params)
|
||||
private static Optional<BigDecimal> queryFirstBigDecimal(Connection conn, String sql, Object[] params)
|
||||
throws SQLException {
|
||||
return queryFirst(conn, sql, params, (rs, rowNumber) -> rs.getBigDecimal(1));
|
||||
}
|
||||
|
||||
// #endregion
|
||||
|
||||
// #region - update & batchUpdate
|
||||
|
||||
private static int update(Connection conn, String sql, Object[] params)
|
||||
throws SQLException {
|
||||
assertConnectionNotNull(conn);
|
||||
@ -556,16 +743,16 @@ public class SimpleJdbcTemplate {
|
||||
*
|
||||
* @param sql 要执行的 SQL 语句
|
||||
* @param params 参数
|
||||
* @param resultMap 结果映射规则
|
||||
* @param rowMapper 结果映射规则
|
||||
*
|
||||
* @return 更新的数据
|
||||
* @throws SQLException 执行 SQL 遇到异常情况将抛出
|
||||
*/
|
||||
private static <T> List<T> update(Connection conn, String sql, Object[] params, ResultMap<T> resultMap)
|
||||
private static <T> List<T> update(Connection conn, String sql, Object[] params, RowMapper<T> rowMapper)
|
||||
throws SQLException {
|
||||
assertConnectionNotNull(conn);
|
||||
assertSqlNotNull(sql);
|
||||
assertResultMapNotNull(resultMap);
|
||||
assertRowMapperNotNull(rowMapper);
|
||||
final List<T> result = new ArrayList<>();
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
|
||||
fillStatement(stmt, params);
|
||||
@ -573,7 +760,7 @@ public class SimpleJdbcTemplate {
|
||||
try (ResultSet generatedKeys = stmt.getGeneratedKeys();) {
|
||||
int rowNumber = 0;
|
||||
while (generatedKeys.next()) {
|
||||
T e = resultMap.map(generatedKeys, rowNumber++);
|
||||
T e = rowMapper.mapRow(generatedKeys, rowNumber++);
|
||||
result.add(e);
|
||||
}
|
||||
}
|
||||
@ -609,7 +796,48 @@ public class SimpleJdbcTemplate {
|
||||
}
|
||||
}
|
||||
|
||||
private static void fillStatement(PreparedStatement stmt, Object[] params)
|
||||
private static List<int[]> batchUpdateAndIgnoreException(Connection conn,
|
||||
String sql, @Nullable Collection<Object[]> params, int batchSize,
|
||||
List<Exception> exceptions)
|
||||
throws SQLException {
|
||||
assertConnectionNotNull(conn);
|
||||
assertSqlNotNull(sql);
|
||||
AssertTools.checkArgument(CollectionTools.isNotEmpty(exceptions),
|
||||
"The list used to store exceptions should be non-null and empty.");
|
||||
if (params == null || params.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
int executeCount = params.size() / batchSize;
|
||||
executeCount = (params.size() % batchSize == 0) ? executeCount : (executeCount + 1);
|
||||
List<int[]> result = Lists.newArrayListWithCapacity(executeCount);
|
||||
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
int i = 0;
|
||||
for (Object[] ps : params) {
|
||||
i++;
|
||||
fillStatement(stmt, ps);
|
||||
stmt.addBatch();
|
||||
final int batchIndex = i % batchSize;
|
||||
if (batchIndex == 0 || i >= params.size()) {
|
||||
try {
|
||||
int[] n = stmt.executeBatch();
|
||||
result.add(n);
|
||||
stmt.clearBatch();
|
||||
}
|
||||
catch (Exception e) {
|
||||
int n = (i >= params.size() && batchIndex != 0) ? batchIndex : batchSize;
|
||||
result.add(new int[n]);
|
||||
stmt.clearBatch();
|
||||
// 收集异常信息
|
||||
exceptions.add(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
private static void fillStatement(@Nonnull PreparedStatement stmt, @Nullable Object[] params)
|
||||
throws SQLException {
|
||||
if (params != null && params.length > 0) {
|
||||
Object param;
|
||||
@ -631,16 +859,28 @@ public class SimpleJdbcTemplate {
|
||||
}
|
||||
}
|
||||
|
||||
// #region - Asserts
|
||||
|
||||
private static void assertConnectionNotNull(Connection conn) {
|
||||
Preconditions.checkArgument(conn != null, "The connection could not be null.");
|
||||
AssertTools.checkArgumentNotNull(conn, "The argument \"conn\" could not be null.");
|
||||
}
|
||||
|
||||
private static void assertSqlNotNull(String sql) {
|
||||
Preconditions.checkArgument(sql != null, "The sql could not be null.");
|
||||
AssertTools.checkArgumentNotNull(sql, "The argument \"sql\" could not be null.");
|
||||
}
|
||||
|
||||
private static void assertResultMapNotNull(ResultMap<?> resultMap) {
|
||||
Preconditions.checkArgument(resultMap != null, "The resultMap could not be null.");
|
||||
private static void assertRowMapperNotNull(RowMapper<?> rowMapper) {
|
||||
AssertTools.checkArgumentNotNull(rowMapper, "The argument \"rowMapper\" could not be null.");
|
||||
}
|
||||
|
||||
private static void assertResultHandlerNotNull(ResultHandler<?> resultHandler) {
|
||||
AssertTools.checkArgumentNotNull(resultHandler, "The argument \"resultHandler\" could not be null.");
|
||||
}
|
||||
|
||||
private static void assertClazzNotNull(Class<?> clazz) {
|
||||
AssertTools.checkArgumentNotNull(clazz, "The argument \"clazz\" could not be null.");
|
||||
}
|
||||
|
||||
// #endregion
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static xyz.zhouxy.jdbc.ParamBuilder.*;
|
||||
import static xyz.zhouxy.plusone.commons.sql.JdbcSql.IN;
|
||||
|
||||
import java.sql.ResultSetMetaData;
|
||||
import java.sql.SQLException;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
@ -27,7 +26,7 @@ import com.zaxxer.hikari.HikariConfig;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
|
||||
import xyz.zhouxy.jdbc.DbRecord;
|
||||
import xyz.zhouxy.jdbc.ResultMap;
|
||||
import xyz.zhouxy.jdbc.RowMapper;
|
||||
import xyz.zhouxy.jdbc.SimpleJdbcTemplate;
|
||||
import xyz.zhouxy.jdbc.SimpleJdbcTemplate.JdbcExecutor;
|
||||
import xyz.zhouxy.plusone.commons.sql.SQL;
|
||||
@ -64,7 +63,7 @@ class SimpleJdbcTemplateTests {
|
||||
.WHERE(IN("id", ids))
|
||||
.toString();
|
||||
log.info(sql);
|
||||
List<DbRecord> rs = jdbcTemplate.queryToRecordList(sql, ids);
|
||||
List<DbRecord> rs = jdbcTemplate.queryRecordList(sql, ids);
|
||||
assertNotNull(rs);
|
||||
for (DbRecord baseEntity : rs) {
|
||||
// log.info("id: {}", baseEntity.getValueAsString("id")); // NOSONAR
|
||||
@ -78,7 +77,7 @@ class SimpleJdbcTemplateTests {
|
||||
List<DbRecord> keys = jdbcTemplate.update(
|
||||
"INSERT INTO base_table(status, created_by) VALUES (?, ?)",
|
||||
buildParams(1, 886L),
|
||||
ResultMap.recordResultMap);
|
||||
RowMapper.RECORD_MAPPER);
|
||||
log.info("keys: {}", keys);
|
||||
assertEquals(1, keys.size());
|
||||
DbRecord result = keys.get(0);
|
||||
@ -92,7 +91,7 @@ class SimpleJdbcTemplateTests {
|
||||
List<DbRecord> keys = jdbcTemplate.update(
|
||||
"UPDATE base_table SET status = ?, version = version + 1, update_time = now(), updated_by = ? WHERE id = ? AND version = ?",
|
||||
buildParams(2, 886, 571328822575109L, 0),
|
||||
ResultMap.recordResultMap);
|
||||
RowMapper.RECORD_MAPPER);
|
||||
log.info("keys: {}", keys);
|
||||
}
|
||||
|
||||
@ -227,7 +226,7 @@ class SimpleJdbcTemplateTests {
|
||||
Optional<TestBean> t = jdbcTemplate.queryFirst(
|
||||
"SELECT * FROM test_table WHERE id = ?",
|
||||
buildParams(22915),
|
||||
ResultMap.beanResultMap(TestBean.class));
|
||||
RowMapper.beanRowMapper(TestBean.class));
|
||||
log.info("t: {}", t);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user