|
|
@ -6,8 +6,10 @@ import com.keyware.shandan.common.util.StringUtils; |
|
|
|
import com.keyware.shandan.dynacmicform.config.spring.ContextHelper; |
|
|
|
import com.keyware.shandan.dynacmicform.config.spring.ContextHelper; |
|
|
|
import org.apache.ibatis.session.SqlSession; |
|
|
|
import org.apache.ibatis.session.SqlSession; |
|
|
|
import org.apache.ibatis.session.SqlSessionFactory; |
|
|
|
import org.apache.ibatis.session.SqlSessionFactory; |
|
|
|
|
|
|
|
import org.mybatis.spring.SqlSessionUtils; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.Reader; |
|
|
|
import java.io.Reader; |
|
|
@ -15,19 +17,24 @@ import java.sql.Date; |
|
|
|
import java.sql.*; |
|
|
|
import java.sql.*; |
|
|
|
import java.util.*; |
|
|
|
import java.util.*; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Component |
|
|
|
public class SqlExecutor { |
|
|
|
public class SqlExecutor { |
|
|
|
private static final Logger log = LoggerFactory.getLogger(SqlExecutor.class); |
|
|
|
private static final Logger log = LoggerFactory.getLogger(SqlExecutor.class); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final SqlSession sqlSession; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public SqlExecutor(SqlSession sqlSession) { |
|
|
|
|
|
|
|
this.sqlSession = sqlSession; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 检查SQL是否可以正常执行 |
|
|
|
* 检查SQL是否可以正常执行 |
|
|
|
* |
|
|
|
* |
|
|
|
* @param sql sql |
|
|
|
* @param sql sql |
|
|
|
* @return boolean |
|
|
|
* @return boolean |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static boolean validateQuery(String sql) { |
|
|
|
public boolean validateQuery(String sql) { |
|
|
|
// 获取SqlSessionFactory实例
|
|
|
|
try (Connection conn = getConnection(); PreparedStatement stmt = conn.prepareStatement(sql)) { |
|
|
|
SqlSessionFactory sqlSessionFactory = getSessionFactory(); |
|
|
|
|
|
|
|
try (SqlSession session = sqlSessionFactory.openSession(); Connection conn = session.getConnection(); PreparedStatement stmt = conn.prepareStatement(sql)) { |
|
|
|
|
|
|
|
ResultSet rs = stmt.executeQuery(); |
|
|
|
ResultSet rs = stmt.executeQuery(); |
|
|
|
rs.close(); |
|
|
|
rs.close(); |
|
|
|
log.debug("==> {}", sql); |
|
|
|
log.debug("==> {}", sql); |
|
|
@ -38,15 +45,15 @@ public class SqlExecutor { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static boolean execute(String sql) throws Exception { |
|
|
|
public boolean execute(String sql) throws Exception { |
|
|
|
if (StringUtils.isEmpty(sql)) { |
|
|
|
if (StringUtils.isEmpty(sql)) { |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
// 获取SqlSessionFactory实例
|
|
|
|
|
|
|
|
SqlSessionFactory sqlSessionFactory = getSessionFactory(); |
|
|
|
|
|
|
|
log.debug("==> SQL: " + sql); |
|
|
|
log.debug("==> SQL: " + sql); |
|
|
|
|
|
|
|
|
|
|
|
try (SqlSession session = sqlSessionFactory.openSession(); Connection conn = session.getConnection(); PreparedStatement stmt = conn.prepareStatement(sql)) { |
|
|
|
try { |
|
|
|
|
|
|
|
Connection conn = sqlSession.getConnection(); |
|
|
|
|
|
|
|
PreparedStatement stmt = conn.prepareStatement(sql); |
|
|
|
return stmt.execute(); |
|
|
|
return stmt.execute(); |
|
|
|
} catch (Exception e) { |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("执行sql查询异常: " + sql, e); |
|
|
|
log.error("执行sql查询异常: " + sql, e); |
|
|
@ -61,15 +68,15 @@ public class SqlExecutor { |
|
|
|
* @return |
|
|
|
* @return |
|
|
|
* @throws SQLException |
|
|
|
* @throws SQLException |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static int[] executeBatch(Collection<String> sqlList) throws SQLException { |
|
|
|
public int[] executeBatch(Collection<String> sqlList) throws SQLException { |
|
|
|
int[] count = new int[sqlList.size()]; |
|
|
|
int[] count = new int[sqlList.size()]; |
|
|
|
if (sqlList.isEmpty()) { |
|
|
|
if (sqlList.isEmpty()) { |
|
|
|
return count; |
|
|
|
return count; |
|
|
|
} |
|
|
|
} |
|
|
|
SqlSessionFactory sqlSessionFactory = getSessionFactory(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PreparedStatement stmt = null; |
|
|
|
PreparedStatement stmt = null; |
|
|
|
try (SqlSession session = sqlSessionFactory.openSession(); Connection conn = session.getConnection()) { |
|
|
|
try { |
|
|
|
|
|
|
|
Connection conn = getConnection(); |
|
|
|
conn.setAutoCommit(false); |
|
|
|
conn.setAutoCommit(false); |
|
|
|
for (String sql : sqlList) { |
|
|
|
for (String sql : sqlList) { |
|
|
|
if (StringUtils.hasText(sql)) { |
|
|
|
if (StringUtils.hasText(sql)) { |
|
|
@ -102,12 +109,11 @@ public class SqlExecutor { |
|
|
|
* @return boolean |
|
|
|
* @return boolean |
|
|
|
* @throws Exception - |
|
|
|
* @throws Exception - |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static boolean executeUpdate(String sql, Object... params) throws Exception { |
|
|
|
public boolean executeUpdate(String sql, Object... params) throws Exception { |
|
|
|
if (StringUtils.isEmpty(sql)) { |
|
|
|
if (StringUtils.isEmpty(sql)) { |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
// 获取SqlSessionFactory实例
|
|
|
|
|
|
|
|
SqlSessionFactory sqlSessionFactory = getSessionFactory(); |
|
|
|
|
|
|
|
log.debug("==> SQL: " + sql); |
|
|
|
log.debug("==> SQL: " + sql); |
|
|
|
// 替换单个?参数为多个,用于拼接IN参数
|
|
|
|
// 替换单个?参数为多个,用于拼接IN参数
|
|
|
|
if (params != null && params.length > 0) { |
|
|
|
if (params != null && params.length > 0) { |
|
|
@ -116,7 +122,9 @@ public class SqlExecutor { |
|
|
|
log.warn("更新参数集合数量过多, size={},请检查调用是否合理!", params.length); |
|
|
|
log.warn("更新参数集合数量过多, size={},请检查调用是否合理!", params.length); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
try (SqlSession session = sqlSessionFactory.openSession(); Connection conn = session.getConnection(); PreparedStatement stmt = conn.prepareStatement(sql)) { |
|
|
|
try { |
|
|
|
|
|
|
|
Connection conn = getConnection(); |
|
|
|
|
|
|
|
PreparedStatement stmt = conn.prepareStatement(sql); |
|
|
|
if (params != null && params.length > 0) { |
|
|
|
if (params != null && params.length > 0) { |
|
|
|
for (int i = 0; i < params.length; i++) { |
|
|
|
for (int i = 0; i < params.length; i++) { |
|
|
|
stmt.setObject(i + 1, params[i]); |
|
|
|
stmt.setObject(i + 1, params[i]); |
|
|
@ -137,12 +145,11 @@ public class SqlExecutor { |
|
|
|
* @param sql |
|
|
|
* @param sql |
|
|
|
* @return |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static List<Map<String, Object>> executeQuery(String sql, Object... params) throws Exception { |
|
|
|
public List<Map<String, Object>> executeQuery(String sql, Object... params) throws Exception { |
|
|
|
if (StringUtils.isBlank(sql)) { |
|
|
|
if (StringUtils.isBlank(sql)) { |
|
|
|
return null; |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
// 获取SqlSessionFactory实例
|
|
|
|
|
|
|
|
SqlSessionFactory sqlSessionFactory = getSessionFactory(); |
|
|
|
|
|
|
|
log.debug("==> SQL: " + sql); |
|
|
|
log.debug("==> SQL: " + sql); |
|
|
|
// 替换单个?参数为多个,用于拼接IN参数
|
|
|
|
// 替换单个?参数为多个,用于拼接IN参数
|
|
|
|
if (params != null && params.length > 0) { |
|
|
|
if (params != null && params.length > 0) { |
|
|
@ -151,7 +158,9 @@ public class SqlExecutor { |
|
|
|
log.warn("查询参数集合数量过多, size={},请检查调用是否合理!", params.length); |
|
|
|
log.warn("查询参数集合数量过多, size={},请检查调用是否合理!", params.length); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
try (SqlSession session = sqlSessionFactory.openSession(); Connection conn = session.getConnection(); PreparedStatement stmt = conn.prepareStatement(sql)) { |
|
|
|
try { |
|
|
|
|
|
|
|
Connection conn = getConnection(); |
|
|
|
|
|
|
|
PreparedStatement stmt = conn.prepareStatement(sql); |
|
|
|
if (params != null && params.length > 0) { |
|
|
|
if (params != null && params.length > 0) { |
|
|
|
for (int i = 0; i < params.length; i++) { |
|
|
|
for (int i = 0; i < params.length; i++) { |
|
|
|
stmt.setObject(i + 1, params[i]); |
|
|
|
stmt.setObject(i + 1, params[i]); |
|
|
@ -186,13 +195,8 @@ public class SqlExecutor { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static SqlSessionFactory getSessionFactory() { |
|
|
|
private Connection getConnection() throws SQLException { |
|
|
|
SqlSessionFactory sqlSessionFactory = ContextHelper.getBean(SqlSessionFactory.class); |
|
|
|
return sqlSession.getConfiguration().getEnvironment().getDataSource().getConnection(); |
|
|
|
if (sqlSessionFactory == null) { |
|
|
|
|
|
|
|
log.warn("无法获取SqlSessionFactory实例,SQL将不被执行。"); |
|
|
|
|
|
|
|
throw new RuntimeException("获取SqlSessionFactory实例异常"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return sqlSessionFactory; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static String clobRsConvert(Clob clob) { |
|
|
|
private static String clobRsConvert(Clob clob) { |
|
|
|