开源轻量级数据库访问框架

Posted Zip Zou

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了开源轻量级数据库访问框架相关的知识,希望对你有一定的参考价值。

本框架为开源框架,旨在简化用户的数据库操作,提供便捷的数据库访问服务而封装。该框架依赖于JDBC,并且基于原生JAVA SE框架的封装。

框架对比

对于经常进行数据库开发和JAVA EE开发的编程人员而言,其最先使用到的数据持久化方式则为Hibernate,或者是使用JAVA EE 或者Spring MVC等框架所自带的数据库缓冲池的方式,去管理数据库的连接。
然而,这些框架是非常优秀的,但是却显得不够轻量。
首先很多JAVA EE框架都维持了一个数据库连接的缓冲池,来管理数据库的连接,使得在这个程序中,始终都可以存在可用的数据库连接,从而避免了重复建立连接和释放连接的性能上的损耗。并且数据库连接的管理,不必交给程序员去维护,而是将维护工作,交给了程序本身,使得其自身可以监控数据库连接,当某个连接超过最大空闲时间时,才将其释放,并且在连接池中始终都保持着有不小于最小连接数的数据库连接,等待着用户的访问和使用,但是这些连接又不会过度创建,它们始终被约束在了最大连接数之内,当数据库连接过多时,则需要考虑是否有连接可用,否则需要等待其他工作的完成,从而降低了数据库的压力,提高了数据库的性能,防止高并发带来的压力。
这在Web设计中是经常使用的,并且是一种有效的解决方案,并且在Hibernate还提供了高效的缓存技术。
然而这种方式,对于客户端而言,及并发要求不高,服务器资源不尽充足的条件下,是存在很大弊端的。在这种解决方案中,始终在连接池中维护了一定数量的连接池,供用户重复使用,虽然其降低了创建连接和释放连接的开销,但是其需要始终在内存中存在这些连接备不时之需,那么对服务器的内存是一个很高的要求。
并且在并发并不是很高的项目中,这些连接很可能不会经常被使用,那么始终保持着连接,实际上也是在浪费和占用资源,浪费带宽(数据库服务和Web服务不在同一主机)和浪费内存,那么这种解决方案的弊端,便显现出来,并且始终管理连接池,也会需要开销,此时的开销不一定比重复创建和释放连接的开销小,因此在这种情形下,使用连接池并不是一种最优的解决方案,而使用传统的JDBC更加能提高数据库访问的效率,和节省不必要的资源消耗。
以上便是该框架所拥有的优势和适用的情形。

UML设计


该UML设计采用Power Designer进行,并遵从UML规范进行设计。
在该框架中,涉及类为10个:

  1. AbstractQueryObject:主要负责数据库的操作,处理用户提交的SQL请求,需要针对不同的业务,创建实例化不同的子类进行操作。
  2. DatabaseFactory:数据库工厂,主要用户创建不同的数据库,在该接口中,只有一个databaseInfo方法,并且有多个重载版本,主要为适应于本地数据库和远程数据库
  3. DBBasicInfo:抽象基类,为数据库的基本信息,数据库名,用户名,URL地址等信息为基本属性,并且抽象出了url的get方法,使得子类自行拼接JDBC的连接URL
  4. DBConnection:数据库连接类,主要管理数据库的连接,负责连接的创建(打开), 关闭等操作
  5. IConnection:数据库连接的接口,主要提供数据库的连接操作接口
  6. Model:工具类,借助反射机制,将数据库查询结果依次解析为对应的实体类
  7. MSSQLDBInfo:Microsoft SQLServer数据库信息,为DBBasicInfo的子类,用于SQLServer的基本信息的服务
  8. MSSQLFactory:Microsoft SQLServer工厂类,可产生指定类型的SQLServer数据库信息类,供其他类使用
  9. mysqlDBInfo:MySQL 数据库信息类,为DBBasicInfo子类,用于提供MySQL数据库的基本信息
  10. MySQLFactory:MySQL数据库工厂类,可产生指定的MySQL数据库信息

使用方式

  1. 子类化AbstractQueryObject类,并重载setupDatabase方法,在该方法中指定数据库信息。
  2. 提供入口函数进行测试使用。
    如:
/**
 *测试查询类
 *@author Frank
 **/
public final class QueryObject extends AbstractQueryObject 

    /* (non-Javadoc)
     * @see com.dbquery.interfaces.AbstractQueryObject#setupDatabase()
     */
    @Override
    public void setupDatabase() 
        DatabaseFactory factory = new MySQLFactory();
        DBBasicInfo mysql = factory.databaseInfo("test");
        DBConnection connection = new DBConnection();
        mysql.setUsername("root");
        mysql.setPassword("123456");
        connection.setDbinfo(mysql);
        dbManager = connection;
    

    private final static String SQL_CREATE_TABLE = "CREATE TABLE TB_QUERY(id int primary key, content char(120) not null);";
    /**
     * 示例,创建一个表
     * @return
     */
    public boolean createTable() 
        avoidNotBindDatabase();
        return this.update(SQL_CREATE_TABLE, new Object[0]);
    

使用JUnit4测试如下:

public class TestUnit 

    @Test
    public void testCreateTable() 
        QueryObject query = new QueryObject();
        if(!query.createTable()) 
            fail("创建失败!");
        
    


如此,即可完成对数据库的操作,而无需使用过多的语句进行处理,也省去了繁琐的而数据库创建和连接的过程,所有的内部过程都自动化进行。

注:该框架是依赖于JDBC的,因此在进行数据库操作时,需要在BuildPath中引用对应数据库类型的JDBC驱动包,如MySQL需要提供MySQL的驱动。

更优雅的方式在于,重新创建一个抽象基类泛化自AbstractQueryObject类,并且重载该setupDatabase方法,在之后的数据库访问类中,都继承自该抽象基类,并且实现业务所需要的接口。详细请参照Demo中的实例代码。

未完善之处

目前该框架,仅支持了MySQL和MSSQL数据库,未适配Oracle等其他数据库。但是对其他数据库的适配也极其简单,只需要遵照工厂方法模式对DatabaseFactory进行子类化以创建不同的数据库信息类,并且具体化DBBasicInfo类,重载其URL的拼接方法即可。
重载完成之后,即可在setupDatabase方法中,指定数据库的信息进行数据库的访问和交互。

Demo测试

Tue Aug 23 15:10:13 CST 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
创建成功!
Tue Aug 23 15:10:14 CST 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
插入成功!
Tue Aug 23 15:10:14 CST 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
[TableModel [id=1, content=测试实例1], TableModel [id=2, content=测试实例2]]
Tue Aug 23 15:10:15 CST 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
更新成功!
Tue Aug 23 15:10:15 CST 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
[TableModel [id=1, content=新内容], TableModel [id=2, content=测试实例2]]
Tue Aug 23 15:10:15 CST 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
删除:1

测试结果截图:
原始表:

新建表:

增删查改后:

支持作者

该框架已经托管为Github仓库,请移步下载:项目源码地址
框架地址:已编译版本
Demo下载地址为:Demo源码下载地址
如果有帮助,请到Github进行star或提交错误,请支持原创,转载注明出处!

以上是关于开源轻量级数据库访问框架的主要内容,如果未能解决你的问题,请参考以下文章

Hibernate 框架理解

阿里开源Sentinel流控框架基本介绍与简单使用

Dubbo高性能轻量级的开源Java RPC框架

Hibernate (开源对象关系映射框架)

好用的WPF开源UI框架项目

Spring的简介使用特点