ActiveRecord初始化,可以实现jfinal系统启动完成后,再建立数据库连接

1、JFinalConfig的afterJFinalStart方法,可以实现系统启动成功后,调用的方法

2、ActiveRecord 多数据源初始化

package com.meiah.common;

import java.util.List;

import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.activerecord.dialect.SqlServerDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.meiah.common.mapping.TaskCenterMappingKit;
import com.meiah.common.mapping.WxMappingKit;
import com.meiah.model.ClusterInfoModel;
import com.meiah.model.Ics_tasks;

/**
 * 创建日期:2017年9月29日上午8:56:27
 * 作者:zhangsp
 * 系统启动成功后,构建所有集群
 * version 1.0.0
 * 
 */
public class ActiveRecordCluster {

    public static void main(String[] args) {
        List<ClusterInfoModel> clusterInfoList = ClusterInfoModel.dao.find("select * from clusterInfos");  
        for (int j = 0; j < clusterInfoList.size(); j++) {
            String jdbcUrl = "";
            String driver = "";
            if(clusterInfoList.get(j).getInt("clusterDriver")==1){//sqlserver
                jdbcUrl = "jdbc:jtds:sqlserver://"+clusterInfoList.get(j).getStr("clusterIp")+
                        ":"+clusterInfoList.get(j).getInt("clusterPort")+
                        "/"+clusterInfoList.get(j).getStr("clusterSqlName")+";useLOBs=false";
                driver = "net.sourceforge.jtds.jdbc.Driver";
            }else{//mysql
                jdbcUrl = "jdbc:mysql://"+clusterInfoList.get(j).getStr("clusterIp")+
                        ":"+clusterInfoList.get(j).getInt("clusterPort")+
                        "/"+clusterInfoList.get(j).getStr("clusterSqlName")+"?useUnicode=true&characterEncoding=UTF8";
                driver = "com.mysql.jdbc.Driver";
            }
            
            DruidPlugin dp = new DruidPlugin(jdbcUrl, clusterInfoList.get(j).getStr("clusterUser"), 
                    clusterInfoList.get(j).getStr("clusterPwd"), driver);
            ActiveRecordPlugin arp = new ActiveRecordPlugin("db"+clusterInfoList.get(j).getInt("id"), dp);
            if(clusterInfoList.get(j).getInt("clusterState") ==1){//facebook
//                FbMappingKit.mapping(arp);
                arp.addMapping("ics_tasks", "id", Ics_tasks.class);
                SqlInfo.fbList.add("db"+clusterInfoList.get(j).getInt("id"));
                SqlInfo.fbClusterList.add(clusterInfoList.get(j).getStr("clusterName"));
                arp.setDialect(new SqlServerDialect());///sqlserver
                System.out.println("fb:"+clusterInfoList.get(j).getInt("id"));
                
            }else if(clusterInfoList.get(j).getInt("clusterState") ==2){//微信
                WxMappingKit.mapping(arp);
                SqlInfo.wxList.add("db"+clusterInfoList.get(j).getInt("id"));
                SqlInfo.wxClusterList.add(clusterInfoList.get(j).getStr("clusterName"));
                arp.setDialect(new SqlServerDialect());///sqlserver
                System.out.println("wx:"+clusterInfoList.get(j).getInt("id"));
                
            }else if(clusterInfoList.get(j).getInt("clusterState") ==0){//mysql
                TaskCenterMappingKit.mapping(arp);
                SqlInfo.mysqlList.add("db"+clusterInfoList.get(j).getInt("id"));
                SqlInfo.myClustersqlList.add(clusterInfoList.get(j).getStr("clusterName"));
                arp.setDialect(new MysqlDialect());///mysql
                System.out.println("mysql:"+clusterInfoList.get(j).getInt("id"));
                
            }
//            arp.addMapping("blog", Blog.class);
            
            // 与web环境唯一的不同是要手动调用一次相关插件的start()方法
            dp.start();
            arp.start();
        }

        // 通过上面简单的几行代码,即可立即开始使用
//        new Blog().set("title", "title").set("content", "cxt text").save();
//        Blog.dao.findById(123);
    }

}
原文地址:https://www.cnblogs.com/learningJAVA/p/7609995.html