diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/template/PreparedStatementJdbcCallback.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/template/PreparedStatementJdbcCallback.java index d482dabb..de13689d 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/template/PreparedStatementJdbcCallback.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/template/PreparedStatementJdbcCallback.java @@ -1,16 +1,16 @@ /** * Copyright (c) 2022 aoshiguchen - * + *

* 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: - * + *

* The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. - * + *

* 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 implements JdbcCallback { - @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(); }