!4 修复 You need to specify Statement.RETURN_GENERATED_KEYS to Statement.executeUpdate(), Statement.executeLargeUpdate() or Connection.prepareStatement(). 问题

Merge pull request !4 from tan90/feature/1.6.5
This commit is contained in:
傲世孤尘
2023-02-26 07:56:27 +00:00
committed by Gitee
@@ -1,16 +1,16 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* <p>
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -27,6 +27,7 @@ import lombok.extern.slf4j.Slf4j;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
/**
*
@@ -36,59 +37,59 @@ import java.sql.SQLException;
@Slf4j
public abstract class PreparedStatementJdbcCallback<T> implements JdbcCallback<T> {
@Override
public T execute() throws SQLException {
PreparedStatement pstm = null;
Object[] params = this.getParams();
Connection conn = getConnection();
@Override
public T execute() throws SQLException {
PreparedStatement pstm = null;
Object[] params = this.getParams();
Connection conn = getConnection();
T res = null;
T res = null;
log.debug("sql:" + this.getSql());
StringBuffer sb = new StringBuffer();
if (ArrayUtil.notEmpty(params)) {
for(Object o : params){
sb.append(o).append(",");
}
log.debug("sql:" + this.getSql());
StringBuffer sb = new StringBuffer();
if (ArrayUtil.notEmpty(params)) {
for (Object o : params) {
sb.append(o).append(",");
}
if(sb.length() > 0 && sb.charAt(sb.length() - 1) == ','){
sb.deleteCharAt(sb.length() - 1);
}
}
log.debug("params:" + sb.toString());
pstm = conn.prepareStatement(this.getSql());
if (ArrayUtil.notEmpty(params)) {
for(int i = 0;i < params.length;i++){
pstm.setObject(i + 1, params[i]);
}
}
res = this.execute(pstm);
if (sb.length() > 0 && sb.charAt(sb.length() - 1) == ',') {
sb.deleteCharAt(sb.length() - 1);
}
}
log.debug("params:" + sb.toString());
pstm = conn.prepareStatement(this.getSql(), Statement.RETURN_GENERATED_KEYS);
if (ArrayUtil.notEmpty(params)) {
for (int i = 0; i < params.length; i++) {
pstm.setObject(i + 1, params[i]);
}
}
res = this.execute(pstm);
return res;
}
return res;
}
/**
* 获取参数
* @return
*/
abstract Object[] getParams();
/**
* 获取参数
* @return
*/
abstract Object[] getParams();
/**
* 获取sql语句
* @return
*/
abstract String getSql();
/**
* 获取sql语句
* @return
*/
abstract String getSql();
/**
* 执行
* @param ps
* @return
*/
abstract T execute(PreparedStatement ps) throws SQLException;
/**
* 执行
* @param ps
* @return
*/
abstract T execute(PreparedStatement ps) throws SQLException;
/**
* 获取数据库连接
* @return
*/
abstract Connection getConnection();
/**
* 获取数据库连接
* @return
*/
abstract Connection getConnection();
}