add setParam

This commit is contained in:
Looly 2019-09-21 08:30:14 +08:00
parent c648c8c945
commit 917d3cb28d
4 changed files with 57 additions and 40 deletions

View File

@ -15,6 +15,7 @@
* 【core】 修复Validator注释错误pr#70@Gitee
* 【cron】 添加获取任务表的方法issue#I12E5H@Gitee
* 【http】 SoapClient增加reset方法用于此对象的复用issue#I12CCC@Gitee
* 【db】 StatementUtil增加setParam方法
### Bug修复
* 【core】 修复DateUtil.offset导致的时区错误问题issue#I1294O@Gitee

View File

@ -37,8 +37,8 @@ public class StatementUtil {
* @return {@link PreparedStatement}
* @throws SQLException SQL执行异常
*/
public static PreparedStatement fillParams(PreparedStatement ps, Collection<Object> params) throws SQLException {
return fillParams(ps, params.toArray(new Object[params.size()]));
public static PreparedStatement fillParams(PreparedStatement ps, Object... params) throws SQLException {
return fillParams(ps, new ArrayIter<>(params));
}
/**
@ -50,42 +50,14 @@ public class StatementUtil {
* @return {@link PreparedStatement}
* @throws SQLException SQL执行异常
*/
public static PreparedStatement fillParams(PreparedStatement ps, Object... params) throws SQLException {
public static PreparedStatement fillParams(PreparedStatement ps, Iterable<?> params) throws SQLException {
if (ArrayUtil.isEmpty(params)) {
return ps;// 无参数
}
Object param;
for (int i = 0; i < params.length; i++) {
int paramIndex = i + 1;
param = params[i];
if (null != param) {
if (param instanceof java.util.Date) {
// 日期特殊处理
if (param instanceof java.sql.Date) {
ps.setDate(paramIndex, (java.sql.Date) param);
} else if (param instanceof java.sql.Time) {
ps.setTime(paramIndex, (java.sql.Time) param);
} else {
ps.setTimestamp(paramIndex, SqlUtil.toSqlTimestamp((java.util.Date) param));
}
} else if (param instanceof Number) {
// 针对大数字类型的特殊处理
if (param instanceof BigInteger) {
// BigInteger转为Long
ps.setLong(paramIndex, ((BigInteger) param).longValue());
} else if (param instanceof BigDecimal) {
// BigDecimal的转换交给JDBC驱动处理
ps.setBigDecimal(paramIndex, (BigDecimal) param);
} else {
// 普通数字类型按照默认传入
ps.setObject(paramIndex, param);
}
} else {
ps.setObject(paramIndex, param);
}
} else {
ps.setNull(paramIndex, getTypeOfNull(ps, paramIndex));
}
int paramIndex = 1;//第一个参数从1计数
for (Object param : params) {
setParam(ps, paramIndex++, param);
}
return ps;
}
@ -208,7 +180,7 @@ public class StatementUtil {
* @throws SQLException SQL执行异常
*/
public static Long getGeneratedKeyOfLong(Statement ps) throws SQLException {
try(final ResultSet rs = ps.getGeneratedKeys()) {
try (final ResultSet rs = ps.getGeneratedKeys()) {
Long generatedKey = null;
if (rs != null && rs.next()) {
try {
@ -245,7 +217,7 @@ public class StatementUtil {
* 获取null字段对应位置的数据类型<br>
* 有些数据库对于null字段必须指定类型否则会插入报错此方法用于获取其类型如果获取失败使用默认的{@link Types#VARCHAR}
*
* @param ps {@link Statement}
* @param ps {@link Statement}
* @param paramIndex 参数位置第一位从1开始
* @return 数据类型默认{@link Types#VARCHAR}
* @since 4.6.7
@ -264,4 +236,50 @@ public class StatementUtil {
return sqlType;
}
/**
* {@link PreparedStatement} 设置单个参数
*
* @param ps {@link PreparedStatement}
* @param paramIndex 参数位置从1开始
* @param param 参数
* @throws SQLException SQL异常
* @since 4.6.7
*/
public static void setParam(PreparedStatement ps, int paramIndex, Object param) throws SQLException {
if (null == param) {
ps.setNull(paramIndex, getTypeOfNull(ps, paramIndex));
return;
}
// 日期特殊处理默认按照时间戳传入避免毫秒丢失
if (param instanceof java.util.Date) {
if (param instanceof java.sql.Date) {
ps.setDate(paramIndex, (java.sql.Date) param);
} else if (param instanceof java.sql.Time) {
ps.setTime(paramIndex, (java.sql.Time) param);
} else {
ps.setTimestamp(paramIndex, SqlUtil.toSqlTimestamp((java.util.Date) param));
}
return;
}
// 针对大数字类型的特殊处理
if (param instanceof Number) {
if (param instanceof BigDecimal) {
// BigDecimal的转换交给JDBC驱动处理
ps.setBigDecimal(paramIndex, (BigDecimal) param);
return;
}
if (param instanceof BigInteger) {
// BigInteger转为Long
ps.setBigDecimal(paramIndex, new BigDecimal((BigInteger) param));
return;
}
// 忽略其它数字类型按照默认类型传入
}
// 其它参数类型
ps.setObject(paramIndex, param);
}
}

View File

@ -26,7 +26,7 @@ public class Condition extends CloneSupport<Condition> {
* @author Looly
*
*/
public static enum LikeType {
public enum LikeType {
/** 以给定值开头拼接后的SQL "value%" */
StartWith,
/** 以给定值开头拼接后的SQL "%value" */
@ -456,7 +456,6 @@ public class Condition extends CloneSupport<Condition> {
this.operator = OPERATOR_BETWEEN;
this.value = unwrapQuote(betweenValueStrs.get(0));
this.secondValue = unwrapQuote(betweenValueStrs.get(1));
return;
}
}

View File

@ -116,7 +116,6 @@ public class NamedSql {
// 无变量对应值原样输出
sqlBuilder.append(nameStartChar).append(name);
}
nameStartChar = null;
name.clear();
}