|
@@ -0,0 +1,95 @@
|
|
|
+package cn.com.qmth.examcloud.task.starter.config;
|
|
|
+
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
+
|
|
|
+import javax.sql.DataSource;
|
|
|
+
|
|
|
+import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
+
|
|
|
+import cn.com.qmth.examcloud.commons.base.util.DBUtil;
|
|
|
+
|
|
|
+/**
|
|
|
+ * JDBC模板
|
|
|
+ *
|
|
|
+ * @author WANGWEI
|
|
|
+ * @date 2018年8月3日
|
|
|
+ * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
|
|
|
+ */
|
|
|
+public class JdbcTemplateHolder {
|
|
|
+
|
|
|
+ private static Map<String, JdbcTemplate> jdbcTemplates = new ConcurrentHashMap<String, JdbcTemplate>();
|
|
|
+
|
|
|
+ private static final Object LOCK = new Object();
|
|
|
+
|
|
|
+ public enum DataSourceName {
|
|
|
+ /**
|
|
|
+ * 基础信息
|
|
|
+ */
|
|
|
+ CORE_BASIC("$ds.core.basic"),
|
|
|
+ /**
|
|
|
+ * 考务
|
|
|
+ */
|
|
|
+ CORE_EXAMWORK("$ds.core.examwork"),
|
|
|
+ /**
|
|
|
+ * 题库
|
|
|
+ */
|
|
|
+ CORE_QUESTION("$ds.core.question"),
|
|
|
+ /**
|
|
|
+ * 阅卷
|
|
|
+ */
|
|
|
+ CORE_MARKING("$ds.core.marking"),
|
|
|
+ /**
|
|
|
+ * 网考
|
|
|
+ */
|
|
|
+ CORE_OE("$ds.core.oe");
|
|
|
+
|
|
|
+ private String configName;
|
|
|
+
|
|
|
+ private DataSourceName(String configName) {
|
|
|
+ this.configName = configName;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getConfigName() {
|
|
|
+ return configName;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 启动初始化
|
|
|
+ *
|
|
|
+ * @author WANGWEI
|
|
|
+ */
|
|
|
+ public static void init() {
|
|
|
+ // getJdbcTemplate(DataSourceName.CORE_BASIC);
|
|
|
+ // getJdbcTemplate(DataSourceName.CORE_EXAMWORK);
|
|
|
+ // getJdbcTemplate(DataSourceName.CORE_MARKING);
|
|
|
+ // getJdbcTemplate(DataSourceName.CORE_OE);
|
|
|
+ // getJdbcTemplate(DataSourceName.CORE_QUESTION);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取 JdbcTemplate
|
|
|
+ *
|
|
|
+ * @author WANGWEI
|
|
|
+ * @param dataSourceName
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static JdbcTemplate getJdbcTemplate(DataSourceName dataSourceName) {
|
|
|
+ JdbcTemplate jdbcTemplate = jdbcTemplates.get(dataSourceName.name());
|
|
|
+ if (null == jdbcTemplate) {
|
|
|
+ synchronized (LOCK) {
|
|
|
+ jdbcTemplate = jdbcTemplates.get(dataSourceName.name());
|
|
|
+ if (null == jdbcTemplate) {
|
|
|
+ DataSource dataSource = DBUtil.getDataSource(dataSourceName.getConfigName());
|
|
|
+ jdbcTemplate = new JdbcTemplate(dataSource);
|
|
|
+ jdbcTemplates.put(dataSourceName.name(), jdbcTemplate);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return jdbcTemplate;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|