多数据源实现,平时刷数据或者项目配置,简洁舒爽
import com.jfinal.kit.Prop; import com.jfinal.kit.PropKit; import com.jfinal.plugin.IPlugin; import com.jfinal.plugin.activerecord.ActiveRecordPlugin; import com.jfinal.plugin.activerecord.IContainerFactory; import com.jfinal.plugin.druid.DruidPlugin; import lombok.Data; import java.util.*; /** * @author zhangshiqiang on 2021/5/31. */ public class ArpPlugin implements IPlugin { private static final Prop config = PropKit.use("init_db.properties"); private static ArpPlugin arpPlugin = null; private final Map<String, Object> pluginMap = new HashMap<>(); private final Map<String, DataConfig> envConfigMap = new HashMap<>(); public ArpPlugin() { arpPlugin = this; } @Override public boolean start() { init(); return true; } public void init() { Properties properties = config.getProperties(); Enumeration<?> enumeration = properties.propertyNames(); String _urlSuffix = ".url"; String _userSuffix = ".user"; String _pwdSuffix = ".pwd"; while (enumeration.hasMoreElements()) { String key = (String) enumeration.nextElement(); String env = key.substring(0, key.indexOf(".")); if (!envConfigMap.containsKey(env)) { DataConfig dataConfig = new DataConfig(); dataConfig.setUrl(properties.getProperty(env + _urlSuffix)); dataConfig.setUser(properties.getProperty(env + _userSuffix)); dataConfig.setPwd(properties.getProperty(env + _pwdSuffix)); envConfigMap.put(env, dataConfig); DruidPlugin dp = new DruidPlugin(dataConfig.getUrl(), dataConfig.getUser(), dataConfig.getPwd()); dp.start(); ActiveRecordPlugin arp = new ActiveRecordPlugin(env, dp); arp.start(); // 是否显示sql arp的参数完全可以自行扩展 // arp.setShowSql(true); pluginMap.put(env + "_dp", dp); pluginMap.put(env + "_arp", arp); } } } @Override public boolean stop() { for (String env : envConfigMap.keySet()) { close(env); } return true; } public void close(String env) { Optional.ofNullable((DruidPlugin) pluginMap.get(env + "_dp")) .ifPresent(DruidPlugin::stop); Optional.ofNullable((ActiveRecordPlugin) pluginMap.get(env + "_arp")) .ifPresent(ActiveRecordPlugin::stop); } @Data public static class DataConfig { private String url; private String user; private String pwd; } public void setContainerFactory(String env, IContainerFactory containerFactory) { Optional.ofNullable((ActiveRecordPlugin) pluginMap.get(env + "_arp")) .ifPresent(e -> e.setContainerFactory(containerFactory)); } public IContainerFactory getLinkedHashMapContainerFactory() { return new IContainerFactory() { public Map<String, Object> getAttrsMap() { return new LinkedHashMap<>(); } public Map<String, Object> getColumnsMap() { return new LinkedHashMap<>(); } public Set<String> getModifyFlagSet() { return new LinkedHashSet<>(); } }; } public static ArpPlugin getArpPlugin() { return arpPlugin; } }
配置文件 init_db.properties
### hato_tt_web_prod hato_tt_web_prod.url= hato_tt_web_prod.user= hato_tt_web_prod.pwd= ### hato_tt_spider_prod hato_tt_spider_prod.url= hato_tt_spider_prod.user= hato_tt_spider_prod.pwd= ### hato_tt_spider_dev hato_tt_spider_dev.url= hato_tt_spider_dev.user= hato_tt_spider_dev.pwd=