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

View File

@ -37,8 +37,8 @@ public class StatementUtil {
* @return {@link PreparedStatement} * @return {@link PreparedStatement}
* @throws SQLException SQL执行异常 * @throws SQLException SQL执行异常
*/ */
public static PreparedStatement fillParams(PreparedStatement ps, Collection<Object> params) throws SQLException { public static PreparedStatement fillParams(PreparedStatement ps, Object... params) throws SQLException {
return fillParams(ps, params.toArray(new Object[params.size()])); return fillParams(ps, new ArrayIter<>(params));
} }
/** /**
@ -50,42 +50,14 @@ public class StatementUtil {
* @return {@link PreparedStatement} * @return {@link PreparedStatement}
* @throws SQLException SQL执行异常 * @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)) { if (ArrayUtil.isEmpty(params)) {
return ps;// 无参数 return ps;// 无参数
} }
Object param;
for (int i = 0; i < params.length; i++) { int paramIndex = 1;//第一个参数从1计数
int paramIndex = i + 1; for (Object param : params) {
param = params[i]; setParam(ps, paramIndex++, param);
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));
}
} }
return ps; return ps;
} }
@ -208,7 +180,7 @@ public class StatementUtil {
* @throws SQLException SQL执行异常 * @throws SQLException SQL执行异常
*/ */
public static Long getGeneratedKeyOfLong(Statement ps) throws SQLException { public static Long getGeneratedKeyOfLong(Statement ps) throws SQLException {
try(final ResultSet rs = ps.getGeneratedKeys()) { try (final ResultSet rs = ps.getGeneratedKeys()) {
Long generatedKey = null; Long generatedKey = null;
if (rs != null && rs.next()) { if (rs != null && rs.next()) {
try { try {
@ -264,4 +236,50 @@ public class StatementUtil {
return sqlType; 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 * @author Looly
* *
*/ */
public static enum LikeType { public enum LikeType {
/** 以给定值开头拼接后的SQL "value%" */ /** 以给定值开头拼接后的SQL "value%" */
StartWith, StartWith,
/** 以给定值开头拼接后的SQL "%value" */ /** 以给定值开头拼接后的SQL "%value" */
@ -456,7 +456,6 @@ public class Condition extends CloneSupport<Condition> {
this.operator = OPERATOR_BETWEEN; this.operator = OPERATOR_BETWEEN;
this.value = unwrapQuote(betweenValueStrs.get(0)); this.value = unwrapQuote(betweenValueStrs.get(0));
this.secondValue = unwrapQuote(betweenValueStrs.get(1)); this.secondValue = unwrapQuote(betweenValueStrs.get(1));
return;
} }
} }

View File

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