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