JFinal 表自动绑定插件实现,实现零配置,支持多数据源

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群版 2核4GB 100GB
推荐场景:
搭建个人博客
云数据库 RDS MySQL,高可用版 2核4GB 50GB
简介: 以mysql数据库实现为例,其它的db也可基于这种方式自己实现大概的思路是这样的,为了简少配置,所以不使用注解的方式首先需要一个工具类来拿到所有的Model类大体的实现方式如下package com.

以mysql数据库实现为例,其它的db也可基于这种方式自己实现

大概的思路是这样的,为了简少配置,所以不使用注解的方式

首先需要一个工具类来拿到所有的Model类大体的实现方式如下

package com.nmtx.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.jfinal.kit.PathKit;
import com.jfinal.kit.StrKit;

public class ClassUtils {
    public static String classRootPath = null;

    public static List<Class<?>> scanPackage(String packageName) throws ClassNotFoundException {
        List<Class<?>> classList = new ArrayList<Class<?>>();
        String path = getClassRootPath() + "/" + packageName.replace(".", "/");
        List<String> fileNameList = getAllFileName(path);
        for (String fileName : fileNameList) {
            classList.add(Class.forName(fileName));
        }
        return classList;
    }

    public static List<String> getAllFileName(String path) {
        List<String> fileNameList = new ArrayList<String>();
        File rootFile = new File(path);
        if (rootFile.isFile()) {
            String fileName = rootFile.getPath().replace(PathKit.getRootClassPath(), "").replace(File.separator, ".")
                    .replaceFirst(".", "");
            String prefix = fileName.substring(fileName.lastIndexOf(".") + 1);
            if (prefix.equals("class")) {
                fileNameList.add(fileName.substring(0, fileName.lastIndexOf(".")));
            }
        } else {
            File[] files = rootFile.listFiles();
            if (files != null) {
                for (File file : files) {
                    fileNameList.addAll(getAllFileName(file.getPath()));
                }
            }
        }
        return fileNameList;
    }

    public static String getClassRootPath() {
        if (StrKit.isBlank(classRootPath))
            classRootPath = PathKit.getRootClassPath();
        return classRootPath;
    }

    public static void setClassRootPath(String classRootPath) {
        ClassUtils.classRootPath = classRootPath;
    }
}

有了工具类,就去处理自动扫描插件,大概实现是这样的

package com.nmtx.plugins.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.IPlugin;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.nmtx.plugins.db.impl.TableToModelByUnderline;
import com.nmtx.utils.ClassUtils;

public class AutoTabelPlugin implements IPlugin {
    private String db;
    private ActiveRecordPlugin arp;
    private String pacekageName;
    private String idKey;
    private ITableToModelFormat tableToModelFormate;
    private C3p0Plugin c3p0Plugin;

    public AutoTabelPlugin(C3p0Plugin erpC3p0, ActiveRecordPlugin arp, String db, String packageName, String idKey,
            ITableToModelFormat tableToModelFormate) {
        this.db = db;
        this.arp = arp;
        this.idKey = idKey;
        this.tableToModelFormate = tableToModelFormate;
        this.c3p0Plugin = erpC3p0;
    }

    public AutoTabelPlugin(C3p0Plugin erpC3p0, ActiveRecordPlugin arp, String db, String packageName, String idKey) {
        this.db = db;
        this.arp = arp;
        this.idKey = idKey;
        this.pacekageName = packageName;
        this.tableToModelFormate = new TableToModelByUnderline();
        this.c3p0Plugin = erpC3p0;
    }

    @SuppressWarnings({ "unchecked" })
    public List<Class<? extends Model<?>>> getModelClass() throws ClassNotFoundException {
        List<Class<?>> classes = ClassUtils.scanPackage(pacekageName);
        List<Class<? extends Model<?>>> modelClasses = new ArrayList<Class<? extends Model<?>>>();
        for (Class<?> classer : classes) {
            modelClasses.add((Class<? extends Model<?>>) classer);
        }
        return modelClasses;
    }

    public boolean start() {
        try {
            HashMap<String, String> tableMap = getTableMap();
            List<Class<? extends Model<?>>> modelClasses = getModelClass();
            for (Class<? extends Model<?>> modelClass : modelClasses) {
                String tableName = tableMap.get(modelClass.getSimpleName());
                if (tableName != null) {
                    if (StrKit.notBlank(idKey)) {
                        arp.addMapping(tableName, idKey, modelClass);
                    } else {
                        arp.addMapping(tableName, modelClass);
                    }

                }
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("auto table mappming is exception" + e);
        }

        return true;
    }

    /**
     * 获取Model和表名的映射
     * 
     * @return
     */
    private HashMap<String, String> getTableMap() {
        HashMap<String, String> map = new HashMap<String, String>();
        Connection connection = null;
        PreparedStatement preStatement = null;
        ResultSet resultSet = null;
        try {
            c3p0Plugin.start();
            connection = c3p0Plugin.getDataSource().getConnection();
            preStatement = connection.prepareStatement(
                    "select table_name as tableName from information_schema.tables where table_schema='" + db
                            + "' and table_type='base table'");
            resultSet = preStatement.executeQuery();
            while (resultSet.next()) {
                String tableName = resultSet.getString(1);
                map.put(tableToModelFormate.getTableByModel(tableName), tableName);
            }
        } catch (Exception e) {
            closeConnection(connection, preStatement, resultSet);
            throw new RuntimeException("auto table mappming is exception" + e);
        } finally {
            closeConnection(connection, preStatement, resultSet);
        }
        return map;

    }

    private void closeConnection(Connection connection, PreparedStatement preStatement, ResultSet resultSet) {
        try {
            if (resultSet != null) {
                resultSet.close();
            }
            if (preStatement != null) {
                preStatement.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException("auto close db connection  is exception" + e);
        }
    }

    public boolean stop() {
        return true;
    }

}

因为java里的属性一般都是驼峰规则,代码看起来舒服一点,这里以数据库中以大写字母为例,表名为T_USER,对应Model名为User实现如下

接口定义

package com.nmtx.plugins.db;

public interface ITableToModelFormat {
   public String generateTableNameToModelName(String tableName);
}

实现如下

package com.nmtx.plugins.db.impl;

import com.nmtx.plugins.db.ITableToModelFormat;

public class TableToModelByUnderline implements ITableToModelFormat{

    public String generateTableNameToModelName(String tableName) {
        StringBuilder modelName = new StringBuilder();
        tableName = tableName.substring(2).toLowerCase();
        String tableNames[] = tableName.split("_");
        for(String tableNameTemp:tableNames){
            modelName.append(fisrtStringToUpper(tableNameTemp));
        }
        return modelName.toString();
    }
    
    private String fisrtStringToUpper(String string){
        return string.replaceFirst(string.substring(0, 1),string.substring(0, 1).toUpperCase()); 
    }
    
}

如果不同的格式可以实现不同的方法,根据自己的需求,这样就完成了自动扫描插件,使用起来也方便如下

C3p0Plugin spuC3p0= new C3p0Plugin(getProperty("jdbc.mysql.url"),
                getProperty("jdbc.mysql.username").trim(), getProperty("jdbc.mysql.password").trim(),
                getProperty("jdbc.mysql.driverClass"));
        spuC3p0.setMaxPoolSize(Integer.parseInt(getProperty("jdbc.mysql.maxPool")));
        spuC3p0.setMinPoolSize(Integer.parseInt(getProperty("jdbc.mysql.minPool")));
        spuC3p0.setInitialPoolSize(Integer.parseInt(getProperty("jdbc.mysql.initialPoolSize")));
        ActiveRecordPlugin spuArp = new ActiveRecordPlugin(DbConfigName.SPU, spuC3p0);
        AutoTabelPlugin spuAutoTabelPlugin = new AutoTabelPlugin(spuC3p0, spuArp, getProperty("jdbc.mysql.spu.db"),
                "com.nmtx.manager.model", "ID");
        me.add(spuAutoTabelPlugin);
        me.add(spuArp);

如果有多个就可以配置多个插件,而无需在管映射了,新增Model直接新增即可,不要再管映射

相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
Java 数据库 开发者
jfinal-weixin是如何支持多公众号配置的
jfinal-weixin是如何支持多公众号配置的
175 0
|
JavaScript
JFinal 参数校验插件扩展,让后台参数校验像js一样方式好用
一、插件实现 插件的功能就是加载校验规则,实现代码如下 package com.nmtx.plugins.validation; import java.util.Properties; import com.
|
JavaScript 前端开发 Java
JFinal框架单文件、多文件上传详解
版权声明:本文为博主原创文章,如需转载,请标明出处。 https://blog.csdn.net/alan_liuyue/article/details/79386540 简介  ...
2748 0
|
Java PHP Spring
从今天开始,要入jfinal的坑了,试试这破框架好不好用。
公司要用jfinal,所以我要入坑了。 听说时去年很火的java框架,不知好不好,试试水吧。 看官网就想吐槽 看个文档吧,还要注册。。。现在是2017年吗?? 好吧,注册完了,接着就。
2000 0
|
Java 调度 Maven
JFinal框架_定时触发程序
JFinal框架进行作业调度,使用JFinal-ext2与quartzf进行配置。 maven说明: com.jfinal jfinal 3.2 com.jfinal jfinal-ext2 2.
1652 0
|
Java 数据格式 XML
jfinal框架文件下载功能代码
版权声明:本文为博主原创文章,如需转载,请标明出处。 https://blog.csdn.net/alan_liuyue/article/details/72779838 一. 前言   上一篇博客我们了解了struts2框架的文件下载功能代码,我们可以从中总结到,struts2主要是通过其xml的配置来处理文件下载的,也就是将普通io流文件下载的页面响应方式的这部分代码分离出来,使用xml配置来处理,这也是struts2的特性。
1732 0