zoukankan      html  css  js  c++  java
  • jfinal多数据源

    比较喜欢JFinal简单的设计。但是我们的不少项目都需要连接至少两个数据库,而JFinal的ActiveRecord对多数据源支持比较若。周末断断续续花了一天的时间对JFinal进行的ActiveRecord做改造。

    设计目标如下:
    1)既有的单数据源的JFinal代码不用修改一行代码就可以自然支持。
    2)Model,Record都支持多数据源,而且改动量要小。
    3)性能不能很大损失。

    实现思路:
    1)一个数据源即一个分组(group),多个数据源既有多个分组,每个Mode只能属于一个分组。Record不限制分组。
    2)default是一个默认分组,当不指定分组名师,默认采用default分组。这个用来解决兼容问题。
    3)尽量启动时计算一下,减少每次请求的计算。

    实现结果:
    1)原来的JFinal方式兼容,但是删除掉了Model和Record中关于DataSource为参数的操作,给位group
    多数据源方式:

    @Override
    public void configPlugin(Plugins me) {
    	//从配置文件中获取数据库配置项
    	PropertyConfig config = PropertyConfig.me();
    	//a数据源
    	DruidPlugin aDruidPlugin = new DruidPlugin(
    			config.getProperty("a.dataSource.url"), 
    			config.getProperty("a.dataSource.userName"), 
    			config.getProperty("a.dataSource.password"), 
    			config.getProperty("a.dataSource.driverClass"));
    	aDruidPlugin.setInitialSize(3).setMaxActive(10);
    	//b数据源
    	DruidPlugin bDruidPlugin = new DruidPlugin(
    			config.getProperty("b.dataSource.url"), 
    			config.getProperty("b.dataSource.userName"), 
    			config.getProperty("b.dataSource.password"), 
    			config.getProperty("b.dataSource.driverClass"));
    	bDruidPlugin.setInitialSize(3).setMaxActive(10);
    	//加载数据库连接池插件
    	me.add(aDruidPlugin);
    	me.add(bDruidPlugin);
    	//分组a,数据源b
    	ActiveRecordPlugin aArp = new ActiveRecordPlugin("aGroup",aDruidPlugin);
    	aArp.setShowSql(true);
    	aArp.addMapping("t_admin_user", AdminUser.class);
    	aArp.addMapping("t_charge_history", ChargeHistory.class);
    	aArp.addMapping("t_sms_api_user", SmsApiUser.class);
    	me.add(aArp);
    	//分组b,数据源b
    	ActiveRecordPlugin bArp = new ActiveRecordPlugin("bGroup",bDruidPlugin);
    	bArp.setDialect(new PostgreSqlDialect());
    	bArp.setShowSql(true);
    	bArp.addMapping("t_send_message_history", SendMessageHistory.class);
    	me.add(bArp);
    }

    差异在于ActiveRecordPlugin多了一个构造函数。Model的写法和以前一样。不用改变。

    如果要在Controller上配置事务(TX系列拦截器)拦截器,configRoute(Routes me) 函数也得做一定改变。若不用事务拦截器,则还和原来一样。使用拦截器的话代码如下:

    public void configRoute(Routes me) {
    	me.addWithGroup("aGroup","/", IndexController.class);
    	me.addWithGroup("aGroup","/au",AdminUserController.class);
    	me.addWithGroup("aGroup","/sau", SmsApiUserController.class);
    	me.addWithGroup("bGroup","/smh",SendMessageHistoryController.class);
    }
    spring集成mongodb
  • 相关阅读:
    深入Android 【一】 —— 序及开篇
    Android中ContentProvider和ContentResolver使用入门
    深入Android 【六】 —— 界面构造
    The service cannot be activated because it does not support ASP.NET compatibility. ASP.NET compatibility is enabled for this application. Turn off ASP.NET compatibility mode in the web.config or add the AspNetCompatibilityRequirements attribute to the ser
    Dynamic Business代码片段总结
    对文件的BuildAction以content,resource两种方式的读取
    paraview 3.12.0 windows下编译成功 小记
    百度网盘PanDownload使用Aria2满速下载
    netdata的安装与使用
    用PS给证件照排版教程
  • 原文地址:https://www.cnblogs.com/yinlixin/p/8443871.html
Copyright © 2011-2022 走看看