com.mars.jdbc.helper.templete.base.BaseJdbcTemplate Maven / Gradle / Ivy
package com.mars.jdbc.helper.templete.base;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mars.common.constant.MarsConstant;
import com.mars.common.constant.MarsSpace;
import com.mars.common.util.ThreadUtil;
import com.mars.jdbc.helper.base.DBHelper;
import com.mars.jdbc.helper.manager.ConnectionManager;
import com.mars.jdbc.helper.templete.model.SqlBuilderModel;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* jdbc模板父类
*/
public class BaseJdbcTemplate {
private static MarsSpace marsSpace = MarsSpace.getEasySpace();
/**
* 获取数据库连接
*
* @return
* @throws Exception
*/
public static ConnectionManager getConnection(String dataSourceName) throws Exception {
ConnectionManager connectionManager = new ConnectionManager();
/* 获取当前线程中的Connection */
Object obj = ThreadUtil.getThreadLocal().get();
/* 获取数据源名称 */
String dataSourceName2 = getDataSourceName(dataSourceName);
if (obj != null) {
Map connections = (Map) obj;
connectionManager.setConnection(connections.get(dataSourceName2));
connectionManager.setHasTraction(false);
} else {
connectionManager.setConnection(DBHelper.getConnection(dataSourceName2));
connectionManager.setHasTraction(true);
}
return connectionManager;
}
/**
* 获取数据源名称
*
* @return str
*/
public static String getDataSourceName(String dataSourceName) {
if (dataSourceName == null) {
dataSourceName = marsSpace.getAttr(MarsConstant.DEFAULT_DATASOURCE_NAME).toString();
}
return dataSourceName;
}
/**
* 构建sql语句
*
* @param sql
* @param args
* @return
* @throws Exception
*/
public static SqlBuilderModel builderSql(String sql, Object args) throws Exception {
SqlBuilderModel sqlBuilderModel = new SqlBuilderModel();
JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(args));
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy